-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.js
98 lines (80 loc) · 3.06 KB
/
daemon.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
const fs = require('fs');
const http = require('http');
const net = require('net');
const path = require('path');
const Logs = require('@coya/logs');
const Contact = require('@coya/contact');
const { TaskManager } = require('@coya/task-manager');
const CleaningTask = require('./cleaning_task');
const ScrapingTask = require('./scraping_task');
const config = require('./config.js');
const clients = [];
const logs = new Logs('daemon_server', config);
const httpAddress = 'http://localhost/jobs/binding';
Contact.init(config);
const sendErrorByEmail = Contact.sendEmailToMe.bind(null, config.smtpSender, 'Error from jobs watcher daemon');
const server = net.createServer(function(client) {
logs.info('New client connected.');
clients.push(client);
client.on('end', function() {
logs.info('Client deconnected.');
clients.splice(clients.indexOf(client), 1);
logs.debug('Number of clients : ' + clients.length + '.');
});
});
server.on('error', function(err) {
logs.error(err);
if(err.code == 'EACCESS')
proces.exit(1);
});
fs.access(config.ipcAddress, (err) => {
if(!err)
fs.unlinkSync(config.ipcAddress); // need to remove the pipe file on linux
server.listen(config.ipcAddress, function() {
logs.info('Daemon server started on "' + server.address() + '".');
http.get(httpAddress).on('error', (e) => { // notify the server
logs.warning('HTTP server not started.');
});
});
});
function sendJobsToClients(job) {
logs.debug(job);
clients.forEach(function(client) {
client.write(JSON.stringify(job));
client.pipe(client);
});
}
function closeServer() {
server.close(function() {
logs.info('Daemon server stopped.');
});
}
const cleaningTask = new CleaningTask('cleaning', 3600, config);
const freelancerTask = new ScrapingTask('freelancer', 120, config);
freelancerTask.on('job', sendJobsToClients);
freelancerTask.on('error', sendErrorByEmail);
const pphTask = new ScrapingTask('pph', 120, config);
pphTask.on('job', sendJobsToClients);
pphTask.on('error', sendErrorByEmail);
const guruTask = new ScrapingTask('guru', 120, config);
guruTask.on('job', sendJobsToClients);
guruTask.on('error', sendErrorByEmail);
const truelancerTask = new ScrapingTask('truelancer', 120, config);
truelancerTask.on('job', sendJobsToClients);
truelancerTask.on('error', sendErrorByEmail);
const twagoTask = new ScrapingTask('twago', 120, config);
twagoTask.on('job', sendJobsToClients);
twagoTask.on('error', sendErrorByEmail);
const codeurTask = new ScrapingTask('codeur', 120, config);
codeurTask.on('job', sendJobsToClients);
codeurTask.on('error', sendErrorByEmail);
const taskManager = new TaskManager(config);
taskManager.onTaskEnd((task) => {
sendErrorByEmail('The task "' + task.name + '" has been removed from the task manager.');
});
taskManager.onEnd(() => {
taskManager.logs.info('Task manager shutted down.');
ScrapingTask.closeScraper();
closeServer();
});
taskManager.processAsynchronousTasks([cleaningTask, freelancerTask, pphTask, guruTask, truelancerTask, twagoTask, codeurTask]);