<commit_before><?php

require_once dirname(__FILE__).'/accesscheck.php';

/*
 * 
 * page to handle any cron-related activity. Instead of having multiple cron entries, this
 * one page should be called to do the tasks. It should be called as often as possible, eg once every 5 minutes
 * or even once a minute.
 * 
 * For now, the configuration is manually, there is no UI for it yet. Plugins can register their own cron activities.
 * TODO, work with eg https://github.com/mtdowling/cron-expression
 * 
 */

if (!$GLOBALS["commandline"]) {
  print "This page can only be called from the commandline";
  return;
}
   
cl_output(ClineSignature());

$cronJobs = array(

    # at a later stage, these should be added
    # for now, that involves conflicts, as they all use similar function names in the files. (eg output and finish)
    # also page locking needs changing, as it's the same page (cron) for all of them
    # so, for now, we only handle plugin Cron Jobs
    
    //array(
    //    'plugin' => '',
    //    'page' => 'processqueue',
    //    'frequency'  => 1,    // once a minute
    //),
    //array(
        //'plugin' => '',
        //'page' => 'processbounces',
        //'frequency' => 1440, // once a day
    //),
);

foreach ($GLOBALS['plugins'] as $pluginname => $plugin) {
    $pluginJobs = $plugin->cronJobs();
   # cl_output($pluginname.' has '.sizeof($pluginJobs).' jobs');
    foreach ($pluginJobs as $job) {
        $cronJobs[] = array(
            'plugin' => $pluginname,
            'page' => $job['page'],
            'frequency' => $job['frequency'],
        );
    }
}

if (!sizeof($cronJobs)) {
    cl_output(s('Nothing to do'));
}

$maxNextRun = 0;
$now = time();
foreach ($cronJobs as $cronJob) {
    $cronID = $cronJob['plugin']. '|'.$cronJob['page'];
    $lastrun = getConfig(md5($cronID));
    if (empty($lastrun) || ($now - $lastrun > $cronJob['frequency'] * 60)) {
        cl_output('Need to run '.$cronJob['plugin'] . ' - '.$cronJob['page']);
        $cronJob['page'] = basename($cronJob['page'],'.php');
        if (isset($GLOBALS['plugins'][$cronJob['plugin']]) && is_file($GLOBALS['plugins'][$cronJob['plugin']]->coderoot . $cronJob['page'].'.php')) {
             include_once $GLOBALS['plugins'][$cronJob['plugin']]->coderoot . $cronJob['page'].'.php';
        } elseif (empty($cronJob['plugin']) && is_file(__DIR__.'/'.$cronJob['page'].'.php')) {
             include_once __DIR__.'/'.$cronJob['page'].'.php';
        }
        SaveConfig(md5($cronID),time(),0);
    } else {
        $nextRun = ($lastrun + $cronJob['frequency'] * 60) - $now;
        if ($nextRun > $maxNextRun) {
            $maxNextRun = $nextRun;
        }
        if (VERBOSE) {
            cl_output('Will run '.$cronJob['plugin'] . ' - '.$cronJob['page']. ' in'.secs2time($nextRun));
        }
    }
}
## tell how soon we need to run again, so that the calling system can relax a bit
cl_output("DELAYUNTIL=".(int)($now + $maxNextRun));
#var_dump($cronJobs);
<commit_msg>exit when nothing to do<commit_after><?php

require_once dirname(__FILE__).'/accesscheck.php';

/*
 * 
 * page to handle any cron-related activity. Instead of having multiple cron entries, this
 * one page should be called to do the tasks. It should be called as often as possible, eg once every 5 minutes
 * or even once a minute.
 * 
 * For now, the configuration is manually, there is no UI for it yet. Plugins can register their own cron activities.
 * TODO, work with eg https://github.com/mtdowling/cron-expression
 * 
 */

if (!$GLOBALS["commandline"]) {
  print "This page can only be called from the commandline";
  return;
}
   
cl_output(ClineSignature());

$cronJobs = array(

    # at a later stage, these should be added
    # for now, that involves conflicts, as they all use similar function names in the files. (eg output and finish)
    # also page locking needs changing, as it's the same page (cron) for all of them
    # so, for now, we only handle plugin Cron Jobs
    
    //array(
    //    'plugin' => '',
    //    'page' => 'processqueue',
    //    'frequency'  => 1,    // once a minute
    //),
    //array(
        //'plugin' => '',
        //'page' => 'processbounces',
        //'frequency' => 1440, // once a day
    //),
);

foreach ($GLOBALS['plugins'] as $pluginname => $plugin) {
    $pluginJobs = $plugin->cronJobs();
   # cl_output($pluginname.' has '.sizeof($pluginJobs).' jobs');
    foreach ($pluginJobs as $job) {
        $cronJobs[] = array(
            'plugin' => $pluginname,
            'page' => $job['page'],
            'frequency' => $job['frequency'],
        );
    }
}

if (!sizeof($cronJobs)) {
    cl_output(s('Nothing to do'));
    exit;
}

$maxNextRun = 0;
$now = time();
foreach ($cronJobs as $cronJob) {
    $cronID = $cronJob['plugin']. '|'.$cronJob['page'];
    $lastrun = getConfig(md5($cronID));
    if (empty($lastrun) || ($now - $lastrun > $cronJob['frequency'] * 60)) {
        cl_output('Need to run '.$cronJob['plugin'] . ' - '.$cronJob['page']);
        $cronJob['page'] = basename($cronJob['page'],'.php');
        if (isset($GLOBALS['plugins'][$cronJob['plugin']]) && is_file($GLOBALS['plugins'][$cronJob['plugin']]->coderoot . $cronJob['page'].'.php')) {
             include_once $GLOBALS['plugins'][$cronJob['plugin']]->coderoot . $cronJob['page'].'.php';
        } elseif (empty($cronJob['plugin']) && is_file(__DIR__.'/'.$cronJob['page'].'.php')) {
             include_once __DIR__.'/'.$cronJob['page'].'.php';
        }
        SaveConfig(md5($cronID),time(),0);
    } else {
        $nextRun = ($lastrun + $cronJob['frequency'] * 60) - $now;
        if ($nextRun > $maxNextRun) {
            $maxNextRun = $nextRun;
        }
        if (VERBOSE) {
            cl_output('Will run '.$cronJob['plugin'] . ' - '.$cronJob['page']. ' in'.secs2time($nextRun));
        }
    }
}
## tell how soon we need to run again, so that the calling system can relax a bit
if ($maxNextRun > 0) {
    cl_output("DELAYUNTIL=".(int)($now + $maxNextRun));
}
#var_dump($cronJobs);

{'fraction_non_alphanumeric': 0.18282778054036133, 'fraction_numerical': 0.005138405436764462, 'mean_word_length': 2.867948717948718, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3081836', 'n_tokens_mistral': 2027, 'n_tokens_neox': 1977, 'n_words': 524}