| Package Data | |
|---|---|
| Maintainer Username: | DeSmart |
| Maintainer Contact: | buka@desmart.com (Michał Uberman) |
| Package Create Date: | 2017-02-14 |
| Package Last Update: | 2017-04-10 |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-11-02 15:01:49 |
| Package Statistics | |
|---|---|
| Total Downloads: | 814 |
| Monthly Downloads: | 0 |
| Daily Downloads: | 0 |
| Total Stars: | 2 |
| Total Watchers: | 5 |
| Total Forks: | 0 |
| Total Open Issues: | 0 |
This package allows for easily temporarily locking your scripts execution.
It might come in handy in cases such as CRON jobs that connect with unreliable APIS, where you're not 100% sure if your script won't fail at some point.
This package requires:
$ composer require desmart/laravel-padlock
DeSmart\Padlock\ServiceProvider to your config/app.php: /*
* Package Service Providers...
*/
DeSmart\Padlock\ServiceProvider::class,
$ php artisan vendor:publish --provider="DeSmart\Padlock\ServiceProvider"
config/padlock.php - choose between Database and Filesystem driverThis package's purpose is to protect your script from being run on multiple threads.
It is useful for long-time backend jobs such as handling queries, or harvesting data from external APIs.
class FooCommand extends \Illuminate\Console\Command
{
protected $signature = 'foo:bar';
protected $description = 'Foo command utilizing Padlock';
/** @var PadlockHandler */
private $padlockHandler;
const PADLOCK_SCRIPT = 'FooCommand';
/** 30 seconds padlock time to live - after that your padlock will be unlocked */
const PADLOCK_TTL = 30;
/**
* FooCommand constructor.
* @param \DeSmart\Padlock\PadlockHandler $padlockHandler
*/
public function __construct(\DeSmart\Padlock\PadlockHandler $padlockHandler)
{
parent::__construct();
$this->padlockHandler = $padlockHandler;
}
public function handle()
{
if (true === $this->padlockHandler->isLocked(self::PADLOCK_SCRIPT, self::PADLOCK_TTL)) {
echo "Padlock exists, script locked." . PHP_EOL;
return;
}
$this->padlockHandler->lock(self::PADLOCK_SCRIPT);
// do your stuff
$this->padlockHandler->unlock(self::PADLOCK_SCRIPT);
}
}