Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/jobs/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class MonitorJob extends job.Job {
return true;
}

if (task.dirty) {
return true;
}

if ((now + duration - lastStart) / 1000 >= interval) {
return true;
}
Expand All @@ -74,7 +78,8 @@ class MonitorJob extends job.Job {
taskmanager.rerunTask({
db: common.db,
id: task._id,
autoUpdate: true
autoUpdate: true,
dirty: task.dirty
}, function(e) {
if (e) {
log.e(e, e.stack);
Expand Down
90 changes: 89 additions & 1 deletion api/utils/taskmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,89 @@ taskmanager.createTask = function(options, callback) {
}
};

var checkIfAllRulesMatch = function(rules, data) {
var match = true;
for (var key in rules) {
if (data[key]) {
if (rules[key] === data[key]) {
continue;
}
else {
if (typeof rules[key] === "object") {
if (!checkIfAllRulesMatch(rules[key], data[key])) {
return false;
}
}
else {
if (data[key].$in) {
if (data[key].$in.indexOf(rules[key]) === -1) {
return false;
}
}
else if (data[key].$nin) {
if (data[key].$nin.indexOf(rules[key]) !== -1) {
return false;
}
}
else {
return false;
}
}
}
}
else {
return false;
}
}
return match;
};

taskmanager.markReportsDirtyBasedOnRule = function(options, callback) {
common.db.collection("long_tasks").find({
autoRefresh: true,
}).toArray(function(err, tasks) {
var ids_to_mark_dirty = [];
if (err) {
log.e("Error while fetching tasks", err);
if (callback && typeof callback === "function") {
callback();
}
return;

}
tasks = tasks || [];
for (var z = 0; z < tasks.length; z++) {
try {
var req = JSON.parse(tasks[z].request);
if (checkIfAllRulesMatch(options.rules, req.json.queryObject)) {
ids_to_mark_dirty.push(tasks[z]._id);
}
}
catch (e) {
log.e(' got error while process task request parse', e);
}

}
if (ids_to_mark_dirty.length > 0) {
common.db.collection("long_tasks").updateMany({_id: {$in: ids_to_mark_dirty}}, {$set: {dirty: new Date().getTime()}}, function(err3) {
if (err3) {
log.e("Error while updating reports", err3);
}
if (callback && typeof callback === "function") {
callback();
}
});
}
else {
if (callback && typeof callback === "function") {
callback();
}
}


});

};
/**
* Save result from the task
* @param {object} options - options for the task
Expand Down Expand Up @@ -388,6 +471,9 @@ taskmanager.saveResult = function(options, data, callback) {
options.db.collection("long_tasks").update({_id: options.subtask}, updateObj, {'upsert': false}, function() {});
}
options.db.collection("long_tasks").findOne({_id: options.id}, function(error, task) {
if (task && task.dirty && task.dirty < task.start) {
update.dirty = false;
}
if (options.gridfs || (task && task.gridfs)) {
//let's store it in gridfs
update.data = {};
Expand Down Expand Up @@ -886,6 +972,8 @@ taskmanager.errorResults = function(options, callback) {
* @param {object} options - options for the task
* @param {object} options.db - database connection
* @param {string} options.id - id of the task result
* @param {boolean} options.autoUpdate - if auto update is needed or not
* @param {boolean} options.dirty - if dirty is true then it means some part of report is wrong. It should be regenerated fully.
* @param {funciton} callback - callback for the result
*/
taskmanager.rerunTask = function(options, callback) {
Expand Down Expand Up @@ -990,7 +1078,7 @@ taskmanager.rerunTask = function(options, callback) {
reqData.json.period = JSON.stringify(reqData.json.period);
}
options.subtask = res.subtask;
reqData.json.autoUpdate = options.autoUpdate || false;
reqData.json.autoUpdate = ((!options.dirty) && (options.autoUpdate || false)); //If dirty set autoUpdate to false
if (!reqData.json.api_key && res.creator) {
options.db.collection("members").findOne({_id: common.db.ObjectID(res.creator)}, function(err1, member) {
if (member && member.api_key) {
Expand Down
Loading