white-poto / php_crontab Goto Github PK
View Code? Open in Web Editor NEWA crontab written in PHP based on pcntl and react/event-loop
License: Apache License 2.0
A crontab written in PHP based on pcntl and react/event-loop
License: Apache License 2.0
错误如下:
[root@localhost example]# php http_daemon.php
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Jenner\Crontab\HttpDaemon::crontabCallback() must be an instance of Jenner\Crontab\Crontab, instance of React\EventLoop\Timer\Timer given in /root/vendor/jenner/crontab/src/Jenner/Crontab/HttpDaemon.php:58
Stack trace:
#0 [internal function]: Jenner\Crontab\HttpDaemon->crontabCallback(Object(React\EventLoop\Timer\Timer))
#1 /root/vendor/jenner/crontab/vendor/react/event-loop/Timer/Timers.php(90): call_user_func(Array, Object(React\EventLoop\Timer\Timer))
#2 /root/vendor/jenner/crontab/vendor/react/event-loop/StreamSelectLoop.php(177): React\EventLoop\Timer\Timers->tick()
#3 /root/vendor/jenner/crontab/src/Jenner/Crontab/HttpDaemon.php(52): React\EventLoop\StreamSelectLoop->run()
#4 /root/vendor/jenner/crontab/example/http_daemon.php(34): Jenner\Crontab\HttpDaemon->start()
#5 {main}
thrown in /root/vendor/jenner/crontab/src/Jenner/Crontab/HttpDaemon.php on line 58
Call to undefined function Jenner\SimpleFork\pcntl_signal()
错误位置
FILE: /home/wwwroot/vendor/jenner/simple_fork/src/Process.php LINE: 234
PHP Notice: Undefined index: help: in /data/app/task/php_crontab/bin/crontab.php on line 193
PHP Notice: Undefined index: config in /data/app/task/php_crontab/bin/crontab.php on line 191
PHP Notice: Undefined index: port in /data/app/task/php_crontab/bin/crontab.php on line 191
PHP Notice: Undefined index: pid-file in /data/app/task/php_crontab/bin/crontab.php on line 191
PHP Notice: Undefined index: log in /data/app/task/php_crontab/bin/crontab.php on line 191
PHP Catchable fatal error: Argument 1 passed to Jenner\Crontab\HttpDaemon::crontabCallback() must be an instance of Jenner\Crontab\Crontab, instance of React\EventLoop\Timer\Timer given in /data/app/task/php_crontab/src/Jenner/Crontab/HttpDaemon.php on line 58
Process默认是60,不过有些Crontab可能执行时期较长,目前我是手动在Misssion里改成0,最好能够在外部指定
/**
* start mission process
*/
public function run()
{
$out = $this->out;
$err = $this->err;
// Todo 最后一个参数代表超时,默认为60,改成0
$process = new \Symfony\Component\Process\Process($this->cmd, null, null, null ,0);
$process->run(function ($type, $buffer) use ($out, $err) {
if ($type == \Symfony\Component\Process\Process::ERR) {
$err->error($buffer);
} else {
echo "run task:" . $this->name() . "\n";
$out->info($this->name);
$out->info($buffer);
}
});
}
其他扩展要求monolog版本^1.2,你这个是固定1.17,有冲突
请问,代码中已经设置了out参数的值,代码如下:
$missions = [
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'time' => '*/1 * * * *',
'user' => 'daemon',
'group' => 'daemon'
]
];
$daemon = new \Jenner\Crontab\Daemon($missions);
$daemon->start();
如题
如题
最新版本找不到Jenner\SimpleFork\Process类,麻烦上传下,谢谢
请更新一下。
看你的demo,out参数对应的应该是脚本执行日志吧,getProtocol中做了如下处理:
/**
*
* @param $stream
* @return mixed
*/
protected static function getProtocol($stream)
{
if(strpos($stream, 'unix') === 0){
return 'unix';
}
$stream_info = parse_url($stream);
if (!array_key_exists('scheme', $stream_info)) {
throw new \InvalidArgumentException("stream format error");
}
return $stream_info['scheme'];
}
这样一来就抛异常了呀
修改如下,不知道合适不合适
/**
* start mission process
*/
public function run()
{
// set user and group
$this->setUserAndGroup();
$out = $this->out;
$err = $this->err;
$process = new \Symfony\Component\Process\Process($this->cmd);
$process->run(function ($type, $buffer) use ($out, $err) {
if ($type == \Symfony\Component\Process\Process::ERR) {
$err->error($buffer);
} else {
$out->info($buffer);
}
});
}
只是个建议
在以daemon方式运行时,Mission是在start方法的定时器外部建立的,后续循环无法动态修改,建议在定时器内部建立,这样当Mission有变动时,不需要重启daemon
/**
* start crontab and loop
*/
public function start()
{
$this->logger->info("crontab start");
$crontab = $this->createCrontab(); // 外部建立Mission
$loop = Factory::create();
// add periodic timer
$loop->addPeriodicTimer(60, function () use ($crontab, $loop) {
$loop->addTimer(60 - time() % 60, function () use ($crontab) {
$pid = pcntl_fork();
if ($pid > 0) {
return;
} elseif ($pid == 0) {
$crontab->start(time());
exit();
} else {
$this->logger->error("could not fork");
}
});
});
//例如
$loop->addPeriodicTimer(60, function () use ($loop) {
$loop->addTimer(60 - time() % 60, function() {
// get missions from outside
$tasks = call_user_func($this->mission_callback);
$this->setTasks($tasks);
$crontab = $this->createCrontab();
$pid = pcntl_fork();
if ($pid > 0) {
return;
} elseif ($pid == 0) {
$crontab->start(time());
exit();
} else {
$this->logger->error("could not fork");
exit();
}
});
});
P.s 好像Daemon.php是ANSI编码
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.