-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.php
139 lines (120 loc) · 3.75 KB
/
run.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Multithread parser runner
*
* @uses pcntl_fork, nohup, exec
*
* @usage nohup php run.php 5 &
*/
/**
* Defining constants
*/
DEFINE('MEMORY_LIMIT', 10 * 1024 /*10 MB*/);
DEFINE('TIME_LIMIT', 6 * 60 * 60 /*6 Hours*/);
DEFINE('LOGS_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'logs');
$time_start = microtime(1);
/**
* Retargeting IO
*/
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
$STDIN = fopen('/dev/null', 'r');
$STDOUT = fopen(LOGS_DIR.'/application.log', 'ab');
$STDERR = fopen(LOGS_DIR.'/error.log', 'ab');
ini_set('error_log', LOGS_DIR.'/error.log');
/**
* Getting accounts info
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php');
/**
* Setting Delay
*/
$sleep = ! empty($argv[1]) ? $argv[1] : 1;
declare(ticks=1);
/*
*
* Без этой директивы PHP не будет перехватывать сигналы
* PHP >= 5.3.0 вместо declare(ticks = 1) надо бы использовать pcntl_signal_dispatch()
*
* #pcntl_signal(SIGTERM, "_process_callback");
* #pcntl_signal(SIGCHLD, "_process_callback");
* function _process_callback($signo)
* {
*
* }
*/
echo "Service started" . PHP_EOL;
while(TRUE)
{
foreach($config->accounts as $k => $account)
{
unset($output);
exec("ps aux | grep '{$account['imap']['login']}' | grep -v grep", $output);
if(count($output) < 1)
{
echo PHP_EOL;
echo "Child: {$account['imap']['login']}".PHP_EOL;
$pid = pcntl_fork();
if ($pid == -1)
{
error_log('Could not launch new job, exiting');
}
elseif ($pid)
{
/**
* Parent code execution
*/
echo "PID: $pid".PHP_EOL;
}
else
{
/**
* Child code execution (running parser)
*/
$run_cmd_arr = array();
$run_cmd_arr[] = 'php parser.php';
$run_cmd_arr[] = $account['imap']['host'];
$run_cmd_arr[] = $account['imap']['login'];
$run_cmd_arr[] = $account['imap']['password'];
$run_cmd_arr[] = '>>' . LOGS_DIR . DIRECTORY_SEPARATOR . $account['imap']['login'] . '.txt';
$run_cmd_arr[] = '2>>' . LOGS_DIR . DIRECTORY_SEPARATOR . $account['imap']['login'] . '.txt';
$run_cmd_str = implode(' ', $run_cmd_arr);
exec($run_cmd_str);
exit;
}
}
else
{
echo "Exists: {$account['imap']['login']}".PHP_EOL;
}
}
/**
* Waiting all processes die
*/
/*
foreach($config->accounts as $k => $account)
{
pcntl_wait($status);
}
*/
/**
* Counting metrics
*/
$memory_usage = round(memory_get_usage(TRUE) / 1024) . 'KB';
#echo 'Memory usage: ' . $memory_usage . '/' . MEMORY_LIMIT . 'KB'. PHP_EOL;
$time_now = microtime(1);
$time_elapsed = round($time_now-$time_start,2);
#echo 'Time elapsed: ' . $time_elapsed . '/' . TIME_LIMIT . PHP_EOL;
if($memory_usage > MEMORY_LIMIT || $time_elapsed > TIME_LIMIT)
{
echo 'Memory usage: ' . $memory_usage . '/' . MEMORY_LIMIT . 'KB'. PHP_EOL;
echo 'Time elapsed: ' . $time_elapsed . '/' . TIME_LIMIT . PHP_EOL;
exec('nohup php ' . __FILE__ . ' ' . $sleep . '&', $output);
echo implode(PHP_EOL, $output) . PHP_EOL;
break;
exit;
}
sleep($sleep);
}
?>