/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { getLogger, Logger } from '@squiz/dx-logger-lib'; import { uploadFile } from '@squiz/virus-scanner-lib'; import { AxiosInstance } from 'axios'; import color from 'cli-color'; import fsp from 'fs/promises'; import path from 'path'; import { cleanupTmpDir, getAndValidateManifest, initializeUpload, pollForResult, watchAndWaitForUploadAndScanComplete, } from './utils'; /** * Service for handling job folder uploads */ export class JobUploadService { private logger: Logger; constructor() { this.logger = getLogger({ name: 'upload-job', format: 'human' }); } /** * Upload a job folder to the job runner service * @param apiClient - Axios client for API calls * @param folderPath - Path to the job folder to upload * @param baseTempDir - Base temporary directory for upload processing */ public async uploadJobFolder(apiClient: AxiosInstance, folderPath: string, baseTempDir: string = ''): Promise { const tmpDir = await fsp.mkdtemp(path.resolve(baseTempDir, 'job-upload')); try { const manifest = await getAndValidateManifest(apiClient, folderPath); const initialUpload = await initializeUpload(apiClient, folderPath, tmpDir, this.logger); await uploadFile(initialUpload.upload, initialUpload.zip); await watchAndWaitForUploadAndScanComplete(apiClient, '/upload-job/status/', initialUpload.upload.id); this.logger.info(`deployment id: ${initialUpload.upload.id} status: deploying job folder`); const result = await pollForResult(apiClient, initialUpload.upload.id, manifest.getModel(), this.logger); await cleanupTmpDir(tmpDir, this.logger); if (result.status === 'successful') { this.logger.info(`deployment id: ${initialUpload.upload.id} status: ${color.green('success')}`); } else { const message = result?.message ?? 'unknown'; this.logger.error(`failed due an unexpected reason: ${message}`); await cleanupTmpDir(tmpDir, this.logger); } } catch (e) { await cleanupTmpDir(tmpDir, this.logger); throw e; } } }