Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
CREATE TABLE `ProcessQueue` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`subscriber_id` int(10) NOT NULL DEFAULT '',
`error_count` smallint(6) NOT NULL DEFAULT '0',
`parameters` blob NOT NULL,
`status` enum('QUEUED','LOCKED','FAILED','DELETED') NOT NULL DEFAULT 'QUEUED',
`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `updated` (`updated`,`subscriber_id`,`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
$PQ = new ProcessQueue($subscriber_id);
$element = false;
$error = false;
while ($element = $PQ->getNextElement($element, $error)) {
// ... process $element ...
if (error occurred) {
$error = true;
} else {
$error = false;
}
}
UPDATE ProcessQueue SET error_count = error_count + 1, STATUS = IF(error_count>5, 'FAILED', 'QUEUED') WHERE updated < $время_минут_15_назад AND STATUS='LOCKED'
$ crontab -e * * * * * /path/to/script1.php * * * * * /path/to/script2.php
class Script extends Application {
// ...........
protected function checkPid() {
$this->pid = posix_getpid();
$pid = (int) @file_get_contents($this->pidfile);
if ($pid) {
if (is_dir('/proc') && is_dir('/proc/curproc')) { // use procfs
$have_pid = is_dir('/proc/'.$pid);
} else { // if no procfs, use /bin/ps. warning: BSD-specific implementation
$output = array();
exec('/bin/ps -auxwwwp '.$pid, $output);
$have_pid = !empty($output[1]);
}
if ($have_pid) {
$this->logScriptEvent(self::EVENT_WARNING, 'already running with pid '.$pid. ', exiting');
exit(1);
}
}
try {
file_put_contents($this->pidfile, $this->pid . "\n");
} catch (PhpException $e) {
$this->logScriptEvent(self::EVENT_WARNING, 'could not write pidfile: '.$e->getMessage());
}
return true;
}
// ...........
function run() {
if (!$this->checkPid()) {
$this->logScriptEvent(self::EVENT_NOTICE, 'the script ' . get_class($this) . ' is already running');
exit(0);
}
// ....
}
}
srv01 ~$ php -r 'var_dump(posix_kill(posix_getpid(),0));' bool(true) srv01 ~$ php -r 'var_dump(posix_kill(33333,0));' bool(false)
Краткий обзор MQ (Messages queue) для применения в проектах на РНР. Часть 1