mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Refactor checkJobStatus() to use API URLs provided via context
This commit is contained in:
2
netbox/project-static/dist/jobs.js
vendored
2
netbox/project-static/dist/jobs.js
vendored
File diff suppressed because one or more lines are too long
2
netbox/project-static/dist/jobs.js.map
vendored
2
netbox/project-static/dist/jobs.js.map
vendored
File diff suppressed because one or more lines are too long
@ -4,7 +4,7 @@ import { apiGetBase, hasError, getNetboxData } from './util';
|
|||||||
let timeout: number = 1000;
|
let timeout: number = 1000;
|
||||||
|
|
||||||
interface JobInfo {
|
interface JobInfo {
|
||||||
id: Nullable<string>;
|
url: Nullable<string>;
|
||||||
complete: boolean;
|
complete: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,15 +23,16 @@ function asyncTimeout(ms: number) {
|
|||||||
function getJobInfo(): JobInfo {
|
function getJobInfo(): JobInfo {
|
||||||
let complete = false;
|
let complete = false;
|
||||||
|
|
||||||
const id = getNetboxData('data-job-id');
|
// Determine the API URL for the job status
|
||||||
const jobComplete = getNetboxData('data-job-complete');
|
const url = getNetboxData('data-job-url');
|
||||||
|
|
||||||
// Determine the job completion status, if present. If the job is not complete, the value will be
|
// Determine the job completion status, if present. If the job is not complete, the value will be
|
||||||
// "None". Otherwise, it will be a stringified date.
|
// "None". Otherwise, it will be a stringified date.
|
||||||
|
const jobComplete = getNetboxData('data-job-complete');
|
||||||
if (typeof jobComplete === 'string' && jobComplete.toLowerCase() !== 'none') {
|
if (typeof jobComplete === 'string' && jobComplete.toLowerCase() !== 'none') {
|
||||||
complete = true;
|
complete = true;
|
||||||
}
|
}
|
||||||
return { id, complete };
|
return { url, complete };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,10 +60,10 @@ function updateLabel(status: JobStatus) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively check the job's status.
|
* Recursively check the job's status.
|
||||||
* @param id Job ID
|
* @param url API URL for job result
|
||||||
*/
|
*/
|
||||||
async function checkJobStatus(id: string) {
|
async function checkJobStatus(url: string) {
|
||||||
const res = await apiGetBase<APIJobResult>(`/api/extras/job-results/${id}/`);
|
const res = await apiGetBase<APIJobResult>(url);
|
||||||
if (hasError(res)) {
|
if (hasError(res)) {
|
||||||
// If the response is an API error, display an error message and stop checking for job status.
|
// If the response is an API error, display an error message and stop checking for job status.
|
||||||
const toast = createToast('danger', 'Error', res.error);
|
const toast = createToast('danger', 'Error', res.error);
|
||||||
@ -82,17 +83,17 @@ async function checkJobStatus(id: string) {
|
|||||||
if (timeout < 10000) {
|
if (timeout < 10000) {
|
||||||
timeout += 1000;
|
timeout += 1000;
|
||||||
}
|
}
|
||||||
await Promise.all([checkJobStatus(id), asyncTimeout(timeout)]);
|
await Promise.all([checkJobStatus(url), asyncTimeout(timeout)]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function initJobs() {
|
function initJobs() {
|
||||||
const { id, complete } = getJobInfo();
|
const { url, complete } = getJobInfo();
|
||||||
|
|
||||||
if (id !== null && !complete) {
|
if (url !== null && !complete) {
|
||||||
// If there is a job ID and it is not completed, check for the job's status.
|
// If there is a job ID and it is not completed, check for the job's status.
|
||||||
Promise.resolve(checkJobStatus(id));
|
Promise.resolve(checkJobStatus(url));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +96,6 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block data %}
|
{% block data %}
|
||||||
<span data-job-id="{{ result.pk }}"></span>
|
<span data-job-url="{% url 'extras-api:jobresult-detail' pk=result.pk %}"></span>
|
||||||
<span data-job-complete="{{ result.completed }}"></span>
|
<span data-job-complete="{{ result.completed }}"></span>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -112,6 +112,6 @@
|
|||||||
{% endblock content-wrapper %}
|
{% endblock content-wrapper %}
|
||||||
|
|
||||||
{% block data %}
|
{% block data %}
|
||||||
<span data-job-id="{{ result.pk }}"></span>
|
<span data-job-url="{% url 'extras-api:jobresult-detail' pk=result.pk %}"></span>
|
||||||
<span data-job-complete="{{ result.completed }}"></span>
|
<span data-job-complete="{{ result.completed }}"></span>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Reference in New Issue
Block a user