Created
February 11, 2021 16:13
-
-
Save urbanisierung/a5a758dc495becc225b0332a4b2857c0 to your computer and use it in GitHub Desktop.
bucket.worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class BucketWorker { | |
constructor(private zeebeController: ZeebeController) {} | |
public create() { | |
this.zeebeController.getZeebeClient().createWorker({ | |
taskType: Worker.AWS_BUCKET_UPLOAD, | |
taskHandler: async (job: any, complete: any, worker: any) => { | |
const jobId = job.variables.jobId; | |
if (!jobId) { | |
complete.failure('Job Id not found on process context: <jobId>'); | |
return; | |
} | |
const screenshotController = new ScreenshotController({ | |
projectId: ConfigController.get( | |
ConfigParameter.WEBSITESHOT_PROJECT_ID | |
), | |
apikey: ConfigController.get(ConfigParameter.WEBSITESHOT_API_KEY), | |
}); | |
const bucketController = new BucketController( | |
{ | |
id: ConfigController.get(ConfigParameter.AWS_SECRET_ID), | |
secret: ConfigController.get(ConfigParameter.AWS_SECRET_KEY), | |
}, | |
ConfigController.get(ConfigParameter.AWS_BUCKET) | |
); | |
try { | |
const getResponse: GetResponse = await screenshotController.get( | |
jobId | |
); | |
const files: Array<{ | |
url: string; | |
name: string; | |
}> = getResponse.jobs.map((screenshotJob) => { | |
return { | |
url: screenshotJob.data, | |
name: `${screenshotJob.url.name}.png`, | |
}; | |
}); | |
files.forEach((file) => logger.info(`name: ${file.name}`)); | |
const downloadPromises = files.map((file) => | |
DownloadController.download(file.url, file.name) | |
); | |
await Promise.all(downloadPromises); | |
logger.info(`Uploading Screenshots to Cloud Bucket`); | |
const uploadPromises = files.map((file) => | |
bucketController.upload( | |
Path.resolve(__dirname, `../..`, DOWNLOAD_FOLDER, file.name), | |
file.name | |
) | |
); | |
await Promise.all(uploadPromises); | |
complete.success({ screenshots: uploadPromises.length }); | |
} catch (error) { | |
complete.failure('Failed to send slack message'); | |
} | |
}, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment