do_work(); class atkp_external_cronjob { /** * Construct the cron */ public function __construct() { //load required settings for cronjob $crontype = get_option(ATKP_PLUGIN_PREFIX.'_crontype', 'wpcron'); switch($crontype) { default: case 'wpcron': //wp cron? nothing todo... throw new exception('external cronjob deactivated'); break; case 'external': $this->echo_messages = false; break; case 'externaloutput': $this->echo_messages = true; break; } $this->dataupdate_interval = get_option(ATKP_PLUGIN_PREFIX.'_cache_duration', 1440); $this->datacheck_enabled = get_option(ATKP_PLUGIN_PREFIX.'_check_enabled', false); $this->datacheck_interval = get_option(ATKP_PLUGIN_PREFIX.'_notification_interval', 4320); $this->csvupdate_interval = get_option(ATKP_PLUGIN_PREFIX.'_access_csv_intervall', 1440); } private $echo_messages = false; private $datacheck_enabled = false; private $datacheck_interval = 4320; private $dataupdate_interval = 1440; private $csvupdate_interval = 1440; private $minutes = 0; public function do_work() { $this->send_message('### cronjob started ###'); $lastactivity = $this->get_lastactivity(); $lastrun_completed = get_option(ATKP_PLUGIN_PREFIX.'_cron_resume') != true; $isresume = !$lastrun_completed; if($lastactivity == '') $this->send_message( 'first run'); else { $this->send_message( 'last activity: '.date('d.m.y H:i:s', $lastactivity)); $this->send_message( 'last run completed: '.$lastrun_completed); $this->send_message( 'is resume: '.$isresume); } //wenn der letzte job noch nicht fertig ist, oder abgebrochen wurde dann ist ein resume flag vorhanden //wenn im resume modus dann sollten wir warten bis das last activity flag länger als 5 minuten her ist //sonst kann es vorkommen das tasks doppelt ausgeführt werden. if($isresume) { $diff_lastactivity = round(abs(time() - $lastactivity) / 60,2); if($diff_lastactivity <= 5) { $this->send_message( '5 minutes blocking: ' .(5-$diff_lastactivity)); return; } } //das resume flag setzt das der cronjob noch nicht durchgelaufen ist //das datum vom lastrun muss immer wieder gesetzt werden. der andere cronjob prüft dann ob lastrun schon länger als 5 minuten her ist $now = self::update_lastactivity(); update_option(ATKP_PLUGIN_PREFIX.'_cron_resume', true); //zuerst werden notwendige CSV-Dateien importiert $this->do_csv_import($isresume); //danach werden die Produkte aktualisiert $this->do_product_update($isresume); //und dann die Listen $this->do_list_update($isresume); //datenüberprüfung machen if($this->datacheck_enabled) $this->do_data_check($isresume); $this->send_message('### cronjob finished ###'); delete_option(ATKP_PLUGIN_PREFIX.'_cron_resume'); echo 'OK'; } public static function update_lastactivity() { $now = time(); update_option(ATKP_PLUGIN_PREFIX.'_cron_lastactivity', $now); //echo date('H:i:s').' - '. 'lastactivity updated: '.date('H:i:s', $now).'
'.PHP_EOL; return $now; } private function get_lastactivity() { return get_option(ATKP_PLUGIN_PREFIX.'_cron_lastactivity'); } private function do_csv_import($isresume) { try { $this->send_message('*** csv cronjob for datacheck started ***'); $lastimport = get_option(ATKP_PLUGIN_PREFIX.'_cron_csv_lastimport'); if(!$isresume && $lastimport != '') { $diff_lastimport = round(abs(time() - $lastimport) / 60,2); //wenn keine fortsetzung, dann prüfen if($diff_lastimport <= $this->csvupdate_interval) { $this->send_message('next import (hours): '. round(($this->csvupdate_interval- $diff_lastimport) / 60,2)); return; } else $this->send_message('import now'); } $posts_found = get_posts(array( 'posts_per_page' => -1, 'post_status' => array('publish','draft'), 'post_type' => ATKP_SHOP_POSTTYPE, 'meta_query' => array( array( 'key' => ATKP_SHOP_POSTTYPE.'_access_webservice', 'value' => '7', )) )); $this->send_message('csv posts found: '. count($posts_found)); //wenn csv feeds vorhanden sind, dann initialisiere den csv cronjob. if($posts_found && count($posts_found) > 0) { require_once ATKP_PLUGIN_DIR.'/includes/shopproviders/atkp_shop_provider_base.php'; require_once ATKP_PLUGIN_DIR.'/includes/shopproviders/atkp_shop_provider_csv.php'; foreach($posts_found as $csvimport) { try { $this->send_message('import csv: '. $csvimport->ID . ' - '.$csvimport->post_title); $csvprovider = new atkp_shop_provider_csv(); $csvprovider->import_csv($csvimport->ID, true, $isresume, array('atkp_external_cronjob', 'update_lastactivity')); unset($csvprovider); $this->send_message('import finished csv: '. $csvimport->ID); } catch (Exception $e) { $this->send_message('import csv error ('.$csvimport->ID.') '.$e->getMessage()); } } } update_option(ATKP_PLUGIN_PREFIX.'_cron_csv_lastimport', time()); $this->send_message('*** csv cronjob finished ***'); } catch (Exception $e) { ATKPLog::LogError($e->getMessage()); } } private function get_datatime() { $updatetime = time(); $this->send_message('cnt: '.$updatetime); if($this->dataupdate_interval > 60) $updatetime = strtotime('-'.($this->dataupdate_interval / 60).' hours'); else $updatetime = strtotime('-'.($this->dataupdate_interval).' min'); $this->send_message('cnt: '.$updatetime); $updatetime = $this->get_time($updatetime, 'timestamp'); $this->send_message('timestamp: '. date('d.m.y H:m:s', $updatetime)); return $updatetime; } private function do_product_update($isresume) { try { $this->send_message('*** product update cronjob started ***'); $lastimport = get_option(ATKP_PLUGIN_PREFIX.'_cron_product_lastimport'); if(!$isresume && $lastimport != '') { $diff_lastimport = round(abs(time() - $lastimport) / 60,2); //wenn keine fortsetzung, dann prüfen if($diff_lastimport <= $this->dataupdate_interval) { $this->send_message('next import (hours): '. round(($this->dataupdate_interval- $diff_lastimport) / 60,2)); return; } else $this->send_message('import now'); } //TODO: nur produkte laden die noch nicht aktualisiert wurden require_once ATKP_PLUGIN_DIR.'/includes/atkp_product.php'; require_once ATKP_PLUGIN_DIR.'/includes/atkp_list.php'; require_once ATKP_PLUGIN_DIR.'/includes/atkp_cronjob.php'; $cron = new atkp_cronjob(array()); $updatetime = $this->get_datatime(); $errors = array(); $posts_found = get_posts(array( 'posts_per_page' => -1, 'post_status' => array('draft', 'publish'), 'post_type' => ATKP_PRODUCT_POSTTYPE, 'meta_query' => array( array( 'key' => ATKP_PRODUCT_POSTTYPE.'_updatedon', 'type'=>'numeric', 'compare' => '<', 'value' => $updatetime, )) )); $this->send_message('products found: '.count($posts_found)); $y = 1; foreach ($posts_found as $prdpost) { try { $this->send_message('updating: '.$prdpost->ID.' '. $prdpost->post_title); $cron->update_product($prdpost->ID, false,false,false, false,false,false, true); //Delay damit nicht zu viele Request hintereinander kommen sleep(1); //resume marker, save position each 50 entries to have a resume mark if ($y % 50 == 0) { self::update_lastactivity(); } $y++; } catch(Exception $e) { array_push( $errors, 'Product '. $prdpost->ID. ': '. $e->getMessage()); ATKPLog::LogError($e->getMessage()); } } update_option(ATKP_PLUGIN_PREFIX.'_cron_product_lastimport', time()); $this->send_message('*** product update cronjob finished ***'); } catch (Exception $e) { ATKPLog::LogError($e->getMessage()); } } function get_time($time, $type, $gmt = 0 ) { switch ( $type ) { case 'mysql': return ( $gmt ) ? gmdate($time, 'Y-m-d H:i:s' ) : gmdate($time, 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) ); case 'timestamp': return ( $gmt ) ? $time : $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); default: return ( $gmt ) ? date($time, $type ) : date($time, $type, $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); } } private function do_list_update($isresume) { try { $this->send_message('*** list update cronjob started ***'); $lastimport = get_option(ATKP_PLUGIN_PREFIX.'_cron_list_lastimport'); if(!$isresume && $lastimport != '') { $diff_lastimport = round(abs(time() - $lastimport) / 60,2); //wenn keine fortsetzung, dann prüfen if($diff_lastimport <= $this->dataupdate_interval) { $this->send_message('next import (hours): '. round(($this->dataupdate_interval- $diff_lastimport) / 60,2)); return; } else $this->send_message('import now'); } //TODO: nur listen laden die noch nicht aktualisiert wurden require_once ATKP_PLUGIN_DIR.'/includes/atkp_product.php'; require_once ATKP_PLUGIN_DIR.'/includes/atkp_list.php'; require_once ATKP_PLUGIN_DIR.'/includes/atkp_cronjob.php'; $cron = new atkp_cronjob(array()); $updatetime = $this->get_datatime(); $errors = array(); $posts_found = get_posts(array( 'posts_per_page' => -1, 'post_status' => array('draft', 'publish'), 'post_type' => ATKP_LIST_POSTTYPE, 'meta_query' => array( array( 'key' => ATKP_LIST_POSTTYPE.'_updatedon', 'type'=>'numeric', 'compare' => '<', 'value' => $updatetime, )) )); $this->send_message('lists found: '.count($posts_found)); $y = 1; foreach ($posts_found as $prdpost) { try { $this->send_message('updating: '.$prdpost->ID.' '. $prdpost->post_title); $cron->update_list($prdpost->ID, true); //Delay damit nicht zu viele Request hintereinander kommen sleep(1); //resume marker, save position each 50 entries to have a resume mark if ($y % 50 == 0) { self::update_lastactivity(); } $y++; } catch(Exception $e) { array_push( $errors, 'List '. $prdpost->ID. ': '. $e->getMessage()); ATKPLog::LogError($e->getMessage()); } } update_option(ATKP_PLUGIN_PREFIX.'_cron_list_lastimport', time()); $this->send_message('*** list update cronjob finished ***'); } catch (Exception $e) { ATKPLog::LogError($e->getMessage()); } } private function do_data_check($isresume) { try { $this->send_message('*** data check cronjob started ***'); if(!$this->datacheck_enabled) { $this->send_message('data check disabled'); return; } $lastimport = get_option(ATKP_PLUGIN_PREFIX.'_cron_lastdatacheck'); if(!$isresume && $lastimport != '') { $diff_lastimport = round(abs(time() - $lastimport) / 60,2); //wenn keine fortsetzung, dann prüfen if($diff_lastimport <= $this->datacheck_interval) { $this->send_message('next check (hours): '. round(($this->datacheck_interval- $diff_lastimport) / 60,2)); return; } else $this->send_message('import now'); } //TODO: nur listen laden die noch nicht aktualisiert wurden require_once ATKP_PLUGIN_DIR.'/includes/atkp_cronjob.php'; $cron = new atkp_cronjob(array()); $cron->do_this_weekly(); update_option(ATKP_PLUGIN_PREFIX.'_cron_lastdatacheck', time()); $this->send_message('*** data check cronjob finished ***'); } catch (Exception $e) { ATKPLog::LogError($e->getMessage()); } } private function send_message($message) { if($this->echo_messages) echo date('H:i:s').' - '. $message.'
'.PHP_EOL; } } ?>