maksslesarenko / emdrive
此包提供了一种处理循环命令工作者的方法
Requires
- php: >7.0
- symfony/framework-bundle: >4.0
- symfony/lock: >4.0
This package is not auto-updated.
Last update: 2024-09-20 04:13:11 UTC
README
Emdrive
此包提供了一个无限期运行的内建服务,允许按日或按间隔触发执行 Symfony\Component\Console\Command\Command
安装
打开命令控制台,进入您的项目目录,并执行以下命令以下载此包的最新版本
composer require maksslesarenko/emdrive
配置
emdrive:
server_name: main # incase app is running on multiple servers this has to be unique
pool_size: 5 # number of simultanious commands allowed to be execute
tick_interval: 2000000 # interval in microseconds between service loop checks
cmd_start: bin/console %s -vv -e prod > /dev/null 2>&1 & # template for scheduled commands to be executed with
cmd_kill: kill -2 %s > /dev/null 2>&1 & # template for scheduled commands to be stopped with
storage:
dsn: '%env(DATABASE_URL)%' # dsn for mysql or sqlite database to store schedule
lock: emdrive_lock # lock name that is configured in framework section
pid_dir: var/pid # directory to store process ids for executed commands
log_dir: var/elog # directory to store logs
lock_dir: var/lock # directory to store lock files
framework:
lock:
enabled: true
resources:
emdrive_lock: flock
#emdrive_lock: [flock, 'memcached://'] # incase multiple servers are used to run app
命令
bin/console emdrive:deploy
# 主部署命令,检测其他部署命令并按顺序执行它们以部署包
bin/console emdrive:deploy:update-schedule
# 更新计划的部署命令
bin/console emdrive:configure-schedule [<name>]
# 更改命令计划时间/间隔
bin/console emdrive:service:run
# 启动服务
bin/console emdrive:service:stop
# 停止服务
bin/console emdrive:service:status [--watch]
# 检查服务是否正在运行
bin/console emdrive:dump-schedule
# 将计划输出到控制台
示例
+----+--------------------------+-------------+---------+---------------------+---------------------+---------------+----------------+
| Id | Name | Server name | Status | Last start at | Next start at | Schedule type | Schedule value |
+----+--------------------------+-------------+---------+---------------------+---------------------+---------------+----------------+
| 1 | first-scheduled-command | main | stopped | 2018-04-06 23:19:01 | 2018-04-06 23:20:01 | interval | 1 minutes |
| 2 | second-scheduled-command | main | stopped | 2018-04-06 23:05:00 | 2018-04-07 23:05:00 | time | 23:05 |
+----+--------------------------+-------------+---------+---------------------+---------------------+---------------+----------------+
创建计划命令
use Emdrive\Command\ScheduledCommandInterface;
use Emdrive\InterruptableExecutionTrait;
use Emdrive\LockableExecutionTrait;
use Symfony\Component\Console\Command\Command;
class FisrtScheduledCommand extends Command implements ScheduledCommandInterface
{
use LockableExecutionTrait;
use InterruptableExecutionTrait;
public function configure()
{
$this->setName('first-scheduled-command');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
...
foreach ($collection as $item) {
if ($this->isInterrupted()) {
break;
}
...
}
}
}
创建新的计划命令后,运行部署
bin/console emdrive:deploy
然后配置计划
bin/console emdrive:configure-schedule first-scheduled-command
Supervisord 配置
[program:emdrive]
command=/code/bin/console emdrive:service:run
stderr_logfile = /var/log/supervisor/emdrive.error.log
stdout_logfile = /var/log/supervisor/emdrive.log
stopsignal = INT
许可证
此包在 MIT 许可证下发布。