-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(events,applicationautoscaling): schedule can be a token #13064
Conversation
Title does not follow the guidelines of Conventional Commits. Please adjust title before merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting this PR 😊 My comments are below.
Looks like the build and other PR checks are failing. Can you look into them?
As for the failing CodeBuild job, the build logs are available as a comment above in this PR.
const invokeScheduleInMinutes = new CfnParameter(stack, 'invokeScheduleInMinutes', { | ||
type: 'Number', | ||
description: 'The minutes between scheduled invocations.', | ||
default: 5, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you switch to using the Lazy
class (which also uses the same token mechanism), it would be easier to test. Something like this -
const lazyDuration = Duration.minutes(Lazy.number({ produce: () => 5 }));
const rate = appscaling.Schedule.rate(lazyDuration)
test.equal('rate(<token> minutes)', rate);
test.equal('rate(5 minutes)', stack.resolve(rate));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will give it a try.
public static rate(duration: Duration): Schedule { | ||
public static rate(duration: Duration): Schedule { | ||
if(duration.isUnresolved()){ | ||
return new LiteralSchedule(`rate(${duration.toHumanString()})`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this only supports minutes, hours and days but Duration
also supports seconds and milliseconds. It could support more in the future.
Also, I believe the number must not be a fraction
Could we add validations for these, and throw an appropriate error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw there was a testcase in test.cron.ts, test.schedule.ts which was makes sure if the duration was not a whole number, following snippet would throw an error. But this would only happen in case of minutes where integral is sent as true. Looking at duration.toSeconds() === 0 being checked explictly makes me wonder if it been done deliberately.
if (!Number.isInteger(value) && integral) {
throw new Error(`'${amount} ${fromUnit}' cannot be converted into a whole number of ${toUnit}.`);
}
Also, in order to support the validation we would need to expose a method in duration since unit is not exposed outside.
test.done(); | ||
}, | ||
|
||
'rate can be in minutes'(test: Test) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious: why do we need this extra test case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to make sure we had something around regular minutes.
expect(stack).to(haveResourceLike('AWS::Events::Rule', { | ||
Targets: [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be asserting the ScheduleExpression
property instead of the Targets
property, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, my bad.
@@ -102,6 +102,49 @@ export = { | |||
})); | |||
test.done(); | |||
}, | |||
|
|||
'can use token from cfnParameter'(test: Test) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a duplicate of the test in test.rule.ts
. Does this check anything additional that the other test case doesn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me check and remove if it is duplicate.
@@ -85,6 +85,40 @@ export = { | |||
test.done(); | |||
}, | |||
|
|||
'add scheduled scaling with cfnParameter'(test: Test) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as my other comment. Feels like the test in test.cron.ts should provide sufficient cover. Do we need an extra test case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me check and remove if not required.
Pull request has been modified.
I missed this in the first round but What should happen for this to work is that the actual token encoded number - https://docs.aws.amazon.com/cdk/latest/guide/tokens.html#tokens_number - must be produced here. So you will likely have to introduce a new API, say const duration = Duration.seconds(5);
const output = duration.format('duration is ${amount} ${unit}');
output <-- "duration is 5 seconds" With tokens, it would be - const duration = Duration.seconds(Lazy.number({ produce: () => 5 }));
const output = duration.format('duration is ${amount} ${unit}');
output <-- "duration is -1.8881545897087626e+289 seconds" // this is a number encoded token
stack.resolve(output) <-- "duration is 5 seconds" |
I had tried sending token value as it is like -1.8881545897087626e+289. I
had modified toString() in duration.ts and used it instead of humanstring.
But it had not worked too with stack.resolve. Will check again.
…On Fri, Feb 19, 2021 at 5:07 PM Niranjan Jayakar ***@***.***> wrote:
I missed this in the first round but toHumanString() is not going to work
here. It's because it places '' when it finds an unresolved token.
What should happen for this to work is that the actual token encoded
number -
https://docs.aws.amazon.com/cdk/latest/guide/tokens.html#tokens_number -
must be produced here.
So you will likely have to introduce a new API, say format(), that would
do something like this -
const duration = Duration.seconds(5);const output = duration.format('duration is ${amount} ${unit}');
output <-- "duration is 5 seconds"
With tokens, it would be -
const duration = Duration.seconds(Lazy.number({ produce: () => 5 }));const output = duration.format('duration is ${amount} ${unit}');
output <-- "duration is -1.8881545897087626e+289 seconds" // this is a number encoded tokenstack.resolve(output) <-- "duration is 5 seconds"
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#13064 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABYHIJTV3EMPECAD23PJGLDS72EFZANCNFSM4XVKCSOQ>
.
|
First time user issues! I am seeing some 80+ merge commits since I pulled from master aws-cdk into my fork repo. Let me know if we could discard this one and create new PR from another fork repository. |
@sneharathod - this is quite normal in the CDK repo as we have quite a few commits going in each day. You can choose one of two things - (a) do nothing; when the PR is approved, our bot will automatically update your branch to track the latest and then merge, or (b) click on the 'Update branch' button at the bottom of the PR that will update your branch to track the latest commit on It is necessary to perform (b) explicitly only if there are merge conflicts with master that need to be resolved manually. You will notified of this in the area that currently says "This branch is out-of-date with the base branch". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better and I like the approach. Thanks for the feedback.
Some small suggestions below.
@@ -66658,6 +66658,12 @@ | |||
"Required": false, | |||
"UpdateType": "Immutable" | |||
}, | |||
"EnableExecuteCommand": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you take a look at why this is coming in? Seems unrelated to your change.
yarn.lock
Outdated
@@ -3687,16 +3682,6 @@ dot-prop@^5.1.0: | |||
dependencies: | |||
is-obj "^2.0.0" | |||
|
|||
dotenv-json@^1.0.0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You changes should not impact this file either.
Sure. I missed them.
…On Mon, Mar 1, 2021 at 3:22 PM Niranjan Jayakar ***@***.***> wrote:
***@***.**** requested changes on this pull request.
Much better and I like the approach. Thanks for the feedback.
Some small suggestions below.
------------------------------
In ***@***.***/aws-applicationautoscaling/lib/schedule.ts
<#13064 (comment)>:
> + if (validDurationUnit.indexOf(duration.unitLabel()) === -1) {
+ throw new Error('Allowed unit for scheduling is: \'minute\',\'minutes\', \'hour\', \'hours\', \'day\' or \'days\'');
Some code readability.
⬇️ Suggested change
- if (validDurationUnit.indexOf(duration.unitLabel()) === -1) {
- throw new Error('Allowed unit for scheduling is: \'minute\',\'minutes\', \'hour\', \'hours\', \'day\' or \'days\'');
+ if (!validDurationUnit.includes(duration.unitLabel())) {
+ throw new Error("Allowed unit for scheduling is: 'minute', 'minutes', 'hour', 'hours', 'day' or 'days'");
------------------------------
In ***@***.***/aws-events/README.md
<#13064 (comment)>:
> @@ -83,6 +83,7 @@ onCommitRule.addTarget(new targets.SnsTopic(topic, {
## Scheduling
You can configure a Rule to run on a schedule (cron or rate).
+Rate can be specified in Minutes/Hours/Days.
⬇️ Suggested change
-Rate can be specified in Minutes/Hours/Days.
+Rate must be specified in minutes, hours or days.
------------------------------
In
***@***.***/cfnspec/spec-source/000_CloudFormationResourceSpecification.json
<#13064 (comment)>:
> @@ -66658,6 +66658,12 @@
"Required": false,
"UpdateType": "Immutable"
},
+ "EnableExecuteCommand": {
can you take a look at why this is coming in? Seems unrelated to your
change.
------------------------------
In yarn.lock
<#13064 (comment)>:
> @@ -3687,16 +3682,6 @@ dot-prop@^5.1.0:
dependencies:
is-obj "^2.0.0"
-dotenv-json@^1.0.0:
You changes should not impact this file either.
------------------------------
In ***@***.***/aws-events/test/test.rule.ts
<#13064 (comment)>:
> + expect(stack).toMatch({
+ 'Resources': {
+ 'MyScheduledRuleD15B5BA3': {
+ 'Type': 'AWS::Events::Rule',
+ 'Properties': {
+ 'Name': 'rateInMinutes',
+ 'ScheduleExpression': 'rate(5 minutes)',
+ 'State': 'ENABLED',
+ },
+ },
+ },
+ });
When asserting properties of single resources, we use -
⬇️ Suggested change
- expect(stack).toMatch({
- 'Resources': {
- 'MyScheduledRuleD15B5BA3': {
- 'Type': 'AWS::Events::Rule',
- 'Properties': {
- 'Name': 'rateInMinutes',
- 'ScheduleExpression': 'rate(5 minutes)',
- 'State': 'ENABLED',
- },
- },
- },
- });
+ expect(stack).toHaveResourceLike('AWS::Events::Rule', {
+ 'Name': 'rateInMinutes',
+ 'ScheduleExpression': 'rate(5 minutes)',
+ });
------------------------------
In ***@***.***/core/lib/duration.ts
<#13064 (comment)>:
> @@ -252,6 +252,28 @@ export class Duration {
}
return ret;
}
+
+ /**
Can you add unit tests for the new methods in this file?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#13064 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABYHIJSPIHMH3HAJVPPIA5DTBOPJHANCNFSM4XVKCSOQ>
.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting this back to 'request changes' to address remaining comments.
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
Fix for Issue: #9413
Testing:
Unit tests added for Schedule.rate used in ApplicationAutoScaling and Events.
design
folderBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license