contactcats/Civi/Api4/Action/ContactCategory/Sync.php
2025-02-24 13:16:05 +00:00

47 lines
1.3 KiB
PHP

<?php
namespace Civi\Api4\Action\ContactCategory;
use Civi;
use Civi\Api4\Generic\Result;
use Civi\ContactCats\Processor;
/**
* Sync the data
*
* @see \Civi\Api4\Generic\AbstractAction
*
*/
class Sync extends \Civi\Api4\Generic\AbstractAction {
/**
* Usually this will not act unless the time is right.
*
* @default FALSE
* @var bool
*/
protected $force = FALSE;
public function _run(Result $result) {
ini_set('memory_limit', '256M');
Civi::log()->debug('Begin', ['=start' => 'ContactCatSync', '=timed' => 1]);
if (!$this->force) {
$nextRun = Civi::settings()->get('contactcats_next_run') ?? 0;
if (time() < $nextRun) {
// not needed yet.
Civi::log()->debug("Skipping because not due until " . date('H:i j M Y', $nextRun), ['=' => 'pop']);
return;
}
}
$processor = new Processor();
$result->exchangeArray($processor->run());
// Limit to running every 24 hours; we actually want it to be stable within one day.
// Schedule for 3am to avoid busy times and DST.
$nextRun = strtotime('tomorrow + 180 minutes');
Civi::settings()->set('contactcats_next_run', $nextRun);
Civi::settings()->set('contactcats_last_run', time());
Civi::log()->debug("Complete. Scheduling next run after " . date('H:i j M Y', $nextRun), ['=' => 'set']);
}
}