From 55956a117f4ac6b3f740ee48c0442460f463ddac Mon Sep 17 00:00:00 2001 From: Moritz Kirstein Date: Fri, 15 Mar 2024 13:34:22 +0100 Subject: [PATCH] feat: add compute stop (#72) * feat: add compute stop * refactor: use stopCompute instead of computeStop --- src/@types/Compute.ts | 7 +++++++ src/Nautilus/Nautilus.ts | 22 +++++++++++++++------- src/compute/index.ts | 12 ++++++++++-- src/utils/provider.ts | 20 ++++++++++++++++++++ 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/@types/Compute.ts b/src/@types/Compute.ts index 308f941..ff7dd92 100644 --- a/src/@types/Compute.ts +++ b/src/@types/Compute.ts @@ -33,6 +33,13 @@ export interface ComputeResultConfig extends ComputeStatusConfig { resultIndex?: number } +export interface StopComputeConfig { + did: string + jobId: string + providerUri: string + signer: Signer +} + export interface TokenInfo { address: string name: string diff --git a/src/Nautilus/Nautilus.ts b/src/Nautilus/Nautilus.ts index 74b2b71..2a71c22 100644 --- a/src/Nautilus/Nautilus.ts +++ b/src/Nautilus/Nautilus.ts @@ -1,3 +1,4 @@ +import { TransactionReceipt } from '@ethersproject/abstract-provider' import { Asset, Config, @@ -14,22 +15,22 @@ import { ComputeStatusConfig, CreateAssetConfig, LifecycleStates, - PublishResponse + PublishResponse, + StopComputeConfig } from '../@types' +import { access } from '../access' +import { compute, getStatus, retrieveResult, stopCompute } from '../compute' import { createAsset, createServiceWithDatatokenAndPricing, publishDDO } from '../publish' import { getAllPromisesOnArray } from '../utils' -import { NautilusAsset } from './Asset/NautilusAsset' -import { access } from '../access' -import { compute, getStatus, retrieveResult } from '../compute' -import { FileTypes, NautilusService, ServiceTypes } from './Asset' -import { TransactionReceipt } from '@ethersproject/abstract-provider' -import { resolvePublisherTrustedAlgorithms } from '../utils/helpers/trusted-algorithms' import { getAsset, getAssets } from '../utils/aquarius' import { editPrice } from '../utils/contracts' +import { resolvePublisherTrustedAlgorithms } from '../utils/helpers/trusted-algorithms' +import { FileTypes, NautilusService, ServiceTypes } from './Asset' +import { NautilusAsset } from './Asset/NautilusAsset' export { LogLevel } from '@oceanprotocol/lib' @@ -345,5 +346,12 @@ export class Nautilus { signer: this.signer }) } + + async stopCompute(stopComputeConfig: Omit) { + return await stopCompute({ + ...stopComputeConfig, + signer: this.signer + }) + } // #endregion } diff --git a/src/compute/index.ts b/src/compute/index.ts index 323bb12..02ec2d4 100644 --- a/src/compute/index.ts +++ b/src/compute/index.ts @@ -15,7 +15,8 @@ import { ComputeConfig, ComputeResultConfig, ComputeStatusConfig, - OrderPriceAndFees + OrderPriceAndFees, + StopComputeConfig } from '../@types/Compute' import { getDatatokenBalance, getServiceByName } from '../utils' import { @@ -26,7 +27,8 @@ import { isOrderable, order, reuseOrder } from '../utils/order' import { approveProviderFee, initializeProviderForCompute, - startComputeJob + startComputeJob, + stopComputeJob } from '../utils/provider' export async function compute(computeConfig: ComputeConfig) { @@ -403,3 +405,9 @@ export async function handleComputeOrder( LoggerInstance.error(`[compute] ${error.message}`) } } + +export async function stopCompute(stopComputeConfig: StopComputeConfig) { + const { did, jobId, providerUri, signer } = stopComputeConfig + + return await stopComputeJob(providerUri, did, jobId, signer) +} diff --git a/src/utils/provider.ts b/src/utils/provider.ts index 1be82b5..5b33131 100644 --- a/src/utils/provider.ts +++ b/src/utils/provider.ts @@ -153,6 +153,26 @@ export async function startComputeJob( } } +export async function stopComputeJob( + providerUri: string, + did: string, + jobId: string, + signer: Signer +): Promise { + try { + return await ProviderInstance.computeStop( + did, + await signer.getAddress(), + jobId, + providerUri, + signer + ) + } catch (error) { + LoggerInstance.error('Error stopping compute job!') + LoggerInstance.error(error) + } +} + export function getValidUntilTime( computeEnvMaxJobDuration: number, datasetTimeout?: number,