node-crontab

Lately I've been working on a project that requires tasks to be ran on user-scheduled intervals through node. We were receiving the schedules in a cron format and needed a node library that could handle scheduling the tasks and running them, even if the next iteration of the schedule was a long time away (the maximum potential amount of time a user could schedule between tasks was approximately 366 days, rounding up).

Node-schedule


The first node library I investigated was node-schedule. Node-schedule is a scheduling library for node (who'd have thunk it) that is built for similar purposes to what I had in mind. It only had a couple problems... One of the problems was that it basically only uses setTimeout/setInterval in node, both of which have a maximum duration of about 24 days, which is far less than a possible time of 366 days. Another problem was that there was no real way to have node-schedule pass in arguments for you, unless you went to changing the library.

I didn't really want to modify the library because I figured it would be much simpler to just write my own, without all of the extra stuff node-schedule allows (human-like times, for example).

The birth of a new library


Thus, node-crontab was born. Node-crontab was built to be a small library that emulated crontab in node. It requires only two parameters, the first of which is a cron-syntax schedule and the second a callback function, and it handles everything else for you (most importantly, perhaps, it manages events scheduled for far-off dates automagically). It also has a few extra parameters that allow you to pass in arguments for your function, and even allows you to give your function context.

This was my first time writing a node library, so I got to learn about some cool stuff like publishing to npmjs and writing tests in mocha. I even set up travis-ci to auto-test pushes.

So, in the end, I got to learn a bit about node, while also making something I actually needed.

Fork me on Github!


Fork me on Github! Node-crontab is publicly available on Github, so fork it if you want. I tried to make the code not-entirely-terrible, and there's even some tests for it!
Also, there's code examples on Github, which makes it a lot less dry than this blog post.



comments powered by Disqus