🤖 Webtask.io : Cron job as a Service

Welcome to serverless era! I should say “There’s a serverless for that” from now on. Let’s start with cron job.

Welcome webtask.io!

It will do free 2 cron job for you so let’s begin.

Install wt command line interface.

$ npm install wt-cli -g

Initialize wt.

$ wt init YOUR_EMAIL

You’ll get…

Please enter the verification code we sent to foo@bar.com below.
Verification code: 123456
Welcome to webtasks! Create your first one as follows:
$ echo "module.exports = function (cb) { cb(null, 'Hello'); }" > hello.js
$ wt create hello.js

Create a simple webtask.

$ echo "module.exports = function (cb) {cb(null, 'Hello');}" > hello.js

And

$ wt create hello.js

Then you’ll get…

Webtask created
You can access your webtask at the following url:
https://wt-foo-bar-com-0.run.webtask.io/hello

Nice! the fun is just start you can check here for modules you can require e.g fetch

Let’s try fetch webtask’s README.md

Modify hello.js as below.

var fetch = require('isomorphic-fetch@2.2.0')
module.exports = function (cb) {
fetch('https://raw.githubusercontent.com/auth0/wt-cli/master/README.md')
.then(function (response) {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.text();
}).then(function (text) {
console.log(new Date().toUTCString());
cb(null, text);
});
}

And update it…

$ wt update hello hello.js

Then try curl it you should get…

Now we cron every 2 minutes

$ wt cron schedule "*/2 * * * *" ./hello.js
Name: hello
State: active
Container: wt-katopz-gmail-com-0
Schedule: */2 * * * *
Next run: 1/16/2017, 11:14:00 PM

To ensure everything working, we log!

$ wt logs

And you should see…

[16:13:43.288Z]  INFO wt: connected to streaming logs (container=wt-katopz-gmail-com-0)
[16:14:03.055Z] INFO wt: new webtask request 1484583242765.907460
[16:14:03.323Z] INFO wt: Mon, 16 Jan 2017 16:14:03 GMT
[16:14:03.330Z] INFO wt: finished webtask request 1484583242765.907460 with HTTP 200 in 396ms
[16:16:10.455Z] INFO wt: new webtask request 1484583370163.697907
[16:16:10.695Z] INFO wt: Mon, 16 Jan 2017 16:16:10 GMT
[16:16:10.695Z] INFO wt: finished webtask request 1484583370163.697907 with HTTP 200 in 379ms
...

Every 2 minutes! FYI, for free plan you can create only 2 crons.

Enjoy repeat yourself!

Note (ref)

Don’t miss a chance to get free $10 for DigitalOcean ← 2 months free hosting!
And do claim your cheapest .io name before it’s gone at NameCheap.