Skip to content

Commit b0d78ac

Browse files
committed
improve cron example
1 parent 35926dc commit b0d78ac

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

aws-node-scheduled-cron/README.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AWS Node Scheduled Cron Example
22

3-
This is an example of creating a function that runs as a cron job using the serverless `schedule` event. For more information on `schedule` event check out [our docs](https://serverless.com/framework/docs/providers/aws/events/schedule/).
3+
This is an example of creating a function that runs as a cron job using the serverless `schedule` event. For more information on `schedule` event check out the Serverless docs on [schedule](https://serverless.com/framework/docs/providers/aws/events/schedule/).
44

55
Schedule events use the `rate` or `cron` syntax.
66

@@ -37,14 +37,68 @@ All fields are required and time zone is UTC only.
3737

3838
Read the [AWS cron expression syntax](http://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html) docs for more info on how to setup cron
3939

40-
## Running
40+
## Deploy
41+
42+
In order to deploy the you endpoint simply run
43+
44+
```bash
45+
serverless deploy
46+
```
47+
48+
The expected result should be similar to:
49+
50+
```bash
51+
Serverless: Packaging service...
52+
Serverless: Uploading CloudFormation file to S3...
53+
Serverless: Uploading service .zip file to S3 (1.47 KB)...
54+
Serverless: Updating Stack...
55+
Serverless: Checking Stack update progress...
56+
..............
57+
Serverless: Stack update finished...
58+
59+
Service Information
60+
service: scheduled-cron-example
61+
stage: dev
62+
region: us-east-1
63+
api keys:
64+
None
65+
endpoints:
66+
None
67+
functions:
68+
scheduled-cron-example-dev-cron: arn:aws:lambda:us-east-1:377024778620:function:scheduled-cron-example-dev-cron
69+
scheduled-cron-example-dev-secondCron: arn:aws:lambda:us-east-1:377024778620:function:scheduled-cron-example-dev-secondCron
70+
```
71+
72+
There is no additional step required. Your defined schedule becomes active right away after deployment.
73+
74+
## Usage
4175

4276
To see your cron job running tail your logs with:
4377

4478
```bash
4579
serverless logs --function cron --tail
4680
```
4781

82+
The expected result should be similar to:
83+
84+
```bash
85+
START RequestId: e6e64a4e-b57d-11e6-9eee-0340b40f6d48 Version: $LATEST
86+
2016-11-28 16:18:23.755 (+01:00) e6e64a4e-b57d-11e6-9eee-0340b40f6d48 Your cron function "scheduled-cron-example-dev-cron" ran at Mon Nov 28 2016 15:18:23 GMT+0000 (UTC)
87+
END RequestId: e6e64a4e-b57d-11e6-9eee-0340b40f6d48
88+
REPORT RequestId: e6e64a4e-b57d-11e6-9eee-0340b40f6d48 Duration: 4.01 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 16 MB
89+
90+
START RequestId: 0a770e6f-b57e-11e6-ba68-f188460981c7 Version: $LATEST
91+
2016-11-28 16:19:22.921 (+01:00) 0a770e6f-b57e-11e6-ba68-f188460981c7 Your cron function "scheduled-cron-example-dev-cron" ran at Mon Nov 28 2016 15:19:22 GMT+0000 (UTC)
92+
END RequestId: 0a770e6f-b57e-11e6-ba68-f188460981c7
93+
REPORT RequestId: 0a770e6f-b57e-11e6-ba68-f188460981c7 Duration: 0.69 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 16 MB
94+
```
95+
96+
Since this only shows you the logs of the first cron job simply change the function name and run the command again:
97+
98+
```bash
99+
serverless logs --function secondCron --tail
100+
```
101+
48102
## Additonal Resources
49103

50-
For more information on running cron with Serverless check out the [Tutorial: Serverless Scheduled Tasks](https://parall.ax/blog/view/3202/tutorial-serverless-scheduled-tasks) by parallax.
104+
For more information on running cron with Serverless check out the [Tutorial: Serverless Scheduled Tasks](https://parall.ax/blog/view/3202/tutorial-serverless-scheduled-tasks) by parallax.

aws-node-scheduled-cron/handler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
'use strict';
12

2-
module.exports.run = () => {
3+
module.exports.run = (event, context) => {
34
const time = new Date();
4-
console.log(`Your cron ran ${time}`); // eslint-disable-line no-console
5+
console.log(`Your cron function "${context.functionName}" ran at ${time}`); // eslint-disable-line no-console
56
};

aws-node-scheduled-cron/serverless.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ functions:
88
cron:
99
handler: handler.run
1010
events:
11+
# Invoke Lambda function every minute
1112
- schedule: rate(1 minute)
13+
secondCron:
14+
handler: handler.run
15+
events:
16+
# Invoke Lambda function every 2nd minute from Mon-Fri
17+
- schedule: cron(0/2 * ? * MON-FRI *)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy