public async createJobIfNotExists()

in packages/azure-services/src/azure-batch/batch.ts [108:140]


    public async createJobIfNotExists(jobId: string, addJobIdIndexOnCreate: boolean = false): Promise<string> {
        let serviceJobId = jobId;
        const client = await this.batchClientProvider();

        try {
            const cloudJob = await client.job.get(serviceJobId);
            if (cloudJob.state !== 'active') {
                throw new VError(`The job ${serviceJobId} is not active and cannot be used to run new tasks.`);
            }
        } catch (error) {
            if ((<BatchServiceModels.BatchError>(<unknown>error)).code === 'JobNotFound') {
                if (addJobIdIndexOnCreate) {
                    serviceJobId = `${jobId}_${crypto.randomBytes(5).toString('hex')}`;
                }

                const jobAddParameter: BatchServiceModels.JobAddParameter = {
                    id: serviceJobId,
                    poolInfo: {
                        poolId: this.config.poolId,
                    },
                    onAllTasksComplete: 'terminatejob',
                };

                await client.job.add(jobAddParameter);

                this.logger.logInfo(`New batch job created successfully.`, { batchJobId: serviceJobId });
            } else {
                throw new VError(error as Error, `An error occurred while retrieving state of ${jobId} job.`);
            }
        }

        return serviceJobId;
    }