I was going to try to get this working better, before I posted it. But I am sure someone can fix this faster. (I haven’t been able to get parameters are passed)
I also was going back and forth about variable names. I started trying to create a cron like system, with a crontab and everything. Using "jobs" as "snippets" But I ended up doing a quick proof of concept that did what I needed.
Maybe it is enough for you too. But I think a few tweaks would make it more useful for everyone.
This is not a true and accurate cron system. Just place this snippet on the front page so that it gets hit more often and it should work.
cronX beta 000.001
instructions: create a folder assets/snippets/cronX
- crontab: is the interval in minutes
- job: is the name of the snippet
<?php
// example call
// [!cronX? &crontab=`15` &cronSettings=`writecron.txt` &job=`writeFile`!]
// parameters passed on
$crontab = isset($crontab) ? $crontab:'60'; // in minutes, default one hour
$cronSettings = isset($cronSettings)? $cronSettings: 'cron_parameters.txt'; // default
$snippetName = isset($job)? $job: ''; //
$snParams = isset($snippetParameters)? $snippetParameters: ''; //
// setup
$path = $modx->config['base_path']."assets/snippets/cronX/".$cronSettings;
// read lastrun
if (!file_exists($path)) {
$CSFileHandle = fopen($path, 'w');
fwrite($CSFileHandle, time());
}
$CSFileHandle = fopen($path, "r");
$lastrun = fread($CSFileHandle, filesize($path));
fclose($CSFileHandle);
$nextrun = $lastrun + (60 * $crontab);
if (time() > $nextrun) {
// run job
// $snParams = array();
// $snParams['sep'] = '|';
$output = $modx->runSnippet($snippetName, $snParams);
echo $output;
// update parameters
$CSFileHandle = fopen($path, 'w');
fwrite($CSFileHandle, time());
fclose($CSFileHandle);
}
?>
Please feel free to suggest improvements... with real code
[edit] fixed small error in the code.