@@ -323,6 +323,89 @@ taskmanager.createTask = function(options, callback) {
323323 }
324324} ;
325325
326+ var checkIfAllRulesMatch = function ( rules , data ) {
327+ var match = true ;
328+ for ( var key in rules ) {
329+ if ( data [ key ] ) {
330+ if ( rules [ key ] === data [ key ] ) {
331+ continue ;
332+ }
333+ else {
334+ if ( typeof rules [ key ] === "object" ) {
335+ if ( ! checkIfAllRulesMatch ( rules [ key ] , data [ key ] ) ) {
336+ return false ;
337+ }
338+ }
339+ else {
340+ if ( data [ key ] . $in ) {
341+ if ( data [ key ] . $in . indexOf ( rules [ key ] ) === - 1 ) {
342+ return false ;
343+ }
344+ }
345+ else if ( data [ key ] . $nin ) {
346+ if ( data [ key ] . $nin . indexOf ( rules [ key ] ) !== - 1 ) {
347+ return false ;
348+ }
349+ }
350+ else {
351+ return false ;
352+ }
353+ }
354+ }
355+ }
356+ else {
357+ return false ;
358+ }
359+ }
360+ return match ;
361+ } ;
362+
363+ taskmanager . markReportsDirtyBasedOnRule = function ( options , callback ) {
364+ common . db . collection ( "long_tasks" ) . find ( {
365+ autoRefresh : true ,
366+ } ) . toArray ( function ( err , tasks ) {
367+ var ids_to_mark_dirty = [ ] ;
368+ if ( err ) {
369+ log . e ( "Error while fetching tasks" , err ) ;
370+ if ( callback && typeof callback === "function" ) {
371+ callback ( ) ;
372+ }
373+ return ;
374+
375+ }
376+ tasks = tasks || [ ] ;
377+ for ( var z = 0 ; z < tasks . length ; z ++ ) {
378+ try {
379+ var req = JSON . parse ( tasks [ z ] . request ) ;
380+ if ( checkIfAllRulesMatch ( options . rules , req . json . queryObject ) ) {
381+ ids_to_mark_dirty . push ( tasks [ z ] . _id ) ;
382+ }
383+ }
384+ catch ( e ) {
385+ log . e ( ' got error while process task request parse' , e ) ;
386+ }
387+
388+ }
389+ if ( ids_to_mark_dirty . length > 0 ) {
390+ common . db . collection ( "long_tasks" ) . updateMany ( { _id : { $in : ids_to_mark_dirty } } , { $set : { dirty : new Date ( ) . getTime ( ) } } , function ( err3 ) {
391+ if ( err3 ) {
392+ log . e ( "Error while updating reports" , err3 ) ;
393+ }
394+ if ( callback && typeof callback === "function" ) {
395+ callback ( ) ;
396+ }
397+ } ) ;
398+ }
399+ else {
400+ if ( callback && typeof callback === "function" ) {
401+ callback ( ) ;
402+ }
403+ }
404+
405+
406+ } ) ;
407+
408+ } ;
326409/**
327410* Save result from the task
328411* @param {object } options - options for the task
@@ -388,6 +471,9 @@ taskmanager.saveResult = function(options, data, callback) {
388471 options . db . collection ( "long_tasks" ) . update ( { _id : options . subtask } , updateObj , { 'upsert' : false } , function ( ) { } ) ;
389472 }
390473 options . db . collection ( "long_tasks" ) . findOne ( { _id : options . id } , function ( error , task ) {
474+ if ( task && task . dirty && task . dirty < task . start ) {
475+ update . dirty = false ;
476+ }
391477 if ( options . gridfs || ( task && task . gridfs ) ) {
392478 //let's store it in gridfs
393479 update . data = { } ;
@@ -886,6 +972,8 @@ taskmanager.errorResults = function(options, callback) {
886972* @param {object } options - options for the task
887973* @param {object } options.db - database connection
888974* @param {string } options.id - id of the task result
975+ * @param {boolean } options.autoUpdate - if auto update is needed or not
976+ * @param {boolean } options.dirty - if dirty is true then it means some part of report is wrong. It should be regenerated fully.
889977* @param {funciton } callback - callback for the result
890978*/
891979taskmanager . rerunTask = function ( options , callback ) {
@@ -990,7 +1078,7 @@ taskmanager.rerunTask = function(options, callback) {
9901078 reqData . json . period = JSON . stringify ( reqData . json . period ) ;
9911079 }
9921080 options . subtask = res . subtask ;
993- reqData . json . autoUpdate = options . autoUpdate || false ;
1081+ reqData . json . autoUpdate = ( ( ! options . dirty ) && ( options . autoUpdate || false ) ) ; //If dirty set autoUpdate to false
9941082 if ( ! reqData . json . api_key && res . creator ) {
9951083 options . db . collection ( "members" ) . findOne ( { _id : common . db . ObjectID ( res . creator ) } , function ( err1 , member ) {
9961084 if ( member && member . api_key ) {
0 commit comments