set( $api_message_id, $batch_status_arr['current_iteration'] ); $progress_bar_estimated_data = self::getProgressBarObject()->calculateRemainingTime(); } if ( $batch_status_arr['is_completed'] == true ) { if ( $api_message_id != '' ) { self::getProgressBarObject()->stop( $api_message_id ); } break; } $running_time = ( time() - $start_epoch ); if ( $running_time > $timeout ) { break; } Debug::Text( ' Sleeping: '. $tmp_retry_timeout, __FILE__, __LINE__, __METHOD__, 10 ); sleep( $tmp_retry_timeout ); $tmp_retry_timeout = $progress_bar_estimated_data['next_check_time']; //Never let retry timeout exceed max. if ( $tmp_retry_timeout > $max_retry_timeout ) { $tmp_retry_timeout = $max_retry_timeout; } } Debug::Text( 'Batch completed: Jobs: '. $batch_status_arr['total_iterations'] .' in '. ( time() - $start_epoch ).'s', __FILE__, __LINE__, __METHOD__, 10 ); return $batch_status_arr; } public static function getBatchStatus( $batch_id ) { $sjqlf = TTnew('SystemJobQueueListFactory'); $retarr = $sjqlf->getBatchStatus( $batch_id ); return $retarr; } public static function Add( $name, $batch_id, $class, $method, $args, $priority = null, $extra_data = null, $effective_date = null, $user_id = null ) { if ( empty( $effective_date ) ) { $effective_date = microtime( true ); } if ( empty( $user_id ) ) { global $current_user; if ( is_object( $current_user ) ) { $user_id = $current_user->getID(); } else { $user_id = TTUUID::getZeroID(); } } $sjqf = TTNew('SystemJobQueueFactory'); /** @var SystemJobQueueFactory $sjqf */ $sjqf->setBatch( $batch_id ); $sjqf->setStatus( 10 ); //10=Pending $sjqf->setPriority( $priority ); $sjqf->setName( $name ); $sjqf->setUser( $user_id ); $sjqf->setEffectiveDate( $effective_date ); $sjqf->setClass( $class ); $sjqf->setMethod( $method ); $sjqf->setArguments( $args ); //Each top level array element is an argument. $sjqf->setExtraData( $extra_data ); //API Message ID/UserGenericStatus Queue ID, etc... if ( $sjqf->isValid() ) { Debug::Arr( $args, ' Adding to Job Queue. Name: '. $name .' Class: '. $class .' Method: '. $method, __FILE__, __LINE__, __METHOD__, 10 ); return $sjqf->Save(); } return false; } public static function DeletePending( $class, $method, $batch_id, $user_id = null ) { if ( empty( $user_id ) ) { global $current_user; if ( is_object( $current_user ) ) { $user_id = $current_user->getID(); } else { $user_id = TTUUID::getZeroID(); } } $sjqlf = TTnew('SystemJobQueueListFactory'); $retval = $sjqlf->deletePending( $user_id, $class, $method, $batch_id ); return $retval; } public static function DeletePendingDuplicates( $class, $method, $user_id = null ) { if ( empty( $user_id ) ) { global $current_user; if ( is_object( $current_user ) ) { $user_id = $current_user->getID(); } else { $user_id = TTUUID::getZeroID(); } } $sjqlf = TTnew('SystemJobQueueListFactory'); $retval = $sjqlf->deletePendingDuplicates( $user_id, $class, $method ); return $retval; } static function sendNotificationToBrowser( $user_id = null, $payload = null ) { if ( $user_id == null ) { global $current_user; if ( isset($current_user) && is_object( $current_user ) ) { $user_id = $current_user->getId(); } } if ( TTUUID::isUUID( $user_id ) && $user_id != TTUUID::getZeroID() ) { if ( $payload == null ) { $payload = [ 'timetrex' => [ 'event' => [ [ 'type' => 'refresh_job_queue', 'check_completed' => true ] ] ] ]; } Debug::Text( ' Sending background notification to users browser to update the job queue...', __FILE__, __LINE__, __METHOD__, 10 ); $notification_data = [ 'object_id' => TTUUID::getZeroID(), 'user_id' => $user_id, 'type_id' => 'system', 'object_type_id' => 0, 'priority' => 2, //2=High 'title_short' => null, //Background 'payload' => $payload, 'device_id' => [ 4 ], //Web Browser Only. ]; Notification::sendNotification( $notification_data ); } return true; } static function Purge() { global $db; Debug::Text( 'Purging old job queues before: ' . TTDate::getDate('DATE+TIME', ( time() - 172800 ) ), __FILE__, __LINE__, __METHOD__, 10 ); //Mark jobs stuck running for more than 12hrs as failed. $purge_query = 'UPDATE system_job_queue SET status_id = 50, retry_attempt = retry_attempt + 1, completed_date = extract( epoch from now() ) WHERE status_id = 20 AND run_date <= '. ( time() - 43200 ) .' AND completed_date IS NULL'; $db->Execute( $purge_query ); //Purge successfully completed jobs within 2 days //Purge failed jobs within 1 week. $purge_query = 'DELETE FROM system_job_queue WHERE ( status_id = 100 AND completed_date <= '. ( time() - 172800 ) .' ) OR ( status_id = 50 AND completed_date <= '. ( time() - 604800 ) .' )'; return $db->Execute( $purge_query ); } } ?>