@acurast/cli
Version:
A cli to interact with the Acurast Cloud.
125 lines • 6.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import '@polkadot/api-augment';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { uploadScript } from './uploadToIpfs.js';
import { RestartPolicy, } from '../types.js';
import { DeploymentStatus } from './types.js';
import { deployJob } from './deployJob.js';
import { getWallet } from '../util/getWallet.js';
import { setEnvVars } from '../util/setEnvVars.js';
import { filelogger } from '../util/fileLogger.js';
import { zipFolder } from '../util/zipFolder.js';
import { createManifest } from '../util/createManifest.js';
import { checkIsFolder } from '../util/checkIsFolder.js';
import { basename } from 'node:path';
const BUNDLE_FOLDER = '.acurast/bundles';
export const createJob = (config, job, rpc, envVars, onlyUpload, statusCallback) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const wsProvider = new WsProvider(rpc);
const api = yield ApiPromise.create({
provider: wsProvider,
noInitWarn: true,
});
const wallet = yield getWallet();
let ipfsHash;
if (config.fileUrl.startsWith('ipfs://')) {
ipfsHash = config.fileUrl;
filelogger.debug(`config.fileUrl is an IPFS hash, so we this: ${ipfsHash}`);
}
else {
filelogger.debug(`config.fileUrl is not an IPFS hash, so we zip it: ${config.fileUrl}`);
// Check if the fileUrl is a folder
const isFolder = yield checkIsFolder(config.fileUrl);
if (isFolder) {
if (!config.entrypoint) {
filelogger.error('entrypoint is required for folders');
throw new Error('entrypoint is required for folders');
}
filelogger.debug(`config.fileUrl is a folder, so we use the entrypoint: ${config.entrypoint}`);
}
let { zipPath } = yield zipFolder(config.fileUrl, BUNDLE_FOLDER, createManifest(config.projectName, (_a = config.entrypoint) !== null && _a !== void 0 ? _a : basename(config.fileUrl), (_b = config.restartPolicy) !== null && _b !== void 0 ? _b : RestartPolicy.OnFailure), config.projectName);
filelogger.log(`zipPath ${zipPath}`);
ipfsHash = yield uploadScript({ file: zipPath });
filelogger.debug(`ipfsHash: ${ipfsHash}`);
}
statusCallback(DeploymentStatus.Uploaded, { ipfsHash });
config.fileUrl = ipfsHash;
job.script = ipfsHash;
statusCallback(DeploymentStatus.Prepared, { job });
let envHasBeenSet = false;
const TWO_MINUTES = 2 * 60 * 1000;
let timeout;
if (!onlyUpload) {
let jobId;
// We want to wrap the status callback to set the environment variables
const statusCallbackWrapper = (status, data) => {
if (status === DeploymentStatus.WaitingForMatch) {
jobId = data.jobIds[0];
if (jobId) {
filelogger.setJobId(jobId);
}
}
if (status === DeploymentStatus.Acknowledged) {
if (envHasBeenSet) {
filelogger.log('Setting Environment Variables: Env has been set, but new acks have been received.');
return statusCallback(status, data);
}
const timeToJobStart = job.schedule.startTime - Date.now();
const setEnv = () => __awaiter(void 0, void 0, void 0, function* () {
envHasBeenSet = true;
if (timeout) {
clearTimeout(timeout);
}
timeout = undefined;
filelogger.debug('Setting Environment Variables: Preparing transaction');
if (!jobId) {
filelogger.error('Setting Environment Variables: JobId not set');
throw new Error('DeploymentId not set');
}
const envs = yield setEnvVars({
id: jobId,
registration: job,
envVars,
});
filelogger.debug(`Setting Environment Variables: Done ${envs.hash ? `(hash: ${envs.hash})` : ''}`);
statusCallback(DeploymentStatus.EnvironmentVariablesSet, envs);
});
if (data.acknowledged >= config.numberOfReplicas) {
// Now we can set env vars
filelogger.debug('Setting Environment Variables: Have all acknowledgements, so we can set the env vars now.');
setEnv();
}
else {
// If start time is within specified timeframe, set env vars now, regardless if all acks are here.
if (timeToJobStart <= TWO_MINUTES) {
filelogger.debug('Setting Environment Variables: Start is scheduled within 2 minutes, so we do it now.');
setEnv();
}
else {
// If start time is in the future, set timeout to 2 mins before start time
if (!timeout) {
filelogger.debug(`Setting Environment Variables: Start is in the future, timeout will trigger in ${timeToJobStart - TWO_MINUTES}ms, 2 minutes before start time.`);
timeout = setTimeout(() => {
filelogger.debug('Setting Environment Variables: Was in the future, timeout was awaited, now it will be set.');
setEnv();
}, timeToJobStart - TWO_MINUTES);
}
}
}
}
statusCallback(status, data);
};
const result = yield deployJob(api, wallet, job, statusCallbackWrapper);
statusCallback(DeploymentStatus.Submit, { txHash: result });
}
return job;
});
//# sourceMappingURL=createJob.js.map