|
1 | 1 | import { Construct } from 'constructs'; |
2 | | -import { CfnOutput, Duration } from 'aws-cdk-lib'; |
| 2 | +import { CfnOutput, Duration, TimeZone } from 'aws-cdk-lib'; |
3 | 3 | import { Architecture, DockerImageCode, DockerImageFunction, IFunction } from 'aws-cdk-lib/aws-lambda'; |
4 | 4 | import { Platform } from 'aws-cdk-lib/aws-ecr-assets'; |
5 | 5 | import { Database } from './database'; |
6 | 6 | import { EventBus } from './event-bus'; |
7 | 7 | import { PolicyStatement } from 'aws-cdk-lib/aws-iam'; |
8 | 8 | import { join } from 'path'; |
| 9 | +import { Schedule, ScheduleExpression, ScheduleTargetInput } from 'aws-cdk-lib/aws-scheduler'; |
| 10 | +import { LambdaInvoke } from 'aws-cdk-lib/aws-scheduler-targets'; |
9 | 11 |
|
10 | 12 | export interface AsyncJobProps { |
11 | 13 | readonly database: Database; |
@@ -49,5 +51,21 @@ export class AsyncJob extends Construct { |
49 | 51 |
|
50 | 52 | new CfnOutput(this, 'HandlerArn', { value: handler.functionArn }); |
51 | 53 | this.handler = handler; |
| 54 | + |
| 55 | + // you can add scheduled jobs here. |
| 56 | + this.addSchedule( |
| 57 | + 'SampleJob', |
| 58 | + ScheduleExpression.cron({ minute: '0', hour: '0', day: '1', timeZone: TimeZone.ETC_UTC }), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public addSchedule(jobType: string, schedule: ScheduleExpression, payload?: any) { |
| 63 | + return new Schedule(this, jobType, { |
| 64 | + schedule, |
| 65 | + target: new LambdaInvoke(this.handler, { |
| 66 | + input: ScheduleTargetInput.fromObject({ jobType, payload }), |
| 67 | + retryAttempts: 5, |
| 68 | + }), |
| 69 | + }); |
52 | 70 | } |
53 | 71 | } |
0 commit comments