mirror of
https://github.com/ZwareBear/awx.git
synced 2026-04-06 08:01:48 -05:00
Add the expected launch and run methods to the different job type models. Include a new helper function to look up the right model given a job type and use it in place of switch statements or passing the type in to build a url.
50 lines
1.1 KiB
JavaScript
50 lines
1.1 KiB
JavaScript
import { useState, useEffect } from 'react';
|
|
import useWebsocket from '../../util/useWebsocket';
|
|
import { getJobModel } from '../../util/jobs';
|
|
|
|
export default function useWsJob(initialJob) {
|
|
const [job, setJob] = useState(initialJob);
|
|
const lastMessage = useWebsocket({
|
|
jobs: ['status_changed'],
|
|
control: ['limit_reached_1'],
|
|
});
|
|
|
|
useEffect(() => {
|
|
setJob(initialJob);
|
|
}, [initialJob]);
|
|
|
|
useEffect(
|
|
function parseWsMessage() {
|
|
async function fetchJob() {
|
|
const { data } = await getJobModel(job.type).readDetail(job.id);
|
|
setJob(data);
|
|
}
|
|
|
|
if (!job || lastMessage?.unified_job_id !== job.id) {
|
|
return;
|
|
}
|
|
|
|
if (
|
|
['successful', 'failed', 'error', 'cancelled'].includes(
|
|
lastMessage.status
|
|
)
|
|
) {
|
|
fetchJob();
|
|
} else {
|
|
setJob(updateJob(job, lastMessage));
|
|
}
|
|
},
|
|
[lastMessage] // eslint-disable-line react-hooks/exhaustive-deps
|
|
);
|
|
|
|
return job;
|
|
}
|
|
|
|
function updateJob(job, message) {
|
|
return {
|
|
...job,
|
|
finished: message.finished,
|
|
status: message.status,
|
|
};
|
|
}
|