peppeocchi / php-cron-scheduler

PHP cron job scheduler
MIT License
808 stars 143 forks source link

Introduce Redis Support for Distributed Lock Management #148

Closed apoca closed 6 months ago

apoca commented 7 months ago

This Pull Request introduces the ability to use Redis for distributed lock management in the scheduler, enhancing its capability to prevent job overlap in distributed environments. While the existing file-based lock mechanism works well for single-instance deployments, modern cloud-based and containerized applications often require a more scalable solution. Redis, being a fast, in-memory data store, offers a robust and efficient way to manage locks across multiple instances.

Implementation Details

Usage Example:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
// Optional: $redis->auth('yourpassword');

$scheduler = new Scheduler([
    'redisClient' => $redis,
    'redisPrefix' => 'my_app_prefix:'
]);

$scheduler->php('script.php')->onlyOne();