/* Copyright (c) 2012 Mircea Alexandru */

var _       = require('underscore');
var uuid    = require('uuid');
var cronJob = require('cron').CronJob;

module.exports = function (options, register) {
  var name  = 'cron'
  var seneca = this
  var opts
  var cronJobs = {}

  seneca.add({role:name, cmd:'addjob'},function(msg, respond){
    var cron_time = msg.time
    var action = msg.act
    var after = msg.after
    var timezone = msg.timezone

    console.log('setting cron for', msg, action, after)

    var job = new cronJob(cron_time, action, after, true, timezone);
    var id = uuid()
    cronJobs[id] = job

    respond(null, id)
  });

  seneca.add({role:name, cmd:'stopjob'},function(msg, respond){
    var id = msg.id

    console.log('stop cron job', id)
    if ( _.isUndefined(cronJobs[id]) ){
      console.log('cannot stop job, job not found', id)
      respond(new Error('invalid cron job ' + id))
    }
    else{
      var job = cronJobs[id]
      job.stop();
      respond()
    }
  });

  seneca.add({role:name, cmd:'startjob'},function(msg, respond){
    var id = msg.id

    console.log('start cron job', id)
    if ( _.isUndefined(cronJobs[id]) ){
      console.log('cannot start job, job not found', id)

      respond(new Error('invalid cron job ' + id))// here I must report error
    }
    else{
      var job = cronJobs[id]
      job.start();
      respond()
    }
  });

  seneca.add({role:name, cmd:'closejob'}, function(msg, respond){
    var id = msg.id

    console.log('close cron job', id)
    if ( _.isUndefined(cronJobs[id]) ){
      console.log('cannot close job, job not found', id)
      respond(new Error('invalid cron job ' + id))// here I must report error
    }
    else{
      var job = cronJobs[id]
      delete cronJobs[id]
      job.stop();
      respond()
    }
  });

  seneca.add({cmd:'close'}, function(msg, respond){
    console.log('close cron jobs')
    for (var id in cronJobs) {
      var job = cronJobs[id];
      console.log('close cron job', id)
      job.stop();
    }
    respond()
  })

  register(null, {
    name:'cron'
  })
}

@@ -173,51 +173,21 @@
 tion
- 
 (
+ 
 options
-, register) %7B%0A  var name  = 'cron'
+ ) %7B
 %0A  v
@@ -213,12 +213,22 @@
 var 
-opts
+name  = 'cron'
 %0A  v
@@ -2032,42 +2032,17 @@
   re
-gister(null, %7B%0A    name:'cron'%0A  %7D)
+turn name;
 %0A%7D%0A

Update to seneca plugin declaration