SCHEDULING SYSTEM

WordPress Cron Integration with Queue Processing

WordPress Cron Integration

Custom Schedule Intervals

function esh_cron_schedules($schedules) {
    $schedules['1min'] = [
        'interval' => 60, 
        'display' => __('Each 1 min', 'esh')
    ];
    $schedules['2min'] = [
        'interval' => 120, 
        'display' => __('Each 2 min', 'esh')
    ];
    $schedules['5min'] = [
        'interval' => 300, 
        'display' => __('Each 5 min', 'esh')
    ];
    $schedules['weekly'] = [
        'interval' => 604800, 
        'display' => __('Once Weekly', 'esh')
    ];
    $schedules['monthly'] = [
        'interval' => 2635200, 
        'display' => __('Once a Month', 'esh')
    ];
    
    return $schedules;
}
add_filter('cron_schedules', 'esh_cron_schedules');

Schedule Management

Schedule Creation

wp_schedule_event(time(), '5min', 'esh_rets_import_schedule_' . $schedule_id);

Schedule Removal

wp_unschedule_event(time(), 'esh_rets_import_schedule_' . $schedule_id); wp_clear_scheduled_hook('esh_rets_import_schedule_' . $schedule_id);

Available Intervals

Interval Seconds Use Case
1min 60 High-frequency updates
2min 120 Fast import cycles
5min 300 Regular updates
weekly 604800 Full data refresh

Schedule Execution Logic

Post-Execution Processing

add_action('esh_scheduler_after_execute', 'esh_scheduler_after_execute', 10, 1);

function esh_scheduler_after_execute($schedule) {
    $update_offset = false;
    
    // Check if import reached end and should reset
    if ($schedule instanceof Esh_Schedule && 
        $schedule->is_import_latest_properties == 1 && 
        $schedule->num_items <= 0 && 
        $schedule->num_items < $schedule->offset) {
        $update_offset = true;
    } elseif ($schedule instanceof Esh_Schedule && 
              $schedule->num_items > 0 && 
              $schedule->num_items <= $schedule->offset) {
        $update_offset = true;
    }
    
    // Reset offset and method when cycle complete
    if ($update_offset) {
        $schedule->save_field_value('offset', 0);
        $schedule_id = $schedule->getID();
        update_post_meta($schedule_id, 'offset', 0);
        $schedule->save_field_value('method', 'skip');
        update_post_meta($schedule_id, 'method', 'skip');
    }
}

Offset Management

Continuous Imports

Tracks position in large datasets to resume where previous import left off

Cycle Completion

Automatically resets to beginning when all records processed

Non-Stop Processing

Continuous Operation

Schedules can run in "non-stop" mode for constant data synchronization

  • • Processes batches of properties continuously
  • • Prevents timeout issues with large datasets
  • • Maintains import position across executions
  • • Automatically handles cycle completion

Queue Processing System

Image Import Queue

add_action('esh_phrets_import_images_from_queue', 'esh_phrets_import_images_from_queue');

function esh_phrets_import_images_from_queue() {
    $queue = get_option('es_phrets_image_queue', []);
    
    if (!empty($queue) && is_array($queue)) {
        $queue = array_reverse($queue);
        
        foreach ($queue as $post_key => $post_id) {
            $property = esh_get_property($post_id);
            $profile_uid = $property->profile_uid;
            $esh_rets_connected = esh_phrets_get_connection(true, $profile_uid);
            
            // Process images for this property
            if ($property->rets_resource && $property->property_mls_id) {
                try {
                    $objects = $esh_rets_connected->GetObject(
                        $property->rets_resource, 
                        'Photo', 
                        $property->property_mls_id, 
                        '*', 
                        $use_obj_loc
                    );
                    
                    // Process each image...
                    
                    // Remove from queue when complete
                    unset($queue[$post_key]);
                } catch (Exception $e) {
                    continue;
                }
            }
            
            // Update queue and break (one property per execution)
            update_option('es_phrets_image_queue', array_reverse($queue));
            break;
        }
    }
}

Queue Benefits

  • Prevents Timeouts: Processes one property at a time
  • Memory Efficiency: Limits memory usage per execution
  • Error Recovery: Failed properties don't block queue
  • Resumable: Queue persists across server restarts

Queue Management

WordPress Options Storage

Uses WordPress options table for persistent queue storage

LIFO Processing

Last-in-first-out processing with array_reverse()

Schedule Configuration Options

Import Methods

Skip Method

Skips existing properties, only imports new ones

Overwrite Method

Updates existing properties with new data

Latest Properties

Imports only recently updated properties

Performance Controls

Setting Purpose
Limit Properties per execution
Offset Starting position in dataset
Images Num Max images per property
Non-Stop Continuous processing mode

Manual Schedule Controls

Control Functions

function esh_get_scheduler_start_link($schedule_id) {
    return add_query_arg([
        'es-action' => 'start-scheduler',
        'id' => $schedule_id,
    ]);
}

function esh_get_scheduler_stop_link($schedule_id) {
    return add_query_arg([
        'es-action' => 'stop-scheduler', 
        'id' => $schedule_id,
    ]);
}

function esh_get_scheduler_edit_link($id) {
    return add_query_arg([
        'es-action' => 'edit-scheduler',
        'id' => $id,
    ]);
}

Admin Interface Integration

Start/Stop Controls

Manual schedule activation and deactivation through admin interface

Schedule Editing

Modify schedule parameters without stopping running imports

Status Monitoring

Real-time status display for active schedules

← Return to Main Documentation