///////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2021, Open Design Alliance (the "Alliance").
// All rights reserved.
//
// This software and its documentation and related materials are owned by
// the Alliance. The software may only be incorporated into application
// programs owned by members of the Alliance, subject to a signed
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable
// trade secrets of the Alliance and its suppliers. The software is also
// protected by copyright law and international treaty provisions. Application
// programs incorporating this software must include the following statement
// with their copyright notices:
//
// This application incorporates Open Design Alliance software pursuant to a
// license agreement with Open Design Alliance.
// Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
// All rights reserved.
//
// By use of this software, its documentation or related materials, you
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
import { IHttpClient } from "./IHttpClient";
import { json, waitFor } from "./impl/Utils";
/**
* The class representing a `job` entity describes the process of converting a file from one
* format to another, to obtain geometric data, validate the file, or run a clash test.
*/
export class Job {
private _data: any;
protected httpClient: IHttpClient;
/**
* @param data - An object that implements job data storage.
* @param httpClient - Http client.
*/
constructor(data: any, httpClient: IHttpClient) {
this.httpClient = httpClient;
this.data = data;
}
protected internalGet() {
return this.httpClient.get(`/jobs/${this.data.id}`);
}
protected internalPut(body?: ArrayBuffer | Blob | globalThis.File | FormData | object | string | null) {
return this.httpClient.put(`/jobs/${this.data.id}`, body);
}
protected internalDelete() {
return this.httpClient.delete(`/jobs/${this.data.id}`);
}
/**
* The ID of the assembly the job is working on (internal).
*
* @readonly
*/
get assemblyId(): string {
return this.data.assemblyId;
}
/**
* Job creator ID. Use {@link Client#getUser | Client.getUser()} to obtain detailed creator information.
*
* @readonly
*/
get authorId(): string {
return this.data.authorId;
}
/**
* Job creation time (UTC) in the format specified in ISO 8601.
*
* @readonly
*/
get createdAt(): string {
return this.data.createdAt;
}
/**
* Raw job data received from the server.
*
* @readonly
*/
get data(): any {
return this._data;
}
private set data(value: any) {
this._data = value;
}
/**
* `true` if job is `done` or `failed`. See {@link Job#status | status} for more details.
*
* @readonly
*/
get done(): boolean {
return this.data.status === "done" || this.data.status === "failed";
}
/**
* The ID of the file the job is working on.
*
* @readonly
*/
get fileId(): string {
return this.data.fileId;
}
/**
* Unique job ID.
*
* @readonly
*/
get id(): string {
return this.data.id;
}
/**
* Job last update (UTC) time in the format specified in ISO 8601.
*
* @readonly
*/
get lastUpdate(): string {
return this.data.lastUpdate;
}
/**
* Job type. Can be `properties`, `geomerty`, `geomertyGltf`, `validation`, `clash`, `dwg`,
* `obj`, `gltf`, `glb`, `vsf`, `pdf` or `3dpdf`.
*
* @readonly
*/
get outputFormat(): string {
return this.data.outputFormat;
}
/**
* Parameters for the job runner.
*
* @readonly
*/
get parameters(): any {
return this.data.parameters;
}
/**
* Job status. Can be `waiting`, `inprogress`, `done` or `failed`.
*
* @readonly
*/
get status(): string {
return this.data.status;
}
/**
* Job status description message.
*
* @readonly
*/
get statusMessage(): string {
return this.data.statusMessage;
}
/**
* Job starting time (UTC) in the format specified in ISO 8601.
*
* @readonly
*/
get startedAt(): string {
return this.data.startedAt;
}
/**
* Refresh job data.
*
* @async
*/
async checkout(): Promise {
this.data = await json(this.internalGet());
return this;
}
/**
* Update job data on the server. Only admins can update job data.
*
* @async
* @param data - Raw job data.
*/
async update(data: any): Promise {
this.data = await json(this.internalPut(data));
return this;
}
/**
* Remove a job from the server job list. Jobs that are in progress or have already been
* completed cannot be deleted.
*
* @async
* @returns Returns the raw data of a deleted job.
*/
delete(): Promise {
return json(this.internalDelete());
}
// /**
// * Save job data changes to the server. Call this method to update job data on the server
// * after any changes.
// *
// * @async
// * @returns {Promise}
// */
// save() {
// return this.update(this.data);
// }
/**
* Wait for job to be done. Job is done when it changes to `done` or `failed` status.
*
* @async
* @param params - An object containing waiting parameters.
* @param params.timeout - The time, in milliseconds that the function should wait job. If
* jobs is not done during this time, the `TimeoutError` exception will be thrown.
* @param params.interval - The time, in milliseconds, the function should delay in between
* checking job status.
* @param params.signal- An AbortController
* signal object instance, which can be used to abort waiting as desired.
* @param params.onCheckout - Waiting progress callback. Return `true` to cancel waiting.
*/
waitForDone(params?: {
timeout?: number;
interval?: number;
signal?: AbortSignal;
onCheckout?: (job: Job, ready: boolean) => boolean;
}): Promise {
const checkDone = () =>
this.checkout().then((job) => {
const ready = ["done", "failed"].includes(job.status);
const cancel = params?.onCheckout?.(job, ready);
return cancel || ready;
});
return waitFor(checkDone, params).then(() => this);
}
}