Re-implement cron and configuration logic to use the table config entity

Created on 27 October 2023, about 1 year ago
Updated 3 February 2024, 10 months ago

Re-implement cron and configuration logic to use the table config entity.

Current cron code logic:

<?php
function denormalizer_cron() {
  $config = \Drupal::service('config.factory')
    ->getEditable('denormalizer.settings');

  if (!$config->get('cron_enabled')) {
    return;
  }

  $last_run = $config->get('last_run');
  $reset = NULL;
  if ($config->get('reload_every') && ($last_run + $config->get('reload_every') * 60 * 60) <= \Drupal::time()->getRequestTime()) {
    $reset = TRUE;
  }
  elseif ($config->get('run_every') && ($last_run + $config->get('run_every') * 60 * 60) <= \Drupal::time()->getRequestTime()) {
      $reset = FALSE;
    }
    if (!is_null($reset)) {
      $denormalizer = new Denormalizer();
      $denormalizer->build();
      try {
        $denormalizer->execute($reset);
      }
      catch (PDOException $e) {
        if (in_array($e->getCode(), ['42S22', '21S01'])) {
          \Drupal::logger('denormalizer')
            ->info('Different fields detected. Running reload.');
          $denormalizer->execute(TRUE);
        }
      }
      $config->set('last_run', \Drupal::time()->getRequestTime())->save();
      \Drupal::logger('denormalizer')->info('Ran denormalizer.');
    }
}
?>
πŸ“Œ Task
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada nikathone Ontario

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024