/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updatedsched = false; foreach ($cron_schedules as $proc => &$params) { $running = 0; if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && time() > $running + 900) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $next_schedule = time() + $processes[$proc]; $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updatedsched = true; } } if ($updatedsched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $timenow = time(); $processesToRun = array(); foreach ($cron_schedules as $process => $scheduled_times) { if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + 900 < $timenow) && $scheduled_times['next_schedule'] < $timenow) { $processesToRun[] = $process; } } $model_config =& WYSIJA::get('config', 'model'); if (!empty($processesToRun) && $model_config->getValue('cron_page_hit_trigger')) { //call the cron url $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; //TODO we should use the http class there $hHTTP =& WYSIJA::get('http', 'helper'); $hHTTP->request_timeout($cron_url); } }
/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updated_sched = false; foreach ($cron_schedules as $schedule => &$params) { $running = 0; $time_now = time(); if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && $time_now > $running + $processes[$schedule]) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $process_frequency = $processes[$schedule]; $next_schedule = $running + $process_frequency; // if the next schedule is already behind, we give it 30 seconds before it can trigger again if ($next_schedule < $time_now) { $next_schedule = $time_now + 30; } $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updated_sched = true; } } if ($updated_sched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $time_now = time(); $processesToRun = array(); foreach ($cron_schedules as $schedule => $scheduled_times) { if (strpos($schedule, '(bounce handling not activated)') !== false) { continue; } if (!isset($processes[$schedule])) { continue; } $process_frequency = $processes[$schedule]; if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + $process_frequency < $time_now) && $scheduled_times['next_schedule'] < $time_now) { $processesToRun[] = $schedule; } } $model_config = WYSIJA::get('config', 'model'); $page_view_trigger = (int) $model_config->getValue('cron_page_hit_trigger'); if (!empty($processesToRun) && $page_view_trigger === 1) { //call the cron url // do not call that more than once per 5 minutes attempt at reducing the CPU load for some users // http://wordpress.org/support/topic/wysija-newsletters-slowing-down-my-site-1 $last_cron_time_plus_5min = (int) get_option('wysija_last_php_cron_call') + 5 * 60; if ($last_cron_time_plus_5min < time()) { $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; $cron_request = apply_filters('cron_request', array('url' => $cron_url, 'args' => array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)))); wp_remote_post($cron_url, $cron_request['args']); WYSIJA::update_option('wysija_last_php_cron_call', time()); } } }
/** * run process if it's not detected as already running * @param type $process * @return type */ function run_scheduled_task($process = 'queue') { //first let's make sure that the process asked to be run is not already running $scheduled_times = WYSIJA::get_cron_schedule($process); $processes = WYSIJA::get_cron_frequencies(); $process_frequency = $processes[$process]; // check if the scheduled task is already being processed, // we consider it timed out once the started running time plus the frequency has been passed if (!empty($scheduled_times['running']) && $scheduled_times['running'] + $process_frequency > time()) { if ($this->report) { echo 'already running : ' . $process . '<br/>'; } return; } // set schedule as running WYSIJA::set_cron_schedule($process, 0, time()); // execute schedule switch ($process) { case 'queue': // check if there are any scheduled newsletters ready for action WYSIJA::check_scheduled_newsletters(); // if premium is activated we execute the premium cron process if (defined('WYSIJANLP')) { $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $helper_premium->croned_queue_process(); } else { // run the standard queue process no scheduled tasks will be check since it has already been checked above WYSIJA::croned_queue(false); } break; case 'bounce': $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $model_config = WYSIJA::get('config', 'model'); // if premium is activated we launch the premium function if (is_multisite()) { $multisite_prefix = 'ms_'; } // we don't process the bounce automatically unless the option is ticked if (defined('WYSIJANLP') && $model_config->getValue($multisite_prefix . 'bounce_process_auto')) { $helper_premium->croned_bounce(); } else { $process .= ' (bounce handling not activated)'; } break; case 'daily': WYSIJA::croned_daily(); break; case 'weekly': if (defined('WYSIJANLP')) { $helper_premium = WYSIJA::get('premium', 'helper', false, WYSIJANLP); $helper_premium->croned_weekly(); } WYSIJA::croned_weekly(); break; case 'monthly': WYSIJA::croned_monthly(); break; } // set next_schedule details WYSIJA::set_cron_schedule($process); if ($this->report) { echo 'processed : ' . $process . '<br/>'; } }
/** * check that there is no passed schedules that need to be executed now * @return void */ public static function cron_check() { $cron_schedules = WYSIJA::get_cron_schedule('all'); if (empty($cron_schedules)) { return; } else { $processes = WYSIJA::get_cron_frequencies(); $updated_sched = false; foreach ($cron_schedules as $schedule => &$params) { $running = 0; $time_now = time(); if (isset($params['running'])) { $running = $params['running']; } //if the process has timedout we reschedule the next execution if ($running && $time_now > $running + $processes[$schedule]) { //WYSIJA::setInfo('error','modifying next schedule for '.$proc); $process_frequency = $processes[$schedule]; $next_schedule = $running + $process_frequency; // if the next schedule is already behind, we give it 30 seconds before it can trigger again if ($next_schedule < $time_now) { $next_schedule = $time_now + 30; } $params = array('next_schedule' => $next_schedule, 'prev_schedule' => $running, 'running' => false); $updated_sched = true; } } if ($updated_sched) { //WYSIJA::setInfo('error','updating scheds'); WYSIJA::update_option('wysija_schedules', $cron_schedules, 'yes'); } } $time_now = time(); $processesToRun = array(); foreach ($cron_schedules as $schedule => $scheduled_times) { $process_frequency = $processes[$schedule]; if ((!$scheduled_times['running'] || (int) $scheduled_times['running'] + $process_frequency < $time_now) && $scheduled_times['next_schedule'] < $time_now) { $processesToRun[] = $schedule; } } $model_config = WYSIJA::get('config', 'model'); $page_view_trigger = (int) $model_config->getValue('cron_page_hit_trigger'); if (!empty($processesToRun) && $page_view_trigger === 1) { //call the cron url $cron_url = site_url('wp-cron.php') . '?' . WYSIJA_CRON . '&action=wysija_cron&process=' . implode(',', $processesToRun) . '&silent=1'; $cron_request = apply_filters('cron_request', array('url' => $cron_url, 'args' => array('timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters('https_local_ssl_verify', true)))); wp_remote_post($cron_url, $cron_request['args']); } }