You are here

function content_lock_timeout_cron in Content locking (anti-concurrent editing) 8

Same name and namespace in other branches
  1. 8.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()
  2. 6.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()
  3. 6 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()
  4. 7.3 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()
  5. 7 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()
  6. 7.2 modules/content_lock_timeout/content_lock_timeout.module \content_lock_timeout_cron()

Implements hook_cron().

Breaks batches of stale locks whenever the cron hooks are run. Inspired by original content_lock_cron() (leftover from the checkout module).

File

modules/content_lock_timeout/content_lock_timeout.module, line 23
Allowed time-based automatic unlocking of nodes.

Code

function content_lock_timeout_cron() {
  $config = \Drupal::config('content_lock_timeout.settings');
  $timeout_minutes = $config
    ->get('content_lock_timeout_minutes');
  $last_valid_time = Drupal::time()
    ->getCurrentTime() - 60 * $timeout_minutes;

  /** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
  $lock_service = \Drupal::service('content_lock');

  // We call release() for each lock so that the
  // hook_content_lock_released may be invoked.
  $query = \Drupal::database()
    ->select('content_lock', 'c');
  $query
    ->fields('c')
    ->condition('c.timestamp', $last_valid_time, '<');
  $count = 0;
  foreach ($query
    ->execute() as $obj) {
    $lock_service
      ->release($obj->entity_id, $obj->langcode, $obj->form_op, $obj->uid, $obj->entity_type);
    $count++;
  }
  if ($count) {
    $period = \Drupal::service('date.formatter')
      ->formatInterval($timeout_minutes * 60);
    \Drupal::logger('content_lock_timeout')
      ->notice('Released @count stale node locks which lasted at least @period.', [
      '@count' => $count,
      '@period' => $period,
    ]);
  }
}