bprs / commandline-bundle
此包已被废弃,不再维护。未建议替代包。
允许在后台工作进程执行命令(任务)。这是chrisboulton php-resque、vend/resque和其他功能的混合体。
此包尚未发布版本,信息很少。
README
特别感谢
如果没有许多开发者的出色工作,这个包将不可能实现。特别是php-resque库(https://github.com/chrisboulton/php-resque)背后的那些人和vend/resque包(https://github.com/vend/php-resque)的人!
关于
此包允许symfony项目在后台执行长时间运行的任务(任务)(例如视频编码、从云服务传输大文件等)。任务是容器感知的,使用monolog记录信息,可以从网页或命令行启动(和停止)工作进程。
此库需要一个运行的redis数据库和连接。
安装
composer require bprs/commandline-bundle
$bundles = array( ... new Bprs\CommandLineBundle\BprsCommandLineBundle(), ...
您需要redis和redis php扩展。
配置
# app/config/config.yml bprs_command_line: php_path: #path to the php that should be used to launch a worker. default: php worker_queue: #the default queue for jobs and worker to use. should be unique. default: * redis_backend: #the connection url. default: tcp://:6379
使用
启动redis
您需要一个运行的redis数据库。
app/console bprs:commandline:start_worker QUEUENAME (--interval=5)
#starts a worker listening to the given queuename in given interval
#you can follow the log in apps/log/dev.log or prod.log
app/console bprs:commandline:stop_worker (--force)
#stops all worker after they finish the current job (graceful stop)
# --force will use kill -9
app/console bprs:commandline:add_job JOBNAME QUEUENAME argument1:arg, argument2:arg, ... argumentN:arg
#schedules a given job in a given queue with an array of given arguments
#example
app/console bprs:commandline:add_job Bprs\\CommandlineBundle\\Model\\GreetJob default name:BPRS
app/console bprs:commandline:list_jobs --queue=QUEUE
#lists all jobs in given queue. You can use the indexnumber and stop_job to remove a job from the queue
app/console bprs:commandline:list_worker
# lists all known worker. Can contain ungraceful stopped worker as well.
高级使用
当然,您可以使用POSIX可靠信号来操作所有工作进程。
app/console bprs:commandline:list_worker
列出此机器上的所有工作进程。还列出进程ID。(pid)您现在可以操作这些pid
#stops a specific worker (gracefull stop)
kill -QUIT pid
#stops a specific worker. (ungracefull stop)
kill -9 pid
#see php-resque worker
pcntl_signal(SIGUSR1, array($this, 'killChild'));
pcntl_signal(SIGUSR2, array($this, 'pauseProcessing'));
pcntl_signal(SIGCONT, array($this, 'unPauseProcessing'));
pcntl_signal(SIGPIPE, array($this, 'reestablishRedisConnection'));
编写您自己的任务
常规任务
编写您的类。您可以使用关联数组"args"访问使用add_job命令提供的所有参数
namespace Your\Cool\Namespace; use Bprs\CommandlineBundle\Resque\AbstractJob; class YourJob extends AbstractJob { public function perform() { echo sprintf("your task to perform"); echo sprintf("your argument:%s", $this->args["yourKey"]); } }
要启动此过程,请执行以下操作
# start an worker for the queue defaultQueue
app/console bprs:commandline:start_worker defaultQueue
# add your job with your arguments to the defaultQueue
app/console bprs:commandline:add_job Your\\Cool\\Namespace\\YourJob defaultQueue yourKey:yourArgument
或使用服务
$this->get('bprs_jobservice')->addJob( "Your\\Cool\\Namespace\\YourJob", //your job class ["your" => "arguments"], //your payload arguments $onTopOfTheQueue, //if you want to enqueue as next job (skip list) $monitorMe //if you want detailed information for this job (more soon) );
如果一切按预期进行,您应该在日志中找到(app/logs/dev.log或prod.log)
...
[2017-04-12 15:37:50] app.NOTICE: Registered signals [] []
[2017-04-12 15:37:50] app.DEBUG: Registering worker yourmachine:5191:yourqueue [] []
[2017-04-12 15:37:50] app.DEBUG: Attempting to reserve job from YOURQUEUE {"queues":"yourqueues"} []
...
容器感知任务
您还可以扩展Bprs\CommandLineBundle\Model\BprsContainerAwareJob
并使用以下方式获取服务容器
$this->getContainer()
在您的任务中。