From f82b2fceca33b856877bb4a2e1b4964f90bb6fae Mon Sep 17 00:00:00 2001 From: ktsosno Date: Tue, 21 Feb 2017 23:05:56 -0800 Subject: [PATCH 1/4] Alter remove to have better update --- lib/index.js | 122 ++++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/lib/index.js b/lib/index.js index aa760fd..1d00167 100644 --- a/lib/index.js +++ b/lib/index.js @@ -14,20 +14,20 @@ var CronJob = require('./CronJob'); * @class CronTab * A JavaScript representation of a user crontab. Each tab has zero or more cron jobs corresponding * to the individual lines in the cron syntax. - * + * * Examples: * new CronTab('bob', function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * console.log("bob's tab: " + tab.render()); * }); - * + * * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * console.log("current user's tab: " + tab.render()); * }); - * + * * @param {String} __username__ * @param {Function} __callback__ */ @@ -38,17 +38,17 @@ function CronTab(u, cb) { var backup = {lines:[], jobs:[]}; var lines = []; var jobs = []; - + load(cb); - - + + /** * Provides access to the jobs collection. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * var jobs = tab.jobs((command:'ls -l /', comment:'this should run every night')); * for (var i = 0; i < jobs.length; i++) { * console.log(jobs[i].render()); @@ -89,20 +89,20 @@ function CronTab(u, cb) { results.push(job); } } - + return results; } this.find = this.jobs; /** * Writes the crontab to the system. Saves all information. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * var jobs = tab.jobs({command:'ls -l /'}); * tab.remove(jobs); - * + * * tab.save(function(err, tab) { * if (err) { console.log(err); process.exit(1); } * @@ -121,7 +121,7 @@ function CronTab(u, cb) { child.stdout.setEncoding('utf8'); child.stderr.setEncoding('utf8'); - + child.stdout.on('data', function(chunk) { stdout += chunk; }); @@ -138,17 +138,17 @@ function CronTab(u, cb) { cb && cb({message:stderr}, self); } }); - + child.stdin.write(this.render()); child.stdin.end(); } /** * Renders the object to a string as it would be written to the system. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * console.log(tab.render()); * }); * @@ -156,29 +156,29 @@ function CronTab(u, cb) { */ this.render = function() { var tokens = []; - + for (var i = 0; i < lines.length; i++) { var job = lines[i]; - + if (job.isValid && !job.isValid()) { tokens.push('# ' + job.toString()); continue; } - + tokens.push(job.toString()); } - + return tokens.join('\n').trim() + '\n'; } /** * Creates a new job with the specified command, comment and date. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * var future = Date.parse('2010/7/11'); - * + * * tab.create('ls -l /'); * tab.create('ls -l /', 'just a silly example'); * tab.create('ls -l /', 'just a silly example', future); @@ -230,11 +230,11 @@ function CronTab(u, cb) { } /** * Removes the specified jobs from the crontab. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * var jobs = tab.jobs({command:'ls -l /'}); * tab.remove(jobs); * }); @@ -242,6 +242,8 @@ function CronTab(u, cb) { * @param {String} __Array[CronJob]__ */ this.remove = function(jobs) { + var didRemove = false; + if (jobs instanceof CronJob) { jobs = [jobs]; } @@ -255,20 +257,22 @@ function CronTab(u, cb) { else { jobs = []; } - + for (var i = 0; i < jobs.length; i++) { - remove(jobs[i]); + didRemove = remove(jobs[i]); } - + truncateLines(); + + return didRemove; } /** * Restores this crontab to its original state. - * + * * Examples: * new CronTab(function(err, tab) { * if (err) { console.log(err); process.exit(1); } - * + * * var jobs = tab.jobs({command:'ls -l /'}); * tab.remove(jobs); * tab.reset(); @@ -278,13 +282,13 @@ function CronTab(u, cb) { lines = backup.lines.slice(); jobs = backup.jobs.slice(); } - - + + /** * Loads the system crontab into this object. * * @param {function} __callback__ - * + * * @api private */ function load(cb) { @@ -293,10 +297,10 @@ function CronTab(u, cb) { var args = makeChildArgs('load'); var command = makeChildCommand(); var child = Spawn(command, args); - + jobs = []; lines = []; - + child.stdout.setEncoding('utf8'); child.stderr.setEncoding('utf8'); @@ -313,12 +317,12 @@ function CronTab(u, cb) { cb && cb({message:stderr}, null); return; } - + var tokens = stdout.split('\n'); for (var i = 0; i < tokens.length; i++) { var token = tokens[i]; var job = makeJob(token); - + if (job != null && job.isValid()) { jobs.push(job); lines.push(job); @@ -327,50 +331,52 @@ function CronTab(u, cb) { lines.push(token); } } - + truncateLines(); - + backup.lines = lines.slice(); backup.jobs = jobs.slice(); - - cb && cb(null, self); + + cb && cb(null, self); }); } /** * Removes the specified job from the crontab. * * @param {CronJob} __job__ - * + * * @api private */ function remove(job) { var oldJobs = jobs; var oldLines = lines; - + jobs = []; lines = []; - + for (var i = 0; i < oldJobs.length; i++) { var oldJob = oldJobs[i]; - + if (oldJob != job) { jobs.push(oldJob); } } for (var i = 0; i < oldLines.length; i++) { var oldLine = oldLines[i]; - + if (oldLine != job) { lines.push(oldLine); } } + + return (oldJobs.length !== jobs.length); } /** * Creates an array of CL arguments for the system "crontab" command. Intended to be passed to * child_process.spawn. * * @param {String} __action__ 'load' | 'save' - * + * * @api private */ function makeChildArgs(action) { @@ -378,21 +384,21 @@ function CronTab(u, cb) { if (user) { args = args.concat('-u', user); } - + if (action == 'load') { args.push('-l'); } if (action == 'save' && process.platform !== 'sunos') { args.push('-'); } - + return args; } /** * Creates a system command string to run crontab. Intended to be passed to * child_process.spawn. If this is going to run for another user and the * current user is not root, we prefix the command with sudo. - * + * * @api private */ function makeChildCommand() { @@ -400,7 +406,7 @@ function CronTab(u, cb) { if (user.length > 0 && root == false) { command = 'sudo ' + command; } - + return command; } /** @@ -410,7 +416,7 @@ function CronTab(u, cb) { * @param {String|null} __line__ * @param {String} __[command]__ * @param {String} __[comment]__ - * + * * @api private */ function makeJob(line, command, comment) { @@ -427,17 +433,17 @@ function CronTab(u, cb) { } /** * Compacts the line collection by removes empty lines from the end. - * + * * @api private */ function truncateLines() { var undefined; var line = lines.pop(); - + while (line != undefined && line.toString().trim() == '') { line = lines.pop(); } - + if (line != undefined) { lines.push(line); } @@ -456,5 +462,3 @@ module.exports = { } } }; - - From acdbe9b9a2ce816dfe0174a49fdd4cad3581ec9b Mon Sep 17 00:00:00 2001 From: ktsosno Date: Thu, 23 Feb 2017 20:28:20 -0800 Subject: [PATCH 2/4] Add in pause, activate, and pausedJobs API calls --- lib/index.js | 111 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 104 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1d00167..a2b9f01 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,12 +32,13 @@ var CronJob = require('./CronJob'); * @param {Function} __callback__ */ function CronTab(u, cb) { - var self = this; - var user = u || ''; - var root = (process.getuid() == 0); - var backup = {lines:[], jobs:[]}; - var lines = []; - var jobs = []; + var self = this; + var user = u || ''; + var root = (process.getuid() == 0); + var backup = {lines:[], jobs:[]}; + var lines = []; + var jobs = []; + var pausedFlag = '#paused' load(cb); @@ -283,6 +284,101 @@ function CronTab(u, cb) { jobs = backup.jobs.slice(); } + /** + * Returns all paused jobs + * This uses the `#paused` flag to determine which comemnts are paused jobs + * + * Examples: + * new CronTab(function(err, tab) { + * if (err) { console.log(err); process.exit(1); } + * + * var jobs = tab.pausedJobsjobs(; + * }); + * + * @param {Object} __[options]__ + * @return {Array[CronJob]} + */ + this.pausedJobs = function() { + // This flag identifies 'paused' jobs in the crontab + const pausedTokens = []; + const pausedJobs = []; + + lines.forEach((line) => { + if (typeof line === 'string') { + let trimmed = line.substring(0, pausedFlag.length); + if (trimmed === pausedFlag) { + pausedTokens.push( + line.slice(pausedFlag.length) + ); + } + } + }); + + if (pausedTokens.length === 0) { + return []; + } + + pausedTokens.forEach((token) => { + const job = makeJob(token); + if (job !== null && job.isValid()) { + pausedJobs.push(job); + } + }); + + return pausedJobs; + } + /** + * Pause a job + * + * @param {CronJob} + * @return {Boolean} + */ + this.pauseJob = function(job) { + if (!job.isValid()) { + return false; + } + + const command = job.command(); + const removeTask = remove(job); + + if (removeTask) { + const pausedEntry = `${pausedFlag}${job.toString()}`; + lines.push(pausedEntry); + return true; + } + + return false; + } + /** + * Activate a job + * + * @param {CronJob} + * @return {Boolean} + */ + this.activateJob = function(job) { + let targetIndex; + lines.forEach((line, index) => { + if (typeof line === 'string') { + const trimmed = line.substring(0, pausedFlag.length); + const command = line.slice(pausedFlag.length); + // Is this a paused job and does it match + if (trimmed === pausedFlag && command === job.toString()) { + targetIndex = index; + } + } + }); + + // Remove the paused job by index + lines.splice(targetIndex, 1); + const activeJob = makeJob(job.toString()); + if (activeJob && activeJob.isValid()) { + // Re-add active, valid job into lines array + lines.push(activeJob); + return true; + } + + return false; + } /** * Loads the system crontab into this object. @@ -425,7 +521,6 @@ function CronTab(u, cb) { if (!job || !job.isValid()) { throw 'invalid job'; } - return job; } catch(e) {} @@ -454,6 +549,8 @@ function CronTab(u, cb) { // public API module.exports = { load:function() { + console.log('Using linked module'); + if (_.isString(arguments[0]) && _.isFunction(arguments[1])) { new CronTab(arguments[0], arguments[1]); } From 89653f84c64163622b840083d11793dbacc00b55 Mon Sep 17 00:00:00 2001 From: ktsosno Date: Thu, 23 Feb 2017 20:29:43 -0800 Subject: [PATCH 3/4] Remove console log --- lib/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index a2b9f01..3c2ca6a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -549,8 +549,6 @@ function CronTab(u, cb) { // public API module.exports = { load:function() { - console.log('Using linked module'); - if (_.isString(arguments[0]) && _.isFunction(arguments[1])) { new CronTab(arguments[0], arguments[1]); } From b71addee4ac83dcbc046a8c37aa13ad155de3601 Mon Sep 17 00:00:00 2001 From: ktsosno Date: Sun, 19 Mar 2017 20:58:55 -0700 Subject: [PATCH 4/4] Remvoe es6-ifying --- lib/index.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3c2ca6a..567667a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ /** * Constants */ -const COMMAND = 'crontab'; +var COMMAND = 'crontab'; /** * @ignore @@ -300,12 +300,12 @@ function CronTab(u, cb) { */ this.pausedJobs = function() { // This flag identifies 'paused' jobs in the crontab - const pausedTokens = []; - const pausedJobs = []; + var pausedTokens = []; + var pausedJobs = []; - lines.forEach((line) => { + lines.forEach(function(line) { if (typeof line === 'string') { - let trimmed = line.substring(0, pausedFlag.length); + var trimmed = line.substring(0, pausedFlag.length); if (trimmed === pausedFlag) { pausedTokens.push( line.slice(pausedFlag.length) @@ -318,8 +318,8 @@ function CronTab(u, cb) { return []; } - pausedTokens.forEach((token) => { - const job = makeJob(token); + pausedTokens.forEach(function(token) { + var job = makeJob(token); if (job !== null && job.isValid()) { pausedJobs.push(job); } @@ -338,11 +338,11 @@ function CronTab(u, cb) { return false; } - const command = job.command(); - const removeTask = remove(job); + var command = job.command(); + var removeTask = remove(job); if (removeTask) { - const pausedEntry = `${pausedFlag}${job.toString()}`; + var pausedEntry = pausedFlag + job.toString(); lines.push(pausedEntry); return true; } @@ -356,11 +356,11 @@ function CronTab(u, cb) { * @return {Boolean} */ this.activateJob = function(job) { - let targetIndex; - lines.forEach((line, index) => { + var targetIndex; + lines.forEach(function(line, index) { if (typeof line === 'string') { - const trimmed = line.substring(0, pausedFlag.length); - const command = line.slice(pausedFlag.length); + var trimmed = line.substring(0, pausedFlag.length); + var command = line.slice(pausedFlag.length); // Is this a paused job and does it match if (trimmed === pausedFlag && command === job.toString()) { targetIndex = index; @@ -370,7 +370,7 @@ function CronTab(u, cb) { // Remove the paused job by index lines.splice(targetIndex, 1); - const activeJob = makeJob(job.toString()); + var activeJob = makeJob(job.toString()); if (activeJob && activeJob.isValid()) { // Re-add active, valid job into lines array lines.push(activeJob);