feat(06-02): create scheduler helper class for email batch actions
- Created DDHH_JM_Scheduler class with static setup_hooks() method - Added schedule_mentor_notification_batch() method with 50-user batching - Uses as_enqueue_async_action() with unique flag and email-notifications group - Initialized in main plugin file and job manager class - Ready for Phase 06-03 to register async action callbacks
This commit is contained in:
84
includes/class-scheduler.php
Normal file
84
includes/class-scheduler.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Action Scheduler Helper Class
|
||||
*
|
||||
* Manages async background jobs for email batch processing.
|
||||
*
|
||||
* @package DDHH_Job_Manager
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly.
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class DDHH_JM_Scheduler
|
||||
*
|
||||
* Provides API for scheduling async email batch processing via Action Scheduler.
|
||||
*/
|
||||
class DDHH_JM_Scheduler {
|
||||
|
||||
/**
|
||||
* Initialize hooks.
|
||||
*/
|
||||
public static function setup_hooks() {
|
||||
// Phase 06-03 will register async action callbacks here.
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule mentor notification batch processing.
|
||||
*
|
||||
* Chunks user IDs into batches of 50 and schedules async actions for each batch.
|
||||
* This prevents email provider limits and timeouts when sending to many mentors.
|
||||
*
|
||||
* @param array $user_ids Array of user IDs to notify.
|
||||
* @param int $job_id Job post ID for the notification.
|
||||
* @return int Number of batches scheduled.
|
||||
*/
|
||||
public static function schedule_mentor_notification_batch( $user_ids, $job_id ) {
|
||||
if ( empty( $user_ids ) || empty( $job_id ) ) {
|
||||
error_log( 'DDHH_JM_Scheduler: Cannot schedule batch - missing user IDs or job ID' );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Chunk user IDs into batches of 50.
|
||||
$batches = array_chunk( $user_ids, 50 );
|
||||
$batch_count = 0;
|
||||
|
||||
foreach ( $batches as $batch ) {
|
||||
// Enqueue async action for this batch.
|
||||
// Hook: ddhh_jm_send_mentor_notification_batch
|
||||
// Args: batch of user IDs and job ID
|
||||
// Group: email-notifications (for organization)
|
||||
// Unique: true (prevents duplicate scheduling)
|
||||
// Priority: 10 (standard priority)
|
||||
as_enqueue_async_action(
|
||||
'ddhh_jm_send_mentor_notification_batch',
|
||||
array(
|
||||
'user_ids' => $batch,
|
||||
'job_id' => $job_id,
|
||||
),
|
||||
'email-notifications',
|
||||
true,
|
||||
10
|
||||
);
|
||||
|
||||
$batch_count++;
|
||||
error_log( sprintf(
|
||||
'DDHH_JM_Scheduler: Scheduled batch %d with %d users for job ID %d',
|
||||
$batch_count,
|
||||
count( $batch ),
|
||||
$job_id
|
||||
) );
|
||||
}
|
||||
|
||||
error_log( sprintf(
|
||||
'DDHH_JM_Scheduler: Scheduled %d total batches for %d users for job ID %d',
|
||||
$batch_count,
|
||||
count( $user_ids ),
|
||||
$job_id
|
||||
) );
|
||||
|
||||
return $batch_count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user