2020-01-10 20:25:14 +01:00
|
|
|
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
|
|
|
import { MetaData } from '../ddo/MetaData'
|
|
|
|
import Account from './Account'
|
|
|
|
import { DDO } from '../ddo/DDO'
|
2020-01-27 19:14:12 +01:00
|
|
|
import { SubscribablePromise } from '../utils'
|
|
|
|
import { OrderProgressStep } from './utils/ServiceUtils'
|
2020-01-29 12:32:24 +01:00
|
|
|
import { DID } from '../squid'
|
2020-01-10 20:25:14 +01:00
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
export enum ComputeJobStatus {
|
|
|
|
Started,
|
|
|
|
ConfiguringVolumes,
|
|
|
|
RunningAlgorithm,
|
|
|
|
FilteringResults,
|
|
|
|
PublishingResult,
|
|
|
|
Completed,
|
|
|
|
Stopped,
|
|
|
|
Deleted
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:11:06 +01:00
|
|
|
export interface Output {
|
|
|
|
publishAlgorithmLog?: boolean
|
|
|
|
publishOutput?: boolean
|
|
|
|
brizoAddress?: string
|
2020-02-01 15:21:59 +01:00
|
|
|
brizoUri?: string
|
2020-02-01 19:57:20 +01:00
|
|
|
metadata?: MetaData
|
2020-02-01 15:21:59 +01:00
|
|
|
metadataUri?: string
|
2020-02-01 15:11:06 +01:00
|
|
|
nodeUri?: string
|
|
|
|
owner?: string
|
2020-02-01 15:21:59 +01:00
|
|
|
secretStoreUri?: string
|
2020-02-01 19:57:34 +01:00
|
|
|
whitelist?: string[]
|
2020-02-01 15:11:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
export interface ComputeJob {
|
2020-01-21 12:45:50 +01:00
|
|
|
owner: string
|
|
|
|
agreementId: string
|
|
|
|
jobId: string
|
|
|
|
dateCreated: string
|
|
|
|
dateFinished: string
|
2020-01-28 18:00:06 +01:00
|
|
|
status: ComputeJobStatus
|
2020-01-21 12:45:50 +01:00
|
|
|
statusText: string
|
|
|
|
algologUrl: string
|
|
|
|
outputsUrl: string[]
|
2020-01-29 12:32:24 +01:00
|
|
|
resultsDid?: DID
|
2020-01-21 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 20:25:14 +01:00
|
|
|
/**
|
|
|
|
* Compute submodule of Ocean Protocol.
|
|
|
|
*/
|
|
|
|
export class OceanCompute extends Instantiable {
|
|
|
|
/**
|
|
|
|
* Returns the instance of OceanCompute.
|
|
|
|
* @return {Promise<OceanCompute>}
|
|
|
|
*/
|
|
|
|
public static async getInstance(config: InstantiableConfig): Promise<OceanCompute> {
|
|
|
|
const instance = new OceanCompute()
|
|
|
|
instance.setInstanceConfig(config)
|
|
|
|
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
2020-01-21 12:45:50 +01:00
|
|
|
/**
|
|
|
|
* Starts an order of a compute service that is defined in an asset's services.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on.
|
2020-01-22 11:16:44 +01:00
|
|
|
* @return {Promise<string>} Returns the Service Agreement ID, representation of `bytes32` ID.
|
2020-01-21 12:45:50 +01:00
|
|
|
*/
|
2020-01-22 11:16:44 +01:00
|
|
|
public order(
|
|
|
|
consumerAccount: Account,
|
2020-01-23 10:33:02 +01:00
|
|
|
datasetDid: string,
|
|
|
|
provider?: string
|
2020-01-22 11:16:44 +01:00
|
|
|
): SubscribablePromise<OrderProgressStep, string> {
|
|
|
|
return new SubscribablePromise(async observer => {
|
2020-01-27 19:14:12 +01:00
|
|
|
const { assets, keeper, utils } = this.ocean
|
2020-01-22 11:16:44 +01:00
|
|
|
const ddo: DDO = await assets.resolve(datasetDid)
|
2020-01-27 19:14:12 +01:00
|
|
|
const condition = keeper.conditions.computeExecutionCondition
|
2020-01-22 11:16:44 +01:00
|
|
|
|
2020-01-27 19:14:12 +01:00
|
|
|
const agreementId = await utils.services.order(
|
|
|
|
'compute',
|
|
|
|
condition,
|
|
|
|
observer,
|
2020-01-22 11:16:44 +01:00
|
|
|
consumerAccount,
|
2020-01-27 19:14:12 +01:00
|
|
|
ddo,
|
|
|
|
provider
|
2020-01-22 11:16:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return agreementId
|
|
|
|
})
|
2020-01-21 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 14:25:24 +01:00
|
|
|
/**
|
|
|
|
* Check the output object and add default properties if needed
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-02-03 10:32:27 +01:00
|
|
|
* @param {Output} output Output section used for publishing the result.
|
2020-02-01 15:11:06 +01:00
|
|
|
* @return {Promise<Output>} Returns output object
|
2020-02-01 14:25:24 +01:00
|
|
|
*/
|
2020-02-03 11:46:48 +01:00
|
|
|
public checkOutput(consumerAccount: Account, output?: Output): Output {
|
2020-02-01 19:59:50 +01:00
|
|
|
const isDefault =
|
|
|
|
!output || (!output.publishAlgorithmLog && !output.publishOutput)
|
2020-02-02 07:32:53 +01:00
|
|
|
|
2020-02-01 19:59:50 +01:00
|
|
|
if (isDefault) {
|
|
|
|
return {
|
|
|
|
publishAlgorithmLog: false,
|
|
|
|
publishOutput: false
|
|
|
|
}
|
2020-02-01 14:25:24 +01:00
|
|
|
}
|
2020-02-02 07:32:53 +01:00
|
|
|
|
2020-02-01 19:59:50 +01:00
|
|
|
return {
|
|
|
|
publishAlgorithmLog: output.publishAlgorithmLog,
|
|
|
|
publishOutput: output.publishOutput,
|
|
|
|
brizoAddress: output.brizoAddress || this.config.brizoAddress,
|
|
|
|
brizoUri: output.brizoUri || this.config.brizoUri,
|
|
|
|
metadataUri: output.metadataUri || this.config.aquariusUri,
|
|
|
|
nodeUri: output.nodeUri || this.config.nodeUri,
|
|
|
|
owner: output.owner || consumerAccount.getId(),
|
|
|
|
secretStoreUri: output.secretStoreUri || this.config.secretStoreUri
|
2020-02-01 14:25:24 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 07:32:53 +01:00
|
|
|
|
2020-01-10 20:25:14 +01:00
|
|
|
/**
|
|
|
|
* Start the execution of a compute job.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} agreementId The service agreement ID.
|
2020-01-10 20:25:14 +01:00
|
|
|
* @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.
|
2020-02-03 10:32:41 +01:00
|
|
|
* @param {Output} output Define algorithm output publishing. Publishing the result of a compute job is turned off by default.
|
2020-01-28 18:00:06 +01:00
|
|
|
* @return {Promise<ComputeJob>} Returns compute job ID under status.jobId
|
2020-01-10 20:25:14 +01:00
|
|
|
*/
|
2020-01-21 12:45:50 +01:00
|
|
|
public async start(
|
2020-01-10 20:25:14 +01:00
|
|
|
consumerAccount: Account,
|
2020-01-21 15:38:19 +01:00
|
|
|
agreementId: string,
|
2020-01-21 12:45:50 +01:00
|
|
|
algorithmDid?: string,
|
2020-02-01 14:25:24 +01:00
|
|
|
algorithmMeta?: MetaData,
|
2020-02-01 15:11:06 +01:00
|
|
|
output?: Output
|
2020-01-28 18:00:06 +01:00
|
|
|
): Promise<ComputeJob> {
|
2020-02-01 22:34:14 +01:00
|
|
|
output = this.checkOutput(consumerAccount, output)
|
2020-01-22 10:01:04 +01:00
|
|
|
const status = await this.ocean.brizo.compute(
|
2020-01-21 12:45:50 +01:00
|
|
|
'post',
|
2020-01-10 20:25:14 +01:00
|
|
|
agreementId,
|
|
|
|
consumerAccount,
|
|
|
|
algorithmDid,
|
2020-02-01 14:25:24 +01:00
|
|
|
algorithmMeta,
|
2020-02-03 19:02:42 +01:00
|
|
|
undefined,
|
2020-02-01 14:25:24 +01:00
|
|
|
output
|
2020-01-10 20:25:14 +01:00
|
|
|
)
|
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
return status as ComputeJob
|
2020-01-10 20:25:14 +01:00
|
|
|
}
|
2020-01-21 12:45:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends a running compute job.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} agreementId The service agreement ID.
|
2020-01-21 12:45:50 +01:00
|
|
|
* @param {string} jobId The ID of the compute job to be stopped
|
2020-01-28 18:00:06 +01:00
|
|
|
* @return {Promise<ComputeJob>} Returns the new status of a job
|
2020-01-21 12:45:50 +01:00
|
|
|
*/
|
|
|
|
public async stop(
|
|
|
|
consumerAccount: Account,
|
2020-01-21 15:38:19 +01:00
|
|
|
agreementId: string,
|
2020-01-21 12:45:50 +01:00
|
|
|
jobId: string
|
2020-01-28 18:00:06 +01:00
|
|
|
): Promise<ComputeJob> {
|
2020-01-22 10:01:04 +01:00
|
|
|
const status = await this.ocean.brizo.compute(
|
2020-01-21 12:45:50 +01:00
|
|
|
'put',
|
|
|
|
agreementId,
|
|
|
|
consumerAccount,
|
2020-02-03 19:02:42 +01:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2020-01-21 12:45:50 +01:00
|
|
|
jobId
|
|
|
|
)
|
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
return status as ComputeJob
|
2020-01-21 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a compute job and all resources associated with the job. If job is running it will be stopped first.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} agreementId The service agreement ID.
|
2020-01-21 12:45:50 +01:00
|
|
|
* @param {string} jobId The ID of the compute job to be stopped
|
2020-01-28 18:00:06 +01:00
|
|
|
* @return {Promise<ComputeJob>} Returns the new status of a job
|
2020-01-21 12:45:50 +01:00
|
|
|
*/
|
|
|
|
public async delete(
|
|
|
|
consumerAccount: Account,
|
2020-01-21 15:38:19 +01:00
|
|
|
agreementId: string,
|
2020-01-21 12:45:50 +01:00
|
|
|
jobId: string
|
2020-01-28 18:00:06 +01:00
|
|
|
): Promise<ComputeJob> {
|
2020-01-22 10:01:04 +01:00
|
|
|
const status = await this.ocean.brizo.compute(
|
2020-01-21 12:45:50 +01:00
|
|
|
'delete',
|
|
|
|
agreementId,
|
|
|
|
consumerAccount,
|
2020-02-03 19:02:42 +01:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2020-01-21 12:45:50 +01:00
|
|
|
jobId
|
|
|
|
)
|
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
return status as ComputeJob
|
2020-01-21 12:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends a running compute job and starts it again.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} agreementId The service agreement ID.
|
2020-01-21 12:45:50 +01:00
|
|
|
* @param {string} jobId The ID of the compute job to be stopped
|
2020-01-28 18:00:06 +01:00
|
|
|
* @return {Promise<ComputeJob>} Returns the new status of a job
|
2020-01-21 12:45:50 +01:00
|
|
|
*/
|
|
|
|
public async restart(
|
2020-01-21 15:38:19 +01:00
|
|
|
consumerAccount: Account,
|
2020-01-21 12:45:50 +01:00
|
|
|
agreementId: string,
|
2020-01-21 15:38:19 +01:00
|
|
|
jobId: string
|
2020-01-28 18:00:06 +01:00
|
|
|
): Promise<ComputeJob> {
|
2020-01-21 15:38:19 +01:00
|
|
|
await this.stop(consumerAccount, agreementId, jobId)
|
|
|
|
const result = await this.start(consumerAccount, agreementId, jobId)
|
2020-01-21 12:45:50 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns information about the status of all compute jobs, or a single compute job.
|
|
|
|
* @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
|
2020-01-28 18:00:06 +01:00
|
|
|
* @return {Promise<ComputeJob[]>} Returns the status
|
2020-01-21 12:45:50 +01:00
|
|
|
*/
|
|
|
|
public async status(
|
|
|
|
consumerAccount: Account,
|
|
|
|
agreementId?: string,
|
|
|
|
jobId?: string
|
2020-01-28 18:00:06 +01:00
|
|
|
): Promise<ComputeJob[]> {
|
2020-01-22 10:01:04 +01:00
|
|
|
const status = await this.ocean.brizo.compute(
|
2020-01-21 12:45:50 +01:00
|
|
|
'get',
|
|
|
|
agreementId,
|
|
|
|
consumerAccount,
|
2020-02-03 19:02:42 +01:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2020-01-21 12:45:50 +01:00
|
|
|
jobId
|
|
|
|
)
|
|
|
|
|
2020-01-28 18:00:06 +01:00
|
|
|
return status as ComputeJob[]
|
2020-01-21 12:45:50 +01:00
|
|
|
}
|
2020-01-21 15:13:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the final result of a specific compute job published as an asset.
|
|
|
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
2020-01-21 15:38:19 +01:00
|
|
|
* @param {string} agreementId The service agreement ID.
|
2020-01-21 15:13:02 +01:00
|
|
|
* @param {string} jobId The ID of the compute job to be stopped.
|
|
|
|
* @return {Promise<DDO>} Returns the DDO of the result asset.
|
|
|
|
*/
|
|
|
|
public async result(
|
|
|
|
consumerAccount: Account,
|
|
|
|
agreementId: string,
|
|
|
|
jobId: string
|
|
|
|
): Promise<DDO> {
|
2020-01-22 10:01:04 +01:00
|
|
|
const status = await this.ocean.brizo.compute(
|
2020-01-21 15:13:02 +01:00
|
|
|
'get',
|
|
|
|
agreementId,
|
|
|
|
consumerAccount,
|
2020-02-03 19:02:42 +01:00
|
|
|
undefined,
|
|
|
|
undefined,
|
2020-01-21 15:13:02 +01:00
|
|
|
jobId
|
|
|
|
)
|
|
|
|
|
|
|
|
return status[0].ddo ? status[0].ddo : null
|
|
|
|
}
|
2020-01-10 20:25:14 +01:00
|
|
|
}
|