Elgg3: difficulties getting "poor man's cron" plugin working

On Elgg 2 my Crontrigger plugin used run_function_once() within a 'shutdown', 'system' callback to trigger the cron plugin hooks if the time since they were triggered last is > the cron interval.

Now on Elgg 3 the run_function_once() function is no longer available (and apparently no alternative that I could use as the BatchUpgrade class seems not fit the needs here). So, I've tried it two other ways instead:

    foreach ($periods as $period => $interval) {
        $ts = $cron_service->getLog('completion', $period);
        $deadline = $ts + $interval;

        if ($now > $deadline) {
            elgg_trigger_plugin_hook('cron', $period, $params);
        }
    }

would be triggering the cron hook as before but the problem is that it results in a mess as the cronjobs can (well they do!) run several times in parallel at the same time as the lock mechanism is missing preventing just that.

Then I also tried with the code used in ElggCli from running the cronjobs:

function crontrigger_shutdownhook(\Elgg\Event $event)) {
    $access = elgg_set_ignore_access(true);
    
    $time = new \DateTime('now');

    _elgg_services()->cron->setCurrentTime($time);
    $jobs = _elgg_services()->cron->run(null, false);

    elgg_set_ignore_access($access);
}

Not sure what's wrong here. The problem is that the execution is totally unreliable. The minute job mostly runs, the fiveminute job sometimes. But all the other intervals seem not getting triggered at all.

Any idea what's wrong or is there any other approach to be able to trigger the cron intervals without using the cron daemon?

  • AFAIK the only reliable way to have the cron work is through the cron daemon.

    In my development setup i have the cron triggered every page request, and that gives the same unreliability you described.

    The crons only run on the minute they're suppose to (so 15 minute cron only in the minutes 0, 15, 30 and 45) if i don't make a request at that time, no cron :(

    But what's against setting up the cron through the cron daemon? If you want reliability.

  • Its about "poor mans cron" plugin. Very helpul utility. On many low budget ( but reliable) web host running cron daemon is a problem. Poor man cron helps a lot in that scenario.

  • I think I got it working by re-implementing a run_once function that read/writes the timestamp of the last runs via plugin settings. Of course, using cron daemon defined jobs if much better. But the Crontrigger plugin is supposed to be helpful in situations where you can NOT use the cron daemon.