<commit_before>/*
	node-schedule
	A cron-like and not-cron-like job scheduler for Node.
*/

var events = require('events'),
    util = require('util'),
    increment = require('./increment.js');

/* Job object */
var anonJobCounter = 0;
function Job(){
	var name;
	
	// process arguments to the constructor
	var arg;
	for (var i = 0; i < arguments.length; i++)
	{
		arg = arguments[i];
		if (typeof(arg) == 'string' || arg instanceof String)
			name = arg;
		else if (typeof(arg) == 'function')
			this.job = arg;
	}
	
	// give us a random name if one wasn't provided
	if (name == null)
		name = '<Anonymous Job ' + (++anonJobCounter) + '>';
	
	// setup a private pendingInvocations variable
	var pendingInvocations = [];
	
	// define properties
	Object.defineProperty(this, 'name', {
		value: name,
		writable: false,
		enumerable: true
	});
	
	// method that require private access
	this.trackInvocation = function(invocation){
		// add to our invocation list
		pendingInvocations.push(invocation);
		
		// and sort
		pendingInvocations.sort(sorter);
		
		return true;
	};
	this.stopTrackingInvocation = function(invocation){
		var invIdx = pendingInvocations.indexOf(invocation);
		if (invIdx > -1)
		{
			pendingInvocations.splice(invIdx, 1);
			return true;
		}
		
		return false;
	};
	this.cancel = function(reschedule){
		reschedule = (typeof(reschedule) == 'boolean') ? reschedule : false;
		
		var inv, newInv;
		var newInvs = [];
		for (var i = 0; i < pendingInvocations.length; i++)
		{
			inv = pendingInvocations[i];
			
			cancelInvocation(inv);
			
			if (reschedule && inv.recurrenceRule.recurs)
			{
				newInv = scheduleNextRecurrence(inv.recurrenceRule, this, inv.fireDate);
				if (newInv !== null)
					newInvs.push(newInv);
			}
		}
		
		pendingInvocations = [];
		
		for (var i = 0; i < newInvs.length; i++)
			this.trackInvocation(newInvs[i]);
		
		return true;
	};
	this.cancelNext = function(reschedule){
		reschedule = (typeof(reschedule) == 'boolean') ? reschedule : true;
		
		if (pendingInvocations.length == 0)
			return false;
		
		var newInv;
		var nextInv = pendingInvocations.shift();
		
		cancelInvocation(nextInv);
		
		if (reschedule && nextInv.recurrenceRule.recurs)
		{
			newInv = scheduleNextRecurrence(nextInv.recurrenceRule, this, nextInv.fireDate);
			if (newInv !== null)
				this.trackInvocation(newInv);
		}
		
		return true;
	};
	this.nextInvocation = function(){
		if (pendingInvocations.length == 0)
			return null;
		return pendingInvocations[0].fireDate;
	};
}

util.inherits(Job, events.EventEmitter);

Job.prototype.invoke = function(){
	if (typeof(this.job) == 'function')
		this.job();
};

Job.prototype.runOnDate = function(date){
	return this.schedule(date);
};

Job.prototype.schedule = function(spec){
	var success = false;

  spec = dateSpec(spec);	
	if (typeof(spec) == 'object' && spec instanceof Date)
	{
		var inv = new Invocation(this, spec);
		scheduleInvocation(inv);
		success = this.trackInvocation(inv);
	}
	else if (typeof(spec) == 'string' || (typeof(spec) == 'object' && spec instanceof String))
	{
		spec = RecurrenceRule.fromCronString(spec);
	}
	else if (typeof(spec == 'object') && !(spec instanceof RecurrenceRule))
	{
		var r = new RecurrenceRule();
		if ('year' in spec)
			r.year = spec.year;
		if ('month' in spec)
			r.month = spec.month;
		if ('date' in spec)
			r.date = spec.date;
		if ('dayOfWeek' in spec)
			r.dayOfWeek = spec.dayOfWeek;
		if ('hour' in spec)
			r.hour = spec.hour;
		if ('minute' in spec)
			r.minute = spec.minute;
		if ('second' in spec)
			r.second = spec.second;
		
		spec = r;
	}
	
	// schedule a recurring invocation
	if (typeof(spec) == 'object' && spec instanceof RecurrenceRule)
	{
		if (spec.recurs)
		{
			var inv = scheduleNextRecurrence(spec, this);
			if (inv !== null)
				success = this.trackInvocation(inv);
		}
	}
	
	return success;
};

function dateSpec(spec) {
    if (spec instanceof Date === false) {
        var validDate = new Date(spec);
        if (validDate.toString() !== 'Invalid Date') {
            spec = validDate;
        }
    }
    return spec;
}

/* API
	invoke()
	runOnDate(date)
	schedule(date || recurrenceRule || cronstring)
	cancel(reschedule = false)
	cancelNext(reschedule = true)
<commit_msg>Update schedule.js<commit_after>/*
	node-schedule
	A cron-like and not-cron-like job scheduler for Node.
*/

