<commit_before>'use strict';

var debug = require('debug')('at-suncalc:scheduler');

exports.execute = function execute(config){
    require('crontab').load(function(err, crontab){

        //TODO: assert

        debug('********************************');
        debug('id:', config.id);
        debug('command:', config.command);
        debug('when:', config.when);
        debug('comment:', config.comment);
        debug('********************************');

        var identifier = 'SCHEDULER: ' + config.id + ' ::';

        var jobs = crontab.jobs({comment:new RegExp('^' + identifier)});

        if(jobs.length){
            debug('Process already registered:');
            jobs.map(function(job){
                debug(job.toString());
            });
            process.exit();
        }

        if(config.dryRun){
            debug(config.when, config.command, '#', identifier + ' ' + config.comment);
            debug('Dry run. Exit');
            process.exit();
        }

        var job = crontab.create(config.command, config.when, identifier + ' ' + config.comment);

        crontab.save(function(err, ctr){
            if(err) {
                console.error('ERROR', err.message);
                debug(err.toString());
                debug(err);
            }
        });
    });
};
<commit_msg>lib scheduler handle opts for command;<commit_after>'use strict';

var debug = require('debug')('at-suncalc:scheduler');

exports.execute = function execute(config){
    require('crontab').load(function(err, crontab){

        //TODO: assert

        debug('********************************');
        debug('id:', config.id);
        debug('command:', config.command);
        debug('options:', config.opts);
        debug('when:', config.when);
        debug('comment:', config.comment);
        debug('********************************');

        var identifier = 'SCHEDULER: ' + config.id + ' ::',
            jobs = crontab.jobs({comment: new RegExp('^' + identifier)}),
            cmd = config.command + (config.opts ?  ' ' + config.opts : '');

        if(jobs.length){
            debug('Process already registered:');
            jobs.map(function(job){
                debug(job.toString());
            });
            process.exit();
        }

        if(config.dryRun){
            debug(config.when, cmd, '#', identifier + ' ' + config.comment);
            debug('Dry run. Exit');
            process.exit();
        }

        var job = crontab.create(cmd, config.when, identifier + ' ' + config.comment);

        crontab.save(function(err, ctr){
            if(err) {
                console.error('ERROR', err.message);
                debug(err.toString());
                debug(err);
            }
        });
    });
};

{'fraction_non_alphanumeric': 0.20786719595813785, 'fraction_numerical': 0.0, 'mean_word_length': 2.0262008733624453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1950778', 'n_tokens_mistral': 772, 'n_tokens_neox': 735, 'n_words': 131}