-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjob-creator.js
68 lines (54 loc) · 1.44 KB
/
job-creator.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
var kue = require('kue')
, DB = require(__dirname + '/web/database.js')
, cron = require('cron')
, jobs = kue.createQueue()
, Job = kue.Job;
var cronRunTime = '*/10 * * * *'
, jobFailCheckTime = '*/5 * * * *';
// Execute the node cron jobs
try {
var newInterval = new cron.CronJob(cronRunTime, function() {
JobCreator.init();
}, null, true);
var failedJobInterval = new cron.CronJob(jobFailCheckTime, function() {
JobFail.check();
}, null, true);
} catch (ex) {
console.log('Invalid cron pattern ', ex);
}
var JobCreator = {
addNew: function() {
DB.job.findJobToProcess(function(err, dataJobs) {
if (!err) {
dataJobs.forEach(function(job) {
var current = jobs.create('serp', {
title: job.name
, id: job._id
, dbId: job._id
, frames: job.keywords.length * job.sources.length
}).save();
console.log('Added job #' + current.id);
DB.job.updateJobStatus(job._id, 'queued', function(err, job) {
if (!err) console.log('Job changed status ');
})
});
} else {
console.log('Error found: ', err);
}
});
},
init: function() {
var self = this;
DB.util.ping(function(err, result) {
if (!err) {
self.addNew();
}
});
}
};
var JobFail = {
check: function() {
}
};
// Web interface for the kue
kue.app.listen(3030);