PHPackages                             mvdstam/graceful-laravel-workers - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. mvdstam/graceful-laravel-workers

AbandonedLibrary[Queues &amp; Workers](/categories/queues)

mvdstam/graceful-laravel-workers
================================

Provides a way to stop Laravel Worker daemons (and jobs) gracefully.

1.0.0(9y ago)135.9k[2 issues](https://github.com/mvdstam/graceful-laravel-workers/issues)MITPHPPHP &gt;=5.6.0

Since Jan 12Pushed 9y ago4 watchersCompare

[ Source](https://github.com/mvdstam/graceful-laravel-workers)[ Packagist](https://packagist.org/packages/mvdstam/graceful-laravel-workers)[ RSS](/packages/mvdstam-graceful-laravel-workers/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (2)Used By (0)

Graceful Laravel Workers
========================

[](#graceful-laravel-workers)

Stopping your Workers and Jobs without hurting your application.

In many modern webapplications, many tasks are handled asynchronously through the use of some kind of queueing system and jobs. In Laravel and Lumen-based applications, this is possible through the use of the `illuminate/queue` component. This package expands a little bit upon that component by making it possible to *gracefully* stop Workers (or any kind of process for that matter) through the use of [Posix Signals](https://en.wikipedia.org/wiki/Unix_signal). This also enables your queue workers to be run inside of [Docker containers](https://www.docker.com/) and be stopped in a graceful and clean way, since Docker utilizes posix signals to stop running containers when, for example, updating them.

Finally, gracefully cleaning up running processes is a crucial part of any [12-factor application](https://12factor.net/).

### Why is this important?

[](#why-is-this-important)

When a queue worker is started through `php artisan queue:work --daemon`, a *long-running* PHP process is started. This process will run endlessly until it is stopped by either external sources (posix signals) or internal sources (the process crashes or simply stops because of an `exit;`).

Usually, these PHP processes are stopped externally by sending `SIGTERM` or `SIGINT` signals to the underlying processes. The default behaviour of PHP is to stop the process **immediately**, which means that any kind of important process is interrupted and possibly your data is lost. By *trapping* these signals with the use of the [PCNTL](http://php.net/manual/en/book.pcntl.php) extension, we can *catch* these signals and define custom behaviour when this happens. This is especially interesting when running queue workers within Docker containers, since containers are ephemeral by design and can (should) be replaced at any time.

### shutting\_down()

[](#shutting_down)

The most important aspect of this package is the function `shutting_down()`. In your long-running jobs, simply call `shutting_down()` in each iteration. This function returns `TRUE` when a signal has been caught and your application should prepare to shut down immediately. For example:

```
