Repository: serverless/serverless (JavaScript) — Bug Fix

Issue:
Schedule events not always working
<!--
1. If you have a question and not a bug/feature request please ask it at http://forum.serverless.com
2. Please check if an issue already exists so there are no duplicates
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->

# This is a (Bug Report)
Bug Report
## Description

For bug reports:
* What went wrong?
Scheduled event not triggering.
* What did you expect should have happened?
Event should trigger no matter how it was defined.
* What was the config you used?
```
    handler: test.test
    events:
      - schedule:
          rate: rate(10 minutes)
```
* What stacktrace or error message from your provider did you see?

* ***Serverless Framework Version you're using***: 1.3.0
* ***Operating System***: Windows 10
* ***Stack Trace***:
* ***Provider Error messages***:

Schedule events not always working
<!--
1. If you have a question and not a bug/feature request please ask it at http://forum.serverless.com
2. Please check if an issue already exists so there are no duplicates
3. Check out and follow our Guidelines: https://github.com/serverless/serverless/blob/master/CONTRIBUTING.md
4. Fill out the whole template so we have a good overview on the issue
5. Do not remove any section of the template. If something is not applicable leave it empty but leave it in the Issue
6. Please follow the template, otherwise we'll have to ask you to update it
-->

# This is a (Bug Report)
Bug Report
## Description

For bug reports:
* What went wrong?
Scheduled event not triggering.
* What did you expect should have happened?
Event should trigger no matter how it was defined.
* What was the config you used?
```
    handler: test.test
    events:
      - schedule:
          rate: rate(10 minutes)
```
* What stacktrace or error message from your provider did you see?

* ***Serverless Framework Version you're using***: 1.3.0
* ***Operating System***: Windows 10
* ***Stack Trace***:
* ***Provider Error messages***:



Discussion:
The problem is that when "schedule" is string, the event enabled by default but when it's an object it's disabled by default. So to make it work you need to always add "enabled: true" to your schedule event. I believe that's a bug, at least from documentation it seems like that's not the expected behavior. 

If you confirm that it is a bug I'll fix it.
This is happening because `undefined` is falsey in JavaScript, due to [this check](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/deploy/compile/events/schedule/index.js#L44) when the schedule is an object.

I agree with @erndob in that this looks like a bug, because the string version [defaults to true](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/deploy/compile/events/schedule/index.js#L69).
The problem is that when "schedule" is string, the event enabled by default but when it's an object it's disabled by default. So to make it work you need to always add "enabled: true" to your schedule event. I believe that's a bug, at least from documentation it seems like that's not the expected behavior. 

If you confirm that it is a bug I'll fix it.
This is happening because `undefined` is falsey in JavaScript, due to [this check](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/deploy/compile/events/schedule/index.js#L44) when the schedule is an object.

I agree with @erndob in that this looks like a bug, because the string version [defaults to true](https://github.com/serverless/serverless/blob/master/lib/plugins/aws/deploy/compile/events/schedule/index.js#L69).

Resolving patch:
diff --git a/docs/providers/aws/events/schedule.md b/docs/providers/aws/events/schedule.md
index b5e32a024d9..d1e12f40402 100644
--- a/docs/providers/aws/events/schedule.md
+++ b/docs/providers/aws/events/schedule.md
@@ -25,6 +25,8 @@ functions:
 
 ## Enabling/Disabling functions
 
+Schedule events are enabled by default.
+
 This will create and attach a schedule event for the `aggregate` function which is disabled. If enabled it will call
 the `aggregate` function every 10 minutes.
 
diff --git a/lib/plugins/aws/deploy/compile/events/schedule/index.js b/lib/plugins/aws/deploy/compile/events/schedule/index.js
index 72fc42529ce..02344a25d40 100644
--- a/lib/plugins/aws/deploy/compile/events/schedule/index.js
+++ b/lib/plugins/aws/deploy/compile/events/schedule/index.js
@@ -41,7 +41,10 @@ class AwsCompileScheduledEvents {
                   .Error(errorMessage);
               }
               ScheduleExpression = event.schedule.rate;
-              State = event.schedule.enabled ? 'ENABLED' : 'DISABLED';
+              State = 'ENABLED';
+              if (event.schedule.enabled === false) {
+                State = 'DISABLED';
+              }
               Input = event.schedule.input;
               InputPath = event.schedule.inputPath;
               Name = event.schedule.name;


Test patch:
diff --git a/lib/plugins/aws/deploy/compile/events/schedule/index.test.js b/lib/plugins/aws/deploy/compile/events/schedule/index.test.js
index de02c20f895..bb3eb72a0c6 100644
--- a/lib/plugins/aws/deploy/compile/events/schedule/index.test.js
+++ b/lib/plugins/aws/deploy/compile/events/schedule/index.test.js
@@ -101,6 +101,54 @@ describe('AwsCompileScheduledEvents', () => {
       ).to.equal('AWS::Lambda::Permission');
     });
 
+    it('should respect enabled variable, defaulting to true', () => {
+      awsCompileScheduledEvents.serverless.service.functions = {
+        first: {
+          events: [
+            {
+              schedule: {
+                rate: 'rate(10 minutes)',
+                enabled: false,
+              },
+            },
+            {
+              schedule: {
+                rate: 'rate(10 minutes)',
+                enabled: true,
+              },
+            },
+            {
+              schedule: {
+                rate: 'rate(10 minutes)',
+              },
+            },
+            {
+              schedule: 'rate(10 minutes)',
+            },
+          ],
+        },
+      };
+
+      awsCompileScheduledEvents.compileScheduledEvents();
+
+      expect(awsCompileScheduledEvents.serverless.service
+        .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule1
+        .Properties.State
+      ).to.equal('DISABLED');
+      expect(awsCompileScheduledEvents.serverless.service
+        .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule2
+        .Properties.State
+      ).to.equal('ENABLED');
+      expect(awsCompileScheduledEvents.serverless.service
+        .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule3
+        .Properties.State
+      ).to.equal('ENABLED');
+      expect(awsCompileScheduledEvents.serverless.service
+        .provider.compiledCloudFormationTemplate.Resources.FirstEventsRuleSchedule4
+        .Properties.State
+      ).to.equal('ENABLED');
+    });
+
     it('should respect name variable', () => {
       awsCompileScheduledEvents.serverless.service.functions = {
         first: {