From f11eacaabdc15285ba78a8096e492fd8863c933a Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 28 Jan 2020 18:00:06 +0100 Subject: [PATCH] job status cleanup --- integration/ocean/Compute.test.ts | 4 +-- src/brizo/Brizo.ts | 3 ++- src/ocean/OceanCompute.ts | 43 +++++++++++++++++++------------ src/squid.ts | 1 + test/ocean/OceanCompute.test.ts | 34 ++++++++++++++++-------- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/integration/ocean/Compute.test.ts b/integration/ocean/Compute.test.ts index 0389617..54debfd 100644 --- a/integration/ocean/Compute.test.ts +++ b/integration/ocean/Compute.test.ts @@ -1,7 +1,7 @@ import { assert } from 'chai' import { config } from '../config' -import { Ocean, Account, DDO, MetaData } from '../../src' // @oceanprotocol/squid +import { Ocean, Account, DDO, MetaData, ComputeJobStatus } from '../../src' // @oceanprotocol/squid import { getMetadata, getComputeServiceExample } from '../utils' describe('Compute', () => { @@ -62,6 +62,6 @@ describe('Compute', () => { it('should start a compute job', async () => { const response = await ocean.compute.start(account, agreementId, ddoAlgorithm.id) - assert.equal(response.status, 1) + assert.equal(response.status, ComputeJobStatus.Started) }) }) diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index b77dda9..a6a3000 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -4,6 +4,7 @@ import { noZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' import { DDO } from '../ddo/DDO' import { ServiceType } from '../ddo/Service' +import { ComputeJob } from '../ocean/OceanCompute' const apiPath = '/api/v1/brizo/services' @@ -116,7 +117,7 @@ export class Brizo extends Instantiable { algorithmDid?: string, algorithmMeta?: MetaData, jobId?: string - ): Promise { + ): Promise { const signature = await this.createSignature(consumerAccount, serviceAgreementId) const address = consumerAccount.getId() const serviceEndpoint = await this.getEndpointFromAgreement( diff --git a/src/ocean/OceanCompute.ts b/src/ocean/OceanCompute.ts index e5f22f5..e9dbd30 100644 --- a/src/ocean/OceanCompute.ts +++ b/src/ocean/OceanCompute.ts @@ -5,13 +5,24 @@ import { DDO } from '../ddo/DDO' import { SubscribablePromise } from '../utils' import { OrderProgressStep } from './utils/ServiceUtils' -export interface ComputeJobStatus { +export enum ComputeJobStatus { + Started, + ConfiguringVolumes, + RunningAlgorithm, + FilteringResults, + PublishingResult, + Completed, + Stopped, + Deleted +} + +export interface ComputeJob { owner: string agreementId: string jobId: string dateCreated: string dateFinished: string - status: number + status: ComputeJobStatus statusText: string configlogUrl: string publishlogUrl: string @@ -71,14 +82,14 @@ export class OceanCompute extends Instantiable { * @param {string} agreementId The service agreement ID. * @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset. * @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified. - * @return {Promise} Returns compute job ID under status.jobId + * @return {Promise} Returns compute job ID under status.jobId */ public async start( consumerAccount: Account, agreementId: string, algorithmDid?: string, algorithmMeta?: MetaData - ): Promise { + ): Promise { const status = await this.ocean.brizo.compute( 'post', agreementId, @@ -87,7 +98,7 @@ export class OceanCompute extends Instantiable { algorithmMeta ) - return status + return status as ComputeJob } /** @@ -95,13 +106,13 @@ export class OceanCompute extends Instantiable { * @param {Account} consumerAccount The account of the consumer ordering the service. * @param {string} agreementId The service agreement ID. * @param {string} jobId The ID of the compute job to be stopped - * @return {Promise} Returns the new status of a job + * @return {Promise} Returns the new status of a job */ public async stop( consumerAccount: Account, agreementId: string, jobId: string - ): Promise { + ): Promise { const status = await this.ocean.brizo.compute( 'put', agreementId, @@ -109,7 +120,7 @@ export class OceanCompute extends Instantiable { jobId ) - return status + return status as ComputeJob } /** @@ -117,13 +128,13 @@ export class OceanCompute extends Instantiable { * @param {Account} consumerAccount The account of the consumer ordering the service. * @param {string} agreementId The service agreement ID. * @param {string} jobId The ID of the compute job to be stopped - * @return {Promise} Returns the new status of a job + * @return {Promise} Returns the new status of a job */ public async delete( consumerAccount: Account, agreementId: string, jobId: string - ): Promise { + ): Promise { const status = await this.ocean.brizo.compute( 'delete', agreementId, @@ -131,7 +142,7 @@ export class OceanCompute extends Instantiable { jobId ) - return status + return status as ComputeJob } /** @@ -139,13 +150,13 @@ export class OceanCompute extends Instantiable { * @param {Account} consumerAccount The account of the consumer ordering the service. * @param {string} agreementId The service agreement ID. * @param {string} jobId The ID of the compute job to be stopped - * @return {Promise} Returns the new status of a job + * @return {Promise} Returns the new status of a job */ public async restart( consumerAccount: Account, agreementId: string, jobId: string - ): Promise { + ): Promise { await this.stop(consumerAccount, agreementId, jobId) const result = await this.start(consumerAccount, agreementId, jobId) return result @@ -156,13 +167,13 @@ export class OceanCompute extends Instantiable { * @param {Account} consumerAccount The account of the consumer ordering the service. * @param {string} agreementId The service agreement ID. * @param {string} jobId The ID of the compute job to be stopped - * @return {Promise} Returns the status + * @return {Promise} Returns the status */ public async status( consumerAccount: Account, agreementId?: string, jobId?: string - ): Promise { + ): Promise { const status = await this.ocean.brizo.compute( 'get', agreementId, @@ -170,7 +181,7 @@ export class OceanCompute extends Instantiable { jobId ) - return status + return status as ComputeJob[] } /** diff --git a/src/squid.ts b/src/squid.ts index b5ff8ef..1eb831f 100644 --- a/src/squid.ts +++ b/src/squid.ts @@ -15,6 +15,7 @@ export * from './ddo/DDO' export * from './ddo/MetaData' export { CreateProgressStep } from './ocean/OceanAssets' +export { ComputeJob, ComputeJobStatus } from './ocean/OceanCompute' export { OrderProgressStep } from './ocean/utils/ServiceUtils' export { OceanPlatformTechStatus, diff --git a/test/ocean/OceanCompute.test.ts b/test/ocean/OceanCompute.test.ts index ebcebfe..eb29438 100644 --- a/test/ocean/OceanCompute.test.ts +++ b/test/ocean/OceanCompute.test.ts @@ -4,7 +4,7 @@ import spies from 'chai-spies' import { Ocean } from '../../src/ocean/Ocean' import config from '../config' import { Account } from '../../src/squid' -import { OceanCompute } from '../../src/ocean/OceanCompute' +import { OceanCompute, ComputeJobStatus } from '../../src/ocean/OceanCompute' use(spies) @@ -39,10 +39,12 @@ describe('OceanCompute', () => { describe('#stop()', () => { it('should stop a job', async () => { - spy.on(ocean.utils.fetch, 'put', () => responsify({ status: 6 })) + spy.on(ocean.utils.fetch, 'put', () => + responsify({ status: ComputeJobStatus.Completed }) + ) const response = await compute.stop(account, 'xxx', 'xxx') - assert(response.status === 6) + assert(response.status === ComputeJobStatus.Completed) }) }) @@ -58,40 +60,50 @@ describe('OceanCompute', () => { describe('#delete()', () => { it('should delete a job', async () => { - spy.on(ocean.utils.fetch, 'delete', () => responsify({ status: 8 })) + spy.on(ocean.utils.fetch, 'delete', () => + responsify({ status: ComputeJobStatus.Deleted }) + ) const response = await compute.delete(account, 'xxx', 'xxx') - assert(response.status === 8) + assert(response.status === ComputeJobStatus.Deleted) }) }) describe('#status()', () => { it('should get the status of one job', async () => { - spy.on(ocean.utils.fetch, 'get', () => responsify([{ status: 1 }])) + spy.on(ocean.utils.fetch, 'get', () => + responsify([{ status: ComputeJobStatus.Started }]) + ) const response = await compute.status(account, 'xxx', 'xxx') assert(response.length === 1) - assert(response[0].status === 1) + assert(response[0].status === ComputeJobStatus.Started) }) it('should get the status of multiple jobs', async () => { spy.on(ocean.utils.fetch, 'get', () => - responsify([{ status: 1 }, { status: 1 }]) + responsify([ + { status: ComputeJobStatus.Started }, + { status: ComputeJobStatus.Started } + ]) ) const response = await compute.status(account, 'xxx') assert(response.length === 2) - assert(response[0].status === 1) + assert(response[0].status === ComputeJobStatus.Started) }) it('should get all jobs for one owner', async () => { spy.on(ocean.utils.fetch, 'get', () => - responsify([{ status: 1 }, { status: 1 }]) + responsify([ + { status: ComputeJobStatus.Started }, + { status: ComputeJobStatus.Started } + ]) ) const response = await compute.status(account) assert(response.length === 2) - assert(response[0].status === 1) + assert(response[0].status === ComputeJobStatus.Started) }) }) })