Cron layer - main/w2/schedule.php
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | <?php # For Licence and Disclaimer of this code, see http://coresky.net/license class Schedule { private $mode_pod = false; # POpen Disabled by security reasons, exec all in one process private $started = 0; private $arg = -1; private $amp = ''; private $i = 0; private $func = []; private $handle = []; private $stdout = []; private $now = []; private $php = 'php'; const TPL = '%s, execution time: %01.3f sec, SQL nums in cron tasks: %d'; const UPD = 'update $_memory set tmemo=substr(concat(%s,%s,tmemo),1,10000) where id=%d'; public $dt; public $imemo; function &load() { if (!isset(SKY::$mem['n'])) { list($this->dt, $this->imemo, $tmemo) = sqlf('-select dt, imemo, tmemo from $_memory where id=9'); SKY::ghost('n', $tmemo, 'update memory set dt=now(), tmemo=%s where id=9'); } return SKY::$mem['n'][3]; } function __get($name) { $ary = $this->load(); if ('www' == $name) { $ary = explode('~', $ary['www']); return DEV ? $ary[0] . '/' : $ary[1] . '/'; } return $ary[substr($name, 2)]; } function __set($name, $value) { if ('n_' == substr($name, 0, 2)) { $this->load(); SKY::save(substr($name, 2), $value, 'n'); } } function __construct() { if ('cli' != PHP_SAPI) return; 'WINNT' == PHP_OS or $this->php = PHP_BINDIR . '/php'; $this->mode_pod |= !function_exists('popen'); global $argv; register_shutdown_function([$this, 'shutdown']); if (isset($argv[1])) { if ('@' == $argv[1][0]) { $this->amp = '@'; $argv[1] = substr($argv[1], 1); } if (strlen($argv[1])) { $this->arg = $argv[1]; } } } function mail_error() { global $sky; list ($dt, $err) = sqlf('-select dt, tmemo from $_memory where id=4'); if (!$dt) return; sqlf('update $_memory set dt=null where id=4'); $mail = new Mail; $mail->add_html("<pre>$err</pre>"); return $mail->send($sky->s_email, 'error on production', 'nr@' . _PUBLIC); } function bot_google($ip) { # see https://support.google.com/webmasters/answer/80553 $host = gethostbyaddr($ip); if (!in_array($domain, ['googlebot.com', 'google.com'])) return false; return $ip == gethostbyname($host); } function at($schedule = '', $func = null) { # Minutes Hours Days Months WeekDays-0=sunday * or 12 or */3 or 1,2,3 global $sky; if (is_callable($schedule)) { $func = $schedule; $schedule = '*'; } if (-1 == $this->arg) { if (!$this->now) { # first at() $sql_tz = $sky->s_sql_tz; if ($this->ok('1') && ($tze = round((strtotime($now = date(DATE_DT)) - strtotime(sql('+select now()'))) / 3600))) { preg_match("/^([\-\+]\d):(\d\d)$/", $sky->s_sql_tz, $m); $sky->s_sql_tz = (($n = $m[1] + $tze) >= 0 ? '+' : '') . "$n:$m[2]"; sqlf(self::UPD, "$now: Summer time changed for MySQL", "\n", 2); } $online = sqlf('+select count(1) from $_visitors where dt_l > now() - interval %d minute', $sky->s_visit); if ($online != $sky->s_online || $sql_tz != $sky->s_sql_tz) { $sky->s_online = $online; SKY::here('s', false); } } if ($this->ok($schedule)) { if ($this->mode_pod) { $func(); } else { global $argv; $this->handle[$this->i] = popen("$this->php $argv[0] $this->amp$this->i 2>&1", 'r'); $this->stdout[$this->i] = ''; } $this->started++; } } elseif ($this->i == $this->arg) { $func(); } $this->i++; return $this; } function ok($schedule = '') { if (!$this->now) { $ary = getdate(); $this->now = [$ary['minutes'], $ary['hours'], $ary['mday'], $ary['mon'], $ary['wday']]; } if ('-' == @$schedule[0]) return false; if ('+' == @$schedule[0]) return true; $in = preg_split("/\s+/", $schedule); for ($i = 0; $i < 5; $i++) { if (!isset($in[$i]) || $in[$i] === '*' || $in[$i] === '') { continue; } if (isset($in[$i][1]) && $in[$i][1] == '/') { if ($this->now[$i] % substr($in[$i], 2) == 0) continue; return false; } $ret = false; foreach (explode(',', $in[$i]) as $d) { if ($d == $this->now[$i]) $ret = true; } if (!$ret) return false; } return true; } function sql() { $start = microtime(true); $sql = (string)call_user_func_array('qp', func_get_args()); $n = sql(1, $sql); $this->write(sprintf("%01.3f sec <= %s <= %s", microtime(true) - $start, is_array($n) ? count($n) : $n, $sql)); return $n; } function write($str, $task = -1, $is_error = false) { if ($unk = -1 == $task) $task = $this->mode_pod ? $this->i : $this->arg; $date = date(DATE_DT); if (!$this->amp) { echo $unk ? "$str\n" : "$date [$task] $str\n"; } elseif ($is_error) { $time = sprintf("$date %01.3fs - cron task #$task:", microtime(true) - START_TS); sqlf(self::UPD, sprintf(span_b, "<b>$time</b>\n"), html("$str\n\n"), 4); } else { sqlf(self::UPD, "$date [$task] ", html("$str\n"), 2); } } function shutdown() { global $sky; if (-1 != $this->arg || $this->mode_pod) { # 2do: resolve if a task longer then 60 sec $this->n_cron_dt = sprintf(self::TPL, NOW, microtime(true) - START_TS, $sky->qn); if ($this->mode_pod && !$sky->was_error) $sky->debug = false; $sky->tailed or $sky->shutdown(); exit; } $write = $except = NULL; $finished = $time = 0; while ($this->handle) { $read = []; foreach ($this->handle as &$one) $read[] =& $one; $cnt = stream_select($read, $write, $except, 0, $time); if (false === $cnt) { $this->write("Error stream_select()", 'ALL', true); foreach ($this->handle as &$one) pclose($one); break; } elseif ($cnt) { foreach ($read as &$hdl) { $str = fread($hdl, 2096); if ('' !== $str) foreach ($this->handle as $i => &$one) if ($one == $hdl) $this->stdout[$i] .= $str; } $time = 0; # 0.1 sec } else { $time += 500000; # 0.5 sec if ($time > 3000000) $time = 3000000; # 3 sec } $eof = []; foreach ($this->handle as $i => &$one) { $eof[$i] =& $one; } foreach ($eof as $i => &$hdl) { if (feof($hdl)) { if ('' !== $this->stdout[$i]) $this->write($this->stdout[$i], $i, true); pclose($this->handle[$i]); $finished++; unset($this->handle[$i]); } } if ($this->started == $finished) break; # the end } $sky->was_error or $sky->debug = false; $this->started or $this->n_cron_dt = sprintf(self::TPL, NOW, microtime(true) - START_TS, $sky->qn); } } |