cesurapp / swoole-bundle
Symfony Swoole Bundle
1.0.11
2024-09-26 18:50 UTC
Requires
- php: >=8.2
- ext-swoole: *
- dragonmantank/cron-expression: ^3.3
- symfony/console: ^7.1
- symfony/dependency-injection: ^7.1
- symfony/dotenv: ^7.1
- symfony/framework-bundle: ^7.1
- symfony/http-client-contracts: ^3.5
- symfony/http-kernel: ^7.1
- symfony/lock: ^7.1
- symfony/runtime: ^7.1
Requires (Dev)
- doctrine/doctrine-bundle: ^2.11
- doctrine/orm: ^2.17
- php-cs-fixer/shim: ^3.40
- phpstan/phpstan: ^1.10
- symfony/process: ^7.1
- symfony/test-pack: ^1.1
- symfony/uid: ^7.1
README
内置 Swoole http 服务器,后台任务(Task),定时任务(Cron)工作进程可用。失败的任务将保存到数据库中以便重试。每个服务器都内置了后台任务工作进程。定时任务在所有服务器上同时运行。由于使用了锁定,任务不能同时运行。
安装
所需 Symfony 7
composer req cesurapp/swoole-bundle
编辑:public/index.php
... require_once dirname(__DIR__).'/vendor/cesurapp/swoole-bundle/src/Runtime/entrypoint.php'; require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; ...
配置
# config/packages/swoole.yaml swoole: entrypoint: public/index.php watch_dir: /config,/src,/templates watch_extension: *.php,*.yaml,*.yml,*.twig replace_http_client: true # Replate Symfony HTTP Client to Swoole Client cron_worker: true # Enable Cron Worker Service task_worker: true # Enable Task Worker Service task_sync_mode: false # Enable SYNC Mode -> Default false failed_task_retry: '@EveryMinute10' failed_task_attempt: 2 # Failed Task Retry Count
服务器环境:.env
#SERVER_WORKER_CRON=true # Run Cron Worker -> Default = True #SERVER_WORKER_TASK=true # Run Task Worker -> Default = True SERVER_HTTP_HOST=127.0.0.1 # Default = 0.0.0.0 SERVER_HTTP_PORT=9090 # Default = 80 #SERVER_HTTP_SETTINGS_WORKER_NUM=2 # Default = CPU Count #SERVER_HTTP_SETTINGS_TASK_WORKER_NUM=1 # Default = CPU Count / 2 #SERVER_HTTP_SETTINGS_LOG_LEVEL=4 # Details Openswoole\Constant LOG_LEVEL
服务器命令
# Cron Commands bin/console cron:list # List cron jobs bin/console cron:run AcmeCron # Run cron process one time, without locking. # Server Commands bin/console server:start # Start http,cron,queue server bin/console server:stop # Stop http,cron,queue server bin/console server:status # Status http,cron,queue server bin/console server:watch # Start http,cron,queue server for development mode (file watcher enabled) # Task|Job Commands bin/console task:list # List registered tasks bin/console task:failed:clear # Clear all failed task bin/console task:failed:retry # Forced send all failed tasks to swoole task worker bin/console task:failed:view # Lists failed tasks
创建 Cron 任务
您可以使用 cron 表达式为定时任务,或者使用预定义的表达式。
/** * Predefined Scheduling * * '@yearly' => '0 0 1 1 *', * '@annually' => '0 0 1 1 *', * '@monthly' => '0 0 1 * *', * '@weekly' => '0 0 * * 0', * '@daily' => '0 0 * * *', * '@hourly' => '0 * * * *', * '@EveryMinute' => 'w* * * * *', * "@EveryMinute5' => '*\/5 * * * *', * '@EveryMinute10' => '*\/10 * * * *', * '@EveryMinute15' => '*\/15 * * * *', * '@EveryMinute30' => '*\/30 * * * *',``` */ class ExampleJob implements \Cesurapp\SwooleBundle\Cron\AbstractCronJob { /** * @see AbstractCronJob */ public string $TIME = '@EveryMinute10'; /** * Cron is Enable|Disable. */ public bool $ENABLE = true; /** * Cron Context */ public function __invoke(): void { } }
创建任务(后台任务或队列)
传递给任务的数据类型必须是字符串、int、bool,对象不能序列化。
创建
class ExampleTask implements \Cesurapp\SwooleBundle\Task\TaskInterface { public function __invoke(object|string $data = null): void { var_dump( $data['name'], $data['invoke'] ); } }
处理任务
public function hello(\Cesurapp\SwooleBundle\Task\TaskHandler $taskHandler) { $taskHandler->dispatch(ExampleTask::class, [ 'name' => 'Test', 'invoke' => 'Data' ]); }