Download the PHP package cesurapp/swoole-bundle without Composer
On this page you can find all versions of the php package cesurapp/swoole-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cesurapp/swoole-bundle
More information about cesurapp/swoole-bundle
Files in cesurapp/swoole-bundle
Package swoole-bundle
Short Description Symfony Swoole Bundle
License MIT
Homepage https://github.com/cesurapp/swoole-bundle
Informations about the package swoole-bundle
Symfony Swoole Bundle
Built-in Swoole http server, background jobs (Task), scheduled task (Cron) worker are available. Failed jobs are saved in the database to be retried. Each server has built-in background task worker. Scheduled tasks run simultaneously on all servers. It is not possible for tasks to run at the same time as locking is used.
Install
Required Symfony 8
Edit: public/index.php
Configuration:
Server Environment: .env
Server Commands
The running server keeps its master process id in var/swoole.pid. server:stop sends it SIGTERM
and waits while the running requests end (up to max_wait_time) and the task executors finish
theirs (up to SHUTDOWN_GRACE), then kills a server still up.
Create Cron Job
You can use cron expression for scheduled tasks, or you can use predefined expressions.
Notes:
- One scheduler process starts the jobs on time and gives every run a process of its own: a slow or blocking job (a long query, say) holds up only its own run, never the other jobs
- A run's process lives as long as the run and opens its own connections; the job runs in a coroutine, like in any worker
- A job whose previous run is still going is not started again; that run is skipped
TIMEOUT(default 1200 seconds) stops a run that takes longer; raise it for a longer job. The run's lock lastsTIMEOUTplus a minute, so no other server starts the job while it runs- Stopping the server stops the runs in progress
- The job's constructor runs in the scheduler: open connections in
__invoke(), never earlier
Task Workers
Tasks do not run in Swoole's task workers. Swoole has one max_wait_time for its HTTP and task
workers alike, so a long task was cut off with the HTTP workers' limit. They run in processes the
bundle adds to the server itself (Server::addProcess), which Swoole never reloads: max_wait_time
applies to the HTTP workers only.
- Task broker, one process: takes every dispatched task without making the caller wait, writes it
to
var/durable/queue.logand hands it to an executor. Waiting tasks survive a restart, a crash or a deploy: the broker reads them back on start. Each process (HTTP worker, executor, cron run) keeps one connection to it for all its tasks. When the broker can't be reached (it is restarting) or does not read within half a second, a task is appended tovar/durable/queue.logdirectly and the broker picks it up. - Deploys: to keep the waiting tasks across a deploy, put only
var/durable/on a volume, never all ofvar/: its compiled container cache andswoole.pidbelong to one image, and the new code would boot with the old container. A server needs a directory of its own (two brokers on onequeue.logcorrupt it: no replicas, nor a deploy that starts the new container before the old one stops, on one volume). It must be writable by the app user, on a local disk. - Executors,
WORKER_NUMprocesses: each runs up toCONCURRENCYtasks at once, in coroutines. An executor runs for as long as it stays underMAX_MEMORY. When a task leaves it above that, it takes no more tasks, lets the running ones finish and exits, and the server starts a new one. - Hung tasks: an executor arms a kernel alarm for
MAX_EXECUTION_TIMEseconds whenever it takes a task, and turns it off while it runs none. Every task gets at least that long from its start, so set it above the longest task. An executor that runs tasks but takes none for that long is taken as hung, and the alarm kills it wherever it is stuck: in a PHP loop or in a blocking call. - Frozen executors: each executor pings the broker every second. One that goes quiet for a few seconds (frozen, or held up by a blocking call) gets no more tasks until it answers again.
- EntityManager: a failed flush closes the EntityManager. A closed one is reset before the next task starts, so one task's failure does not fail the ones after it.
- Server stop: the HTTP workers end first (up to
max_wait_time), then the executors haveSHUTDOWN_GRACEseconds to finish their tasks. Waiting tasks stay invar/durable/queue.log. - A task that dies with its executor (hung, crashed, cut off by a stop) does not run again, unless it is durable.
- Keep PHP's
memory_limitat least twiceMAX_MEMORY, or -1.
Create Task (Background Job or Queue)
Data passed to tasks must be serializable (string, int, bool, array). Objects cannot be serialized directly.
Create Task:
Dispatch Task:
Durable Task:
Waiting tasks survive a restart in var/durable/queue.log, but a task that dies with its executor is lost.
Pass durable: true for work that must survive a deploy or a crash. The task is written to the
failed_task store before it is queued, and its row is deleted only once the task succeeds.
FailedTaskCron runs it again after a failure (on the task_retry schedule) and after
task_redeliver_timeout if its executor died.
- A durable task runs at least once, so make it idempotent.
- A durable task never runs inline in the caller. When it can't be queued right away it waits for
FailedTaskCron. That happens when the task broker can't be reached, or when the dispatch is inside an open database transaction, where a worker could not yet see its row. - In sync mode (
task_sync_mode, tests) it runs inline like any other task and writes no row.
Create Process Worker
Process Worker allows you to create continuously running tasks in a separate process when the server starts. It's ideal for Redis LISTEN, Postgres LISTEN, or similar continuous listening commands.
Features:
- Each process runs as a separate, server-managed Swoole Process (
Server::addProcess) - Automatic restart support when the process completes
- Configurable restart delay
- Enable/Disable support
- One running copy across instances (lock), with the other instances on standby as failover
- Can dispatch tasks like any worker
- Graceful stop: with
$STOP_TIMEOUTset, a SIGTERM lets the job finish the work in hand (isStopping(),pause()) before the process ends
Configuration:
Or via environment variable:
Create Process Job:
Use ProcessInterface or extend AbstractProcessJob:
Postgres LISTEN Example:
One-Time Process (Without Restart):
Notes:
- Each process runs as a separate Swoole Process, isolated from each other
- Processes are registered with
Server::addProcess: they start with the server, the server restarts one that exits or crashes, andTaskHandler::dispatch()works inside them - One copy runs per job across all instances (the
process_server_<FQCN>lock). The other instances' copies wait on standby and take over when that lock is released - Use a PostgreSQL advisory lock for it:
LOCK_DSN=postgresql+advisory://..., connected directly (PgBouncer's transaction pooling hands the lock's session to other clients, so two copies could run). It never expires, however long the job holds up its process, and drops with the process. With a store whose locks expire (Redis, a plainpostgresql://table) the lock lasts 60 seconds past its last refresh, so a job that blocks its process longer lets a standby copy start - A stop releases the lock at once. Each copy checks its lock every 10 seconds; one that lost it (e.g. its database session dropped) stops and the server restarts it
- A copy that takes the lock over from another waits 15 seconds before starting the job, so the other has noticed a lost lock and stopped
- When
RESTART=true, the job runs again afterRESTART_DELAYseconds upon completion (an exception or error counts as completion) - When
RESTART=false, the finished process stays parked while the server runs: exiting would only have the server restart it and run the job again - The job's constructor runs in the master process (to read
ENABLE): open connections in__invoke(), never earlier - Processes must implement
ProcessInterface(or extendAbstractProcessJob) - Automatically registered in Symfony DI container with lazy loading support
Requirements
- PHP >= 8.4
- Symfony 8+
- Swoole Extension
- POSIX Extension
- PCNTL Extension
License
MIT
All versions of swoole-bundle with dependencies
ext-posix Version *
ext-pcntl Version *
ext-swoole Version *
symfony/dependency-injection Version ^8.1
symfony/http-kernel Version ^8.1
symfony/framework-bundle Version ^8.1
symfony/runtime Version ^8.1
symfony/console Version ^8.1
dragonmantank/cron-expression Version ^3.3
symfony/lock Version ^8.1
symfony/dotenv Version ^8.1
symfony/http-client-contracts Version ^3.7