var events = require('events'),
    util = require('util'),
    increment = require('./increment.js');

/* Job object */
var anonJobCounter = 0;
function Job(){
	var name;
	
	// process arguments to the constructor
	var arg;
	for (var i = 0; i < arguments.length; i++)
	{
		arg = arguments[i];
		if (typeof(arg) == 'string' || arg instanceof String)
			name = arg;
		else if (typeof(arg) == 'function')
			this.job = arg;
	}
	
	// give us a random name if one wasn't provided
	if (name == null)
		name = '<Anonymous Job ' + (++anonJobCounter) + '>';
	
	// setup a private pendingInvocations variable
	var pendingInvocations = [];
	
	// define properties
	Object.defineProperty(this, 'name', {
		value: name,
		writable: false,
		enumerable: true
	});
	
	// method that require private access
	this.trackInvocation = function(invocation){
		// add to our invocation list
		pendingInvocations.push(invocation);
		
		// and sort
		pendingInvocations.sort(sorter);
		
		return true;
	};
	this.stopTrackingInvocation = function(invocation){
		var invIdx = pendingInvocations.indexOf(invocation);
		if (invIdx > -1)
		{
			pendingInvocations.splice(invIdx, 1);
			return true;
		}
		
		return false;
	};
	this.cancel = function(reschedule){
		reschedule = (typeof(reschedule) == 'boolean') ? reschedule : false;
		
		var inv, newInv;
		var newInvs = [];
		for (var i = 0; i < pendingInvocations.length; i++)
		{
			inv = pendingInvocations[i];
			
			cancelInvocation(inv);
			
			if (reschedule && inv.recurrenceRule.recurs)
			{
				newInv = scheduleNextRecurrence(inv.recurrenceRule, this, inv.fireDate);
				if (newInv !== null)
					newInvs.push(newInv);
			}
		}
		
		pendingInvocations = [];
		
		for (var i = 0; i < newInvs.length; i++)
			this.trackInvocation(newInvs[i]);
		
		return true;
	};
	this.cancelNext = function(reschedule){
		reschedule = (typeof(reschedule) == 'boolean') ? reschedule : true;
		
		if (pendingInvocations.length == 0)
			return false;
		
		var newInv;
		var nextInv = pendingInvocations.shift();
		
		cancelInvocation(nextInv);
		
		if (reschedule && nextInv.recurrenceRule.recurs)
		{
			newInv = scheduleNextRecurrence(nextInv.recurrenceRule, this, nextInv.fireDate);
			if (newInv !== null)
				this.trackInvocation(newInv);
		}
		
		return true;
	};
	this.nextInvocation = function(){
		if (pendingInvocations.length == 0)
			return null;
		return pendingInvocations[0].fireDate;
	};
}

util.inherits(Job, events.EventEmitter);

Job.prototype.invoke = function(){
	if (typeof(this.job) == 'function')
		this.job();
};

Job.prototype.runOnDate = function(date){
	return this.schedule(date);
};

Job.prototype.schedule = function(spec){
	var success = false;

  spec = dateSpec(spec);	
	if (typeof(spec) == 'object' && spec instanceof Date)
	{
		var inv = new Invocation(this, spec);
		scheduleInvocation(inv);
		success = this.trackInvocation(inv);
	}
	else if (typeof(spec) == 'string' || (typeof(spec) == 'object' && spec instanceof String))
	{
		spec = RecurrenceRule.fromCronString(spec);
	}
	else if (typeof(spec == 'object') && !(spec instanceof RecurrenceRule))
	{
		var r = new RecurrenceRule();
		if ('year' in spec)
			r.year = spec.year;
		if ('month' in spec)
			r.month = spec.month;
		if ('date' in spec)
			r.date = spec.date;
		if ('dayOfWeek' in spec)
			r.dayOfWeek = spec.dayOfWeek;
		if ('hour' in spec)
			r.hour = spec.hour;
		if ('minute' in spec)
			r.minute = spec.minute;
		if ('second' in spec)
			r.second = spec.second;
		
		spec = r;
	}
	
	// schedule a recurring invocation
	if (typeof(spec) == 'object' && spec instanceof RecurrenceRule)
	{
		if (spec.recurs)
		{
			var inv = scheduleNextRecurrence(spec, this);
			if (inv !== null)
				success = this.trackInvocation(inv);
		}
	}
	
	return success;
};

function dateSpec(spec) {
    if (spec instanceof Date === false) {
        var validDate = new Date(spec);
        if (validDate.toString() !== 'Invalid Date' && validDate > new Date()) {
            spec = validDate;
        }
    }
    return spec;
}

/* API
	invoke()
	runOnDate(date)
	schedule(date || recurrenceRule || cronstring)
	cancel(reschedule = false)
	cancelNext(reschedule = true)

{'fraction_non_alphanumeric': 0.14290742696233721, 'fraction_numerical': 0.0021119324181626186, 'mean_word_length': 3.416580310880829, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 8, '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': '1495686', 'n_tokens_mistral': 3267, 'n_tokens_neox': 2856, 'n_words': 716}