dachev / node-crontab

A module for creating, reading, updating, and deleting system cron jobs
189 stars 35 forks source link

Fixed invalid syntax for creating a cron job returning null #18

Closed toymachiner62 closed 8 years ago

toymachiner62 commented 8 years ago

When creating a cronjob with an invalid syntax, it used to just return null, so a user wouldn't know why the cron job was not created.

I've modified this so it now throws an error when there is an invalid cronjob trying to be created so the user knows what the issue is.

dachev commented 8 years ago

@toymachiner62 Not throwing errors was a deliberate design decision. You can still check the validity by ensuring that create() didn't return null and job.isValid() == true

toymachiner62 commented 8 years ago

Interesting. What is the reason for allowing invalid syntax? I'm just curious.

On Sunday, November 8, 2015, Blagovest Dachev notifications@github.com wrote:

@toymachiner62 https://github.com/toymachiner62 Not throwing errors was a deliberate design decision. You can still check the validity by ensuring that create() didn't return null and job.isValid() == true

— Reply to this email directly or view it on GitHub https://github.com/dachev/node-crontab/pull/18#issuecomment-154929143.

Tom Caflisch 507.202.0087 TomCaflisch@gmail.com http://allthingswebdesign.com

dachev commented 8 years ago

create() does not allow invalid syntax, it simply chooses to return a null instead of throwing an exception.

Also, validation capabilities should not be overestimated. There are many versions and flavors of crontab, each with its own options and supported syntax.

toymachiner62 commented 8 years ago

I suppose that's fair since we check for job === null && !job.isValid().

dachev commented 8 years ago

I haven used this module in a while but this should work :-)

require('crontab').load(function(err, crontab) {
  var jobs = crontab.jobs();

  for (var i = 0; i < jobs.length; i++) {
    crontab.remove(jobs[0]);
  }

  crontab.save(function(err, crontab) {

  });
});

Let me know if it doesn't and I'll look into making this possible.

toymachiner62 commented 8 years ago

That worked like a charm. Thanks!

dachev commented 8 years ago

No problem.