Skip to content

Commit

Permalink
fixed README and policy generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Oct 24, 2023
1 parent b8ac3e7 commit a3c91ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-scheduler-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const target = new targets.LambdaInvoke(fn, {
## Overriding Target Properties

If you wish to reuse the same target in multiple schedules, you can override target properties like `input`,
`maximumRetryAttempts` and `maximumEventAgeInSeconds` when creating a Schedule using the `targetOverrides` parameter:
`retryAttempts` and `maxEventAge` when creating a Schedule using the `targetOverrides` parameter:

```ts
declare const target: targets.LambdaInvoke;
Expand All @@ -266,7 +266,7 @@ const oneTimeSchedule = new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(cdk.Duration.hours(12)),
target,
targetOverrides: {
input: ScheduleTargetInput.fromText("Overriding Target Input"),
input: ScheduleTargetInput.fromText('Overriding Target Input'),
maxEventAge: Duration.seconds(180),
retryAttempts: 5,
},
Expand Down
38 changes: 26 additions & 12 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ export class Schedule extends Resource implements ISchedule {
* The schedule group associated with this schedule.
*/
public readonly group?: IGroup;

/**
* The arn of the schedule.
*/
public readonly scheduleArn: string;

/**
* The name of the schedule.
*/
Expand All @@ -231,6 +233,11 @@ export class Schedule extends Resource implements ISchedule {
*/
readonly key?: kms.IKey;

/**
* A `RetryPolicy` object that includes information about the retry policy settings.
*/
private readonly retryPolicy?: CfnSchedule.RetryPolicyProperty;

constructor(scope: Construct, id: string, props: ScheduleProps) {
super(scope, id, {
physicalName: props.scheduleName,
Expand All @@ -245,12 +252,7 @@ export class Schedule extends Resource implements ISchedule {
this.key.grantEncryptDecrypt(targetConfig.role);
}

const retryPolicy = {
maximumEventAgeInSeconds: props.targetOverrides?.maxEventAge?.toSeconds() ?? targetConfig.retryPolicy?.maximumEventAgeInSeconds,
maximumRetryAttempts: props.targetOverrides?.retryAttempts ?? targetConfig.retryPolicy?.maximumRetryAttempts,
};

this.validateRetryPolicy(retryPolicy.maximumEventAgeInSeconds, retryPolicy.maximumRetryAttempts);
this.retryPolicy = targetConfig.retryPolicy;

const resource = new CfnSchedule(this, 'Resource', {
name: this.physicalName,
Expand All @@ -267,7 +269,7 @@ export class Schedule extends Resource implements ISchedule {
props.targetOverrides?.input?.bind(this) :
targetConfig.input?.bind(this),
deadLetterConfig: targetConfig.deadLetterConfig,
retryPolicy: retryPolicy.maximumEventAgeInSeconds || retryPolicy.maximumRetryAttempts ? retryPolicy : undefined,
retryPolicy: this.renderRetryPolicy(props.targetOverrides?.maxEventAge?.toSeconds(), props.targetOverrides?.retryAttempts),
ecsParameters: targetConfig.ecsParameters,
kinesisParameters: targetConfig.kinesisParameters,
eventBridgeParameters: targetConfig.eventBridgeParameters,
Expand All @@ -284,12 +286,24 @@ export class Schedule extends Resource implements ISchedule {
});
}

private validateRetryPolicy(maximumEventAgeInSeconds: number | undefined, maximumRetryAttempts: number | undefined) {
if (maximumEventAgeInSeconds && (maximumEventAgeInSeconds < 60 || maximumEventAgeInSeconds > 86400)) {
throw new Error(`maximumEventAgeInSeconds must be between 60 and 86400, got ${maximumEventAgeInSeconds}`);
private renderRetryPolicy(
maximumEventAgeInSeconds?: number,
maximumRetryAttempts?: number,
): CfnSchedule.RetryPolicyProperty | undefined {
const policy = {
...this.retryPolicy,
maximumEventAgeInSeconds: maximumEventAgeInSeconds ?? this.retryPolicy?.maximumEventAgeInSeconds,
maximumRetryAttempts: maximumRetryAttempts ?? this.retryPolicy?.maximumRetryAttempts,
};

if (policy.maximumEventAgeInSeconds && (policy.maximumEventAgeInSeconds < 60 || policy.maximumEventAgeInSeconds > 86400)) {
throw new Error(`maximumEventAgeInSeconds must be between 60 and 86400, got ${policy.maximumEventAgeInSeconds}`);
}
if (maximumRetryAttempts && (maximumRetryAttempts < 0 || maximumRetryAttempts > 185)) {
throw new Error(`maximumRetryAttempts must be between 0 and 185, got ${maximumRetryAttempts}`);
if (policy.maximumRetryAttempts && (policy.maximumRetryAttempts < 0 || policy.maximumRetryAttempts > 185)) {
throw new Error(`maximumRetryAttempts must be between 0 and 185, got ${policy.maximumRetryAttempts}`);
}

const isEmptyPolicy = Object.values(policy).every(value => value === undefined);
return !isEmptyPolicy ? policy : undefined;
}
}

0 comments on commit a3c91ef

Please sign in to comment.