contactcats/Civi/Api4/Action/ContactCategory/Sync.php
2025-05-02 09:50:30 +01:00

60 lines
1.6 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 {
/**
* Contact ID
*
* Force sync for given contact only.
*
* @var int
*/
protected $contact_id;
/**
* 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 sync for ' . ($this->contact_id ? "contact $this->contact_id" : "all contacts"),
['=start' => 'ContactCatSync', '=timed' => 1]
);
if (!$this->force && !$this->contact_id) {
$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($this->contact_id);
$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']);
}
}