<?php
namespace Entity; class CronJob extends \App\Entity\GeneralAbstract { protected $saveInsideTransaction = false; protected $primaryKeyType='string'; static public function queue($idEntity) { $db=\DB::getInstance(); global $DBTCronJobs; $incrementSQL = "UPDATE `{$DBTCronJobs}` SET `isQueued` = `isQueued` + 1 WHERE `id` = ?"; $db->Execute($incrementSQL, [$idEntity]); if ($db->ErrorNo()) { throw new \DBException("SQL FAILED - [{$incrementSQL}]."); } } protected function setTableName(): void { global $DBTCronJobs; $this->tableName=$DBTCronJobs; } protected function getAttribute($var) { switch ($var) { case 'scheduleOptions': case 'status': case 'settings': case 'errorManagement': case 'cronJobCommandQueue': if (!is_array($this->fields[$var])) { if ($this->fields[$var]) { $this->fields[$var] = unserialize($this->fields[$var]); } else { $this->fields[$var] = []; } } return $this->fields[$var]; break; default: { return parent::getAttribute($var); } } } protected function setAttribute($var, $value) { if ($this->operation!='insert' and $this->operation!='update') { throw new \Exception('You can call '.__METHOD__.' only for insert/update operations.'); } switch ($var) { case 'id': { if ($this->operation=='insert') { $this->fields[$var]=$value; } else { throw new \Exception('You can use __set magic method with `id` parameter only with insert operation in `'.__CLASS__.'` class.'); } break; } case 'active': case 'scheduleOptions': case 'settings': case 'status': case 'errorManagement': case 'isQueued': case 'lastExecutionTime': case 'cronJobCommandQueue': { $this->fields[$var]=$value; break; } default: { throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity."); } } } protected function save_filterSaveData(Array $saveData) { if ($this->operation=='insert') { $cron = \App\Cron\Main::getCronjobObj($this->fields['id']); } else { $cron = \App\Cron\Main::getCronjobObj($this->getId()); } $saveData = $cron->cleanupSettings($saveData); if (array_key_exists('scheduleOptions', $saveData) && is_array($saveData['scheduleOptions'])) { $saveData['scheduleOptions'] = serialize($saveData['scheduleOptions']); } if (array_key_exists('cronJobCommandQueue', $saveData) && is_array($saveData['cronJobCommandQueue'])) { $saveData['cronJobCommandQueue'] = serialize($saveData['cronJobCommandQueue']); } if (array_key_exists('settings', $saveData) && is_array($saveData['settings'])) { $saveData['settings'] = serialize($saveData['settings']); } if (array_key_exists('errorManagement', $saveData) && is_array($saveData['errorManagement'])) { $saveData['errorManagement'] = serialize($saveData['errorManagement']); } if (array_key_exists('status', $saveData) && is_array($saveData['status'])) { $saveData['status'] = serialize($saveData['status']); } return $saveData; } static public function getRawCommandQueue($idEntity) { global $DBTCronJobs; $db = \DB::getInstance(); $query = "SELECT `cronJobCommandQueue` FROM `{$DBTCronJobs}` WHERE `id`=?"; $commandQueue = $db->getOne($query, [$idEntity]); if ($db->ErrorNo()) { throw new \DBException("SQL FAILED"); } return ($commandQueue?unserialize($commandQueue):[]); } } 