This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
startworkers.php
82 lines (78 loc) · 3.04 KB
/
startworkers.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
<?php
ini_set('log_errors', 1);
ini_set('error_log', '/tmp/worker.log');
set_time_limit(0);
ignore_user_abort(1);
require 'vendor/autoload.php';
if (!isset($argv[2])) {
return 1;
}
$file = $argv[2];
$sfile = preg_replace(['|^pwr_|', '|^pwruser_|', '|_.*|'], '', basename($argv[2]));
ini_set('error_log', '/tmp/worker'.$sfile.'.log');
$lock = '/tmp/workerlock'.$sfile;
if (!file_exists($lock)) {
touch($lock);
}
$lock = fopen($lock, 'w+');
echo $sfile;
switch ($argv[1]) {
case 'start':
if (!preg_match('|'.$sfile.'|', shell_exec('tmux ls'))) {
shell_exec('tmux new-session -d -s '.escapeshellarg($sfile).' '.escapeshellarg('php '.$argv[0].' nuffink '.$file));
echo 'WORKER STARTED'.PHP_EOL;
}
return 0;
case 'stop':
error_log(shell_exec('tmux kill-session -t '.escapeshellarg($sfile)));
return 0;
case 'check':
echo flock($lock, LOCK_EX) ? 'STARTED' : 'STOPPED';
flock($lock, LOCK_UN);
return 0;
}
flock($lock, LOCK_EX);
$size = 0;
while (true) {
try {
clearstatcache();
if (filesize($file) !== $size) {
$MadelineProto = \danog\MadelineProto\Serialization::deserialize($file);
}
$MadelineProto->API->get_updates_difference();
$MadelineProto->API->store_db([], true);
$size = \danog\MadelineProto\Serialization::serialize($file, $MadelineProto);
usleep(250000);
} catch (\danog\MadelineProto\ResponseException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
} catch (\danog\MadelineProto\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
if (preg_match('/unserialize/', $e->getMessage())) {
return 1;
}
} catch (\danog\MadelineProto\RPCErrorException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
if (in_array($e->rpc, ['AUTH_KEY_UNREGISTERED', 'SESSION_REVOKED'])) {
foreach (glob($madeline.'*') as $file) {
unlink($file);
}
if (isset($MadelineProto)) {
$MadelineProto->session = null;
}
exit();
}
} catch (\danog\MadelineProto\TL\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
} catch (\PDOException $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
} catch (\Exception $e) {
error_log('Exception thrown: '.$e->getMessage().' on line '.$e->getLine().' of '.basename($e->getFile()));
error_log($e->getTraceAsString());
}
}
flock($lock, LOCK_UN);