# configctl mymodule mycronjob
# MyModule.xml<model> <mount>//OPNsense/MyModule</mount> <description> MyModule application </description> <items> <general> <UpdateCron type="ModelRelationField"> <Model> <queues> <source>OPNsense.Cron.Cron</source> <items>jobs.job</items> <display>description</display> <filters> <origin>/MyModule/</origin> </filters> </queues> </Model> <ValidationMessage>Related cron not found</ValidationMessage> <Required>N</Required> </UpdateCron> </general> </items></model>
# ServiceController.php/** * reconfigure MyModule */public function reloadAction(){ $status = "failed"; if ($this->request->isPost()) { $mdlMyModule = new MyModule(); if ((string)$mdlMyModule->general->UpdateCron == "") { $mdlCron = new Cron(); // update cron relation (if this doesn't break consistency) $mdlMyModule->general->UpdateCron = $mdlCron->newDailyJob("MyModule", "mymodule test", "MyModule test job", "0"); if ($mdlCron->performValidation()->count() == 0) { $mdlCron->serializeToConfig(); // save data to config, do not validate because the current in memory model doesn't know about the // cron item just created. $mdlMyModule->serializeToConfig($validateFullModel = false, $disable_validation = true); Config::getInstance()->save(); } } $backend = new Backend(); $bckresult = trim($backend->configdRun("template reload OPNsense.MyModule")); if ($bckresult == "OK") { $status = "ok"; } } return array("status" => $status);}
$mdlCron->performValidation()->count() return 1
$mdlCron->performValidation() return empty
$mdlCron = new Cron();$mdlCron->newDailyJob("MyModule", "mymodule test", "MyModule test job", "0");print_r($mdlCron->performValidation());
Phalcon\Validation\Message\Group Object( [_position:protected] => [_messages:protected] => Array ( [0] => Phalcon\Validation\Message Object ( [_type:protected] => PresenceOf [_message:protected] => Value needs to be between 0 and 59, multiple values, ranges and * are supported (ex. 1,10,20,30 or 1-30 ) [_field:protected] => jobs.job.f8e0db2b-8909-41cf-9847-82abc5b17741.minutes ) [1] => Phalcon\Validation\Message Object ( [_type:protected] => PresenceOf [_message:protected] => Value needs to be between 0 and 23, multiple values, ranges and * are supported (ex. 1,2,8 or 0-8 ) [_field:protected] => jobs.job.f8e0db2b-8909-41cf-9847-82abc5b17741.hours ) [2] => Phalcon\Validation\Message Object ( [_type:protected] => PresenceOf [_message:protected] => Value needs to be between 0 and 6 ( Sunday to Saturday), multiple values, ranges and * are supported (ex. 1,2,4 or 0-4 ) [_field:protected] => jobs.job.f8e0db2b-8909-41cf-9847-82abc5b17741.weekdays ) [3] => Phalcon\Validation\Message Object ( [_type:protected] => InclusionIn [_message:protected] => Select a command from the list. [_field:protected] => jobs.job.f8e0db2b-8909-41cf-9847-82abc5b17741.command ) ))
echo $mdlCron->toXML()->asXML();
# configctl mymodule test command works successfully on the command line.
Phalcon\Validation\Message\Group Object( [_position:protected] => [_messages:protected] => Array ( [0] => Phalcon\Validation\Message Object ( [_type:protected] => InclusionIn [_message:protected] => Select a command from the list. [_field:protected] => jobs.job.c98659f2-3555-471e-9e0c-e1acf26e791a.command [_code:protected] => 0 ) ))
<OPNsense> <cron> <jobs> <job uuid="ae473210-e201-4c51-8eb5-0cb70f211101"> <origin>IDS</origin> <enabled>0</enabled> <minutes>0</minutes> <hours>0</hours> <days>*</days> <months>*</months> <weekdays>0</weekdays> <who>root</who> <command>ids update</command> <parameters/> <description>ids rule updates</description> </job> <job uuid="c98659f2-3555-471e-9e0c-e1acf26e791a"> <origin>MyModule</origin> <enabled>0</enabled> <minutes>0</minutes> <hours>0</hours> <days>*</days> <months>*</months> <weekdays>0</weekdays> <who>root</who> <command>mymodule test</command> <parameters/> <description>MyModule test job</description> </job> </jobs> </cron></OPNsense>
configctl configd actions list | grep mymodule
ids update [ update IDS rules ]
/** * Class Cron * @package OPNsense\Cron */class Cron extends BaseModel{ /** * create a new daily job * @param string $origin * @param string $command * @param string $description * @param string $weekdays day(s) of the week to run * @param string $enabled default add disabled cron jobs, if triggered enabled be sure to call regenerate on cron. * @return string */ public function newDailyJob($origin, $command, $description, $weekdays = "*", $enabled = "0") { $cron = $this->jobs->job->Add(); $uuid = $cron->getAttributes()['uuid']; $cron->origin = $origin; $cron->command = $command; $cron->description = $description; $cron->weekdays = $weekdays ; $cron->enabled = $enabled; return $uuid; }}
if ((string)$mdlMymodule->general->UpdateCron == "") { $mdlCron = new Cron(); $mdlMymodule->general->UpdateCron = $mdlCron->newDailyJob("Mymodule", "mymodule test", "Mymodule Test cron", "1"); if ($mdlCron->performValidation()->count() == 0) { $mdlCron->serializeToConfig(); $mdlMymodule->serializeToConfig($validateFullModel = false, $disable_validation = true); Config::getInstance()->save(); }}
$mdlcron = new Cron();$cron = $mdlcron->jobs->job->Add();$cron->origin = "your origin";$cron->command = "your command";$cron->description = "your description";$cron->hours = "*" ;$cron->enabled = 1;... followed by the same save actions you already had.