ActionScheduler.php
2 years ago
ActionScheduler_Abstract_ListTable.php
1 year ago
ActionScheduler_Abstract_QueueRunner.php
2 years ago
ActionScheduler_Abstract_RecurringSchedule.php
3 years ago
ActionScheduler_Abstract_Schedule.php
3 years ago
ActionScheduler_Abstract_Schema.php
2 years ago
ActionScheduler_Lock.php
2 years ago
ActionScheduler_Logger.php
3 years ago
ActionScheduler_Store.php
1 year ago
ActionScheduler_TimezoneHelper.php
1 year ago
ActionScheduler_Lock.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Abstract class for setting a basic lock to throttle some action. |
| 5 | * |
| 6 | * Class ActionScheduler_Lock |
| 7 | */ |
| 8 | abstract class ActionScheduler_Lock { |
| 9 | |
| 10 | /** @var ActionScheduler_Lock */ |
| 11 | private static $locker = NULL; |
| 12 | |
| 13 | /** @var int */ |
| 14 | protected static $lock_duration = MINUTE_IN_SECONDS; |
| 15 | |
| 16 | /** |
| 17 | * Check if a lock is set for a given lock type. |
| 18 | * |
| 19 | * @param string $lock_type A string to identify different lock types. |
| 20 | * @return bool |
| 21 | */ |
| 22 | public function is_locked( $lock_type ) { |
| 23 | return ( $this->get_expiration( $lock_type ) >= time() ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Set a lock. |
| 28 | * |
| 29 | * To prevent race conditions, implementations should avoid setting the lock if the lock is already held. |
| 30 | * |
| 31 | * @param string $lock_type A string to identify different lock types. |
| 32 | * @return bool |
| 33 | */ |
| 34 | abstract public function set( $lock_type ); |
| 35 | |
| 36 | /** |
| 37 | * If a lock is set, return the timestamp it was set to expiry. |
| 38 | * |
| 39 | * @param string $lock_type A string to identify different lock types. |
| 40 | * @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire. |
| 41 | */ |
| 42 | abstract public function get_expiration( $lock_type ); |
| 43 | |
| 44 | /** |
| 45 | * Get the amount of time to set for a given lock. 60 seconds by default. |
| 46 | * |
| 47 | * @param string $lock_type A string to identify different lock types. |
| 48 | * @return int |
| 49 | */ |
| 50 | protected function get_duration( $lock_type ) { |
| 51 | return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return ActionScheduler_Lock |
| 56 | */ |
| 57 | public static function instance() { |
| 58 | if ( empty( self::$locker ) ) { |
| 59 | $class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' ); |
| 60 | self::$locker = new $class(); |
| 61 | } |
| 62 | return self::$locker; |
| 63 | } |
| 64 | } |
| 65 |