-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabber.module
103 lines (79 loc) · 3.46 KB
/
grabber.module
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
<?php
require_once(__DIR__ . '/grabber.menu.php');
require_once(__DIR__ . '/include/Registry.php');
require_once(__DIR__ . '/include/ProcessHelper.php');
require_once(__DIR__ . '/include/Logger.php');
require_once(__DIR__ . '/include/Tracker.php');
require_once(__DIR__ . '/include/ProxyList.php');
function grabber_cron() {
//$scheduler = \Grabber\Registry::get_scheduler();
//$scheduler->execute();
}
function grabber_execute() {
\Grabber\ProxyList::update_file();
$scheduler = \Grabber\Registry::get_scheduler();
$scheduler->execute();
}
function grabber_execute_processor($processor_name) {
$processor_name = filter_var($processor_name, FILTER_SANITIZE_STRING);
$semaphore = \Grabber\Registry::get_semaphore();
// wait for called process finished
sleep(3); // TODO check request flag (header|var)
if ($semaphore->is_set("Processor-{$processor_name}")) {
\Grabber\Logger::log("Execute processor: " . $processor_name . " - Already executed.");
print "Already executed: {$processor_name}.";
exit();
}
$semaphore->set("Processor-{$processor_name}");
\Grabber\Logger::log("Execute processor: " . $processor_name . " ...");
try {
$processor = \Grabber\Registry::get_processor($processor_name);
$processor->execute();
\Grabber\Logger::log("Finish processor: " . get_class($processor) . " - [ OK ]");
print 'OK';
} catch (\Grabber\TrackerTimeoutException $e) {
\Grabber\Logger::log("Processor timeout: " . $processor_name . ': ' . $e->getMessage() . " - [TIME]");
\Grabber\ProcessHelper::fork();
print 'TIME';
} catch (\Exception $e) {
\Grabber\Logger::error("Processor error: " . $processor_name . ': ' . $e->getMessage());
print 'FAIL';
}
exit();
}
function grabber_execute_scanner($scanner_name, $thread_id) {
$scanner_name = filter_var($scanner_name, FILTER_SANITIZE_STRING);
$thread_id = filter_var($thread_id, FILTER_SANITIZE_NUMBER_INT);
$semaphore = \Grabber\Registry::get_semaphore();
if ($thread_id > \Grabber\ProcessHelper::MAX_SCANNER_THREADS) {
print "Maximum: " . \Grabber\ProcessHelper::MAX_SCANNER_THREADS . " thread/scanner. (MAX_SCANNER_THREADS)";
exit();
}
// prevent overload
sleep(3);
if ($semaphore->is_set("Scanner-{$scanner_name}-{$thread_id}")) {
\Grabber\Logger::log("Execute scanner: {$scanner_name}/{$thread_id} - Already executed.");
print "Already executed: {$scanner_name}/{$thread_id}.";
exit();
}
$semaphore->set("Scanner-{$scanner_name}-{$thread_id}");
\Grabber\Logger::log("Execute scanner: {$scanner_name}/{$thread_id}...");
try {
$scanner = \Grabber\Registry::get_scanner($scanner_name);
$scanner->execute();
\Grabber\Logger::log("Finish scanner: " . get_class($scanner) . "/{$thread_id} - [ OK ]");
print 'OK';
} catch (\Grabber\TrackerTimeoutException $e) {
\Grabber\Logger::log("Scanner timeout: {$scanner_name}/{$thread_id}: " . $e->getMessage() . " - [TIME]");
\Grabber\ProcessHelper::fork();
print 'TIME';
} catch (\Exception $e) {
\Grabber\Logger::error("Scanner error: {$scanner_name}/{$thread_id}: " . $e->getMessage());
print 'FAIL';
}
exit();
}
function grabber_execute_scanner_all($scanner_name) {
$scanner_name = filter_var($scanner_name, FILTER_SANITIZE_STRING);
\Grabber\ProcessHelper::execute_scanner($scanner_name);
}