-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjobHandler.js
116 lines (106 loc) · 2.72 KB
/
jobHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var fs = require('fs');
var jobHandler = {};
jobHandler.jobs = [];
jobHandler.updateJobs = updateJobsFromFile;
jobHandler.callback = function() {};
jobHandler.setCallback = setCallback;
module.exports = jobHandler;
function setCallback( callback ) {
jobHandler.callback = callback;
}
function updateJobsFromFile() {
clearJobFuncSchedule();
fs.readdir( __dirname + '/jobs', selectJobs );
}
function unrequire( moduleName ) {
var jobDefName = require.resolve(moduleName);
delete require.cache[ jobDefName ];
}
function selectJobs( err, files ) {
jobHandler.jobs = [];
files.forEach( function( file ) {
if( file.substr( file.indexOf('.'), file.length ) == ".js" ) {
console.log( "Reading file " + file );
var name = './jobs/' + file;
var newJobs;
unrequire( name );
try {
var newJobsDef = require( name );
if( newJobsDef.setNewJobCallback ) {
if( newJobsDef.setNewJobCallback && typeof newJobsDef.setNewJobCallback === 'function' ) {
newJobsDef.setNewJobCallback( addJob );
}
} else {
newJobs = newJobsDef;
if( ! Array.isArray( newJobs ) ) {
newJobs = [ newJobs ];
}
if( newJobs.length ) {
newJobs.forEach( addJob );
}
}
} catch(err) {
console.log("Error loading " + file);
console.log( err );
return;
}
}
});
}
function addJobExec( job, index ) {
if( job.func ) {
job.func( callbackWrapper( index ) );
if( job.interval ) {
if( job.intervalId ) clearInterval( job.intervalId );
job.intervalId = setInterval( job.func, job.interval, callbackWrapper( index ) );
}
}
}
function addJob( job ) {
if( job.enabled ) {
job.id = jobHandler.jobs.push( job ) - 1;
addJobExec( job, job.id );
console.log( "Adding Job: " + job.name );
}
}
function clearJobFuncSchedule() {
jobHandler.jobs.forEach( function( job ) {
if( job.intervalId )
clearInterval( job.intervalId );
});
}
function callbackWrapper( jobNo ) {
return function( err, data ) {
if( ! err ) {
var news = false;
Object.keys( data ).forEach( function( key ) {
var oldData = jobHandler.jobs[ jobNo ][ key ];
var newData = data[ key ];
if( newData &&
( ( ! oldData && newData.length ) ||
( oldData && oldData != newData &&
newData.length ) ) )
{
news = true;
jobHandler.jobs[ jobNo ][ key ] = newData;
}
});
if( news )
jobHandler.callback( jobHandler.jobs[ jobNo ] );
} else {
console.log( "Error in callbackWrapper: " + err );
}
};
}
function getJobById( id, callback ) {
if( id >= jobHandler.jobs.length ) callback( null );
else {
jobHandler.jobs.some( function( job ) {
if( job.id === id ) {
console.log("Job " + job.name + " has id " + id );
callback( job );
return true;
}
});
}
}