-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cli.php
181 lines (165 loc) · 3.37 KB
/
Cli.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace core;
use prj\Shared;
/** Hack to clear lock on shutdown. */
class SyncShutdown
{
private $db;
private $key;
private $time;
public function __construct($db, $key)
{
$this->db = $db;
$this->key = $key;
$this->time = time();
}
public function __destruct()
{
$diff = time() - $this->time;
if ($diff > 60*5) {
error_log(sprintf("WARN: Cron(%s) took long, %s min!", $this->key, $diff/60));
}
$this->db->exec(
"UPDATE
cron_lock
SET
`expire` = 0
WHERE
`key` = ?",
[$this->key]
);
}
}
/** CLI Helpers */
class Cli
{
private static $_conf = null;
/** Please never call this func, it's used with init-cli.php */
public static function init(array $conf)
{
if (self::$_conf !== null) {
user_error("Cli::init() called twice?");
}
self::$_conf = $conf;
}
/** Ensure we're the only cron running this task */
public static function platform_lock($key)
{
global $EXITHACK;
// Sleep random so we're not racing on all machines.
$rand = mt_rand(1, 5);
msg("Random wait $rand sec");
sleep($rand);
// Attempt to acquire lock.
$db = Shared::db();
// TODO: Suppress lock error in log here?
$res = $db->exec(
"UPDATE
cron_lock
SET
`expire` = ?,
`server` = ?
WHERE
`key` = ?
AND
`expire` < ?",
[
strtotime("+1 hour"), gethostname(),
$key, time()
]
);
if ($res->rowCount() === 0) {
return false;
}
// We got the lock, now keep it till shutdown.
$EXITHACK = new SyncShutdown($db, $key);
return true;
}
/** Ask if user is sure */
public static function confirm($msg, $default = false)
{
// Display
{
$defaultMsg = "[y|N]";
if ($default) {
$defaultMsg = "[Y|n]";
}
echo "$msg $defaultMsg";
}
// Read
$ok = false;
{
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
echo "\n";
$cmp = strtolower(trim($line));
$ok = $cmp === "y";
if ($cmp === "") {
// Fallback if no value set
$ok = $default;
}
}
return $ok;
}
/** Show simple heading */
public static function heading($msg)
{
echo "\n$msg\n=====================\n";
}
/** Show aligned text. */
public static function align($key, $value)
{
$mask = "%15.15s | %s\n";
echo sprintf($mask, $key, $value);
}
public static function text($msg)
{
echo $msg . "\n";
}
public static function cli()
{
$ret = self::$_conf;
if ($ret === null) {
var_dump(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3));
user_error("Cli::cli() fail, init never called?");
}
return $ret;
}
public static function error($msg)
{
$fd = fopen('php://stderr', 'w+');
fwrite($fd, "$msg\n");
fclose($fd);
}
public static function can_write()
{
$cli = self::cli();
return isset($cli["flags"]["w"]) ? $cli["flags"]["w"] : false;
}
public static function exec($program, array $args = [])
{
$fds = [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
2 => ["pipe", "w"]
];
$cmd = $program;
foreach ($args as $arg) {
$cmd .= " " . escapeshellarg($arg);
}
$proc = proc_open($cmd, $fds, $pipes, null, null);
if ($proc === false) {
throw new \Exception("Failed calling proc_open");
}
fclose($pipes[0]); // close stdin
$out = [
"cmd" => $cmd,
"stdout" => stream_get_contents($pipes[1]),
"stderr" => stream_get_contents($pipes[2])
];
fclose($pipes[1]);
fclose($pipes[2]);
$out["exit"] = proc_close($proc);
return $out;
}
}