NodeJS Cron Job with Kue

At Cozy we needed to write some jobs of this kind for our Mails app. For that we used Kue a simple task manager for NodeJS that requires Redis to run. The documentation is great but there was no sample for writing a cron task. That’s why I share this little snippet that runs a 2 seconds long job every 3 seconds, in case you need something similar.

NB : The delay between each job is set in minutes.

kue = require 'kue'

Job = kue.Job

jobs = kue.createQueue()

# Set up server if you want to see your task progression with a beautiful UI 3003

# Your cron timing
delay = 3000

# Function used to launch a job.
repeatJob = ->
    job = jobs.create "test complete",
        title: "my job"
        info: "job is working"

    job.on 'promotion', () ->
        console.log + " #" + + " promoted"


# The job to run, a fake task that is 2 seconds long.
cronTask = (job, done) ->
    global.currentJob =
    console.log + " #" + + " job started"
    setTimeout ->
        console.log "my job is done"
    , 2000

# Register job
jobs.process "test complete", myFunc = cronTask

# Check for new job every 3s, change this value to set your cron timing.

# Run the cron job for the first time.

4 Responses to NodeJS Cron Job with Kue

  1. ashuorg says:

    please explain your example using clear syntax of javascript

  2. z says:

    Oh my, you require kue, but still repeat stuff using setTimeout. Kinda defeats the purpose. I know this is an old article, but it’s easy to google this, so a tip to those who got here looking for a solution: keep looking

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: