spatie/laravel-signal-aware-command

Handle signals in artisan commands

Fund package maintenance!
spatie

Installs: 6 799 358

Dependents: 3

Suggesters: 0

Security: 0

Stars: 146

Watchers: 3

Forks: 9

Open Issues: 0

2.0.0 2024-02-05 13:37 UTC

This package is auto-updated.

Last update: 2024-11-05 15:16:37 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Using this package you can easily handle signals like SIGINT, SIGTERM in your Laravel app.

Here's a quick example where the SIGINT signal is handled.

use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    public function handle()
    {
        $this->info('Command started...');

        sleep(100);
    }

    public function onSigint()
    {
        // will be executed when you stop the command
    
        $this->info('You stopped the command!');
    }
}

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-signal-aware-command

Usage

In order to make an Artisan command signal aware you need to let it extend SignalAwareCommand.

use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    // your code
}

Handling signals

There are three ways to handle signals:

  • on the command itself
  • via the Signal facade
  • using the SignalReceived event

On the command

To handle signals on the command itself, you need to let your command extend SignalAwareCommand. Next, define a method that starts with on followed by the name of the signal. Here's an example where the SIGINT signal is handled.

use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';

    public function handle()
    {
        $this->info('Command started...');

        sleep(100);
    }

    public function onSigint()
    {
        // will be executed when you stop the command
    
        $this->info('You stopped the command!');
    }
}

Via the Signal facade

Using the Signal facade you can register signal handling code anywhere in your app.

First, you need to define the signals you want to handle in your command in the handlesSignals property.

use Spatie\SignalAwareCommand\SignalAwareCommand;

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';
    
    protected $handlesSignals = [SIGINT];

    public function handle()
    {
        (new SomeOtherClass())->performSomeWork();

        sleep(100);
    }
}

In any class you'd like you can use the Signal facade to register code that should be executed when a signal is received.

use Illuminate\Console\Command;
use Spatie\SignalAwareCommand\Facades\Signal;

class SomeOtherClass
{
    public function performSomeWork()
    {
        Signal::handle(SIGINT, function(Command $commandThatReceivedSignal) {
            $commandThatReceivedSignal->info('Received the SIGINT signal!');
        })
    }
}

You can call clearHandlers if you want to remove a handler that was previously registered.

use Spatie\SignalAwareCommand\Facades\Signal;

public function performSomeWork()
{
    Signal::handle(SIGNINT, function() {
        // perform cleanup
    });
    
    $this->doSomeWork();
    
    // at this point doSomeWork was executed without any problems
    // running a cleanup isn't necessary anymore
    Signal::clearHandlers(SIGINT);
}

To clear all handlers for all signals use Signal::clearHandlers().

Using the SignalReceived event

Whenever a signal is received, the Spatie\SignalAwareCommand\Events\SignalReceived event is fired.

To register which events you want to receive you must define a handlesSignals property on your command. Here's an example where we register listening for the SIGINT signal.

use Spatie\SignalAwareCommand\SignalAwareCommand

class YourCommand extends SignalAwareCommand
{
    protected $signature = 'your-command';
    
    protected $handlesSignals = [SIGINT];

    public function handle()
    {
        (new SomeOtherClass())->performSomeWork();

        sleep(100);
    }
}

In any class you'd like you can listen for the SignalReceived event.

use Spatie\SignalAwareCommand\Events\SignalReceived;
use Spatie\SignalAwareCommand\Signals;

class SomeOtherClass
{
    public function performSomeWork()
    {
        Event::listen(function(SignalReceived $event) {
            $signalNumber = $event->signal;
            
            $signalName = Signals::getSignalName($signalNumber);
        
            $event->command->info("Received the {$signalName} signal");
        });
    }
}

Learn how this package was built

The foundations of this pacakge were coded up in this live stream on YouTube.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.