Skip to content

Instantly share code, notes, and snippets.

@frankrousseau
Created August 28, 2012 16:49
Show Gist options
  • Save frankrousseau/3500433 to your computer and use it in GitHub Desktop.
Save frankrousseau/3500433 to your computer and use it in GitHub Desktop.
Cron job with Kue
Job = kue.Job
jobs = kue.createQueue()
# Set up server if you want to see your task progression with a beautiful UI
kue.app.listen 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 job.data.title + " #" + job.id + " promoted"
job.delay(delay)
job.save()
# The job to run, a fake task that is 2 seconds long.
cronTask = (job, done) ->
global.currentJob = job.id
console.log job.data.title + " #" + job.id + " job started"
setTimeout ->
console.log "my job is done"
repeatJob()
done()
, 2000
# Register job
jobs.process "test complete", myFunc = cronTask
# Check for new job every 3s, change this value to set your cron timing.
jobs.promote(delay)
# Run the cron job for the first time.
repeatJob()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment