<commit_before>function Workflow() {
	this.config = {
		statusInterval: 60000,
		workflowRunCount: 0,
		workFlowMaxRuns: -1
	};
	this.running = false;
	this.lastInterval = -1;
	this.wfTimeout = null;
	this.handlers = {
		statusStart: [],
		statusComplete: [],
		workflowStart: [],
		workflowComplete: []
	};
}

Workflow.prototype.on = function(event, handler) {
	if (this.handlers[event]) {
		this.handlers[event].push(handler);
	}
	return this;
};

Workflow.prototype.off = function(event, handler) {
	if (this.handlers[event] && this.handlers[event].indexOf(handler) != -1) {
		this.handlers[event].splice(this.handlers[event].indexOf(handler), 1);
	}
	return this;
};

Workflow.prototype.init = function(api, config) {
	this.api = api;
	this.config.apiConfig = config;
	return this;
};

Workflow.prototype.maxRuns = function(value) {
	value = parseInt(value);
	if (value < -1) {
		value = -1;
	}
	this.config.workFlowMaxRuns = value;
	return this;
};

Workflow.prototype.statusInterval = function(value) {
	value = parseInt(value);
	if (value < 5000) {
		value = 5000;
	}
	this.config.statusInterval = value;
	return this;
};

Workflow.prototype.end = function(func) {
	this.config.endFunc = func;
};

Workflow.prototype.start = function(func) {
	this.running = true;

	var self = this;

	function getUpdateInterval() {
		var interval = self.config.apiConfig && self.config.apiConfig.deviceInfo && self.config.apiConfig.deviceInfo.updateInterval ? self.config.apiConfig.deviceInfo.updateInterval : 30 * 60 * 1000;
		if (interval < 5000) {
			interval = 5000;
		}
		return interval;
	}

	function workflow() {
		
		self.config.workflowRunCount++;

		if (self.handlers.workflowStart.length > 0) {
			self.handlers.workflowStart.forEach(function(item) {
				item(self.config.workflowRunCount);
			});
		}
		
		var interval = getUpdateInterval();

		func(this, interval, function(info, err) {
			if (self.handlers.workflowComplete.length > 0) {
				self.handlers.workflowComplete.forEach(function(item) {
					item(info, err);
				});
			}
			
			if (self.config.workFlowMaxRuns == -1 || self.config.workflowRunCount < self.config.workFlowMaxRuns) {
				self.wfTimeout = setTimeout(workflow, interval);
			} else {
				if (typeof self.config.endFunc == 'function') {
					self.config.endFunc(self);
				}
				self.running = false;
			}
		});
	}

	setImmediate(function status() {
		if (self.handlers.statusStart.length > 0) {
			self.handlers.statusStart.forEach(function(item) {
				item();
			});
		}
		self.api.status(function(config, err) {

			if (self.handlers.statusComplete.length > 0) {
				self.handlers.statusComplete.forEach(function(item) {
					item(config, err);
				});
			}
			var interval = getUpdateInterval();

			// Reset workflow execution time
			if (self.wfTimeout && interval != self.lastInterval) {
				clearTimeout(self.wfTimeout);
				workflow();
			}
			self.lastInterval = interval;

			if (self.running) {
				setTimeout(status, self.config.statusInterval);
			}
		});
	});

	setTimeout(workflow, 500);
	return this;<commit_msg>Change to prevent workflow and status from running at the same time<commit_after>function Workflow() {
	this.config = {
		statusInterval: 60000,
		workflowRunCount: 0,
		workFlowMaxRuns: -1
	};
	this.running = false;
	this.lastInterval = -1;
	this.wfTimeout = null;
	this.handlers = {
		statusStart: [],
		statusComplete: [],
		workflowStart: [],
		workflowComplete: []
	};
	this.pendingWorkflow = null;
}

Workflow.prototype.on = function(event, handler) {
	if (this.handlers[event]) {
		this.handlers[event].push(handler);
	}
	return this;
};

Workflow.prototype.off = function(event, handler) {
	if (this.handlers[event] && this.handlers[event].indexOf(handler) != -1) {
		this.handlers[event].splice(this.handlers[event].indexOf(handler), 1);
	}
	return this;
};

Workflow.prototype.init = function(api, config) {
	this.api = api;
	this.config.apiConfig = config;
	return this;
};

Workflow.prototype.maxRuns = function(value) {
	value = parseInt(value);
	if (value < -1) {
		value = -1;
	}
	this.config.workFlowMaxRuns = value;
	return this;
};

Workflow.prototype.statusInterval = function(value) {
	value = parseInt(value);
	if (value < 5000) {
		value = 5000;
	}
	this.config.statusInterval = value;
	return this;
};

Workflow.prototype.end = function(func) {
	this.config.endFunc = func;
};

Workflow.prototype.start = function(func) {
	this.running = true;

	var self = this;

	function getUpdateInterval() {
		var interval = self.config.apiConfig && self.config.apiConfig.deviceInfo && self.config.apiConfig.deviceInfo.updateInterval ? self.config.apiConfig.deviceInfo.updateInterval : 30 * 60 * 1000;
		if (interval < 5000) {
			interval = 5000;
		}
		return interval;
	}

	function workflow() {
		self.pendingWorkflow = function() {

			// Make null to only run once
			self.pendingWorkflow = null;

			self.config.workflowRunCount++;

			if (self.handlers.workflowStart.length > 0) {
				self.handlers.workflowStart.forEach(function(item) {
					item(self.config.workflowRunCount);
				});
			}

			var interval = getUpdateInterval();

			func(this, interval, function(info, err) {
				if (self.handlers.workflowComplete.length > 0) {
					self.handlers.workflowComplete.forEach(function(item) {
						item(info, err);
					});
				}

				if (self.config.workFlowMaxRuns == -1 || self.config.workflowRunCount < self.config.workFlowMaxRuns) {
					self.wfTimeout = setTimeout(workflow, interval);
				} else {
					if (typeof self.config.endFunc == 'function') {
						self.config.endFunc(self);
					}
					self.running = false;
				}
			});
		};
	}

	setImmediate(function status() {
		if (self.handlers.statusStart.length > 0) {
			self.handlers.statusStart.forEach(function(item) {
				item();
			});
		}
		self.api.status(function(config, err) {

			if (self.handlers.statusComplete.length > 0) {
				self.handlers.statusComplete.forEach(function(item) {
					item(config, err);
				});
			}
			var interval = getUpdateInterval();

			// Reset workflow execution time
			if (self.wfTimeout && interval != self.lastInterval) {
				clearTimeout(self.wfTimeout);
				workflow();
			}
			self.lastInterval = interval;

			if (typeof self.pendingWorkflow == 'function') {
				self.pendingWorkflow();
			}

			if (self.running) {
				setTimeout(status, self.config.statusInterval);
			}
		});
	});

	setTimeout(workflow, 100);
	return this;
{'fraction_non_alphanumeric': 0.14508580343213728, 'fraction_numerical': 0.013728549141965678, 'mean_word_length': 3.6693372177713037, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3699294', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2144, 'n_words': 383}