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!');
}
}
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.
You can install the package via composer:
composer require spatie/laravel-signal-aware-command
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
}
There are three ways to handle signals:
- on the command itself
- via the
Signal
facade - using the
SignalReceived
event
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!');
}
}
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()
.
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");
});
}
}
The foundations of this pacakge were coded up in this live stream on YouTube.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.