Skip to content

Commit 85417a7

Browse files
committed
Add Worker_Task.
1 parent 9346a9b commit 85417a7

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/**
3+
* Class Google\Site_Kit\Core\Email_Reporting\Worker_Task
4+
*
5+
* @package Google\Site_Kit\Core\Email_Reporting
6+
* @copyright 2025 Google LLC
7+
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8+
* @link https://sitekit.withgoogle.com
9+
*/
10+
11+
namespace Google\Site_Kit\Core\Email_Reporting;
12+
13+
/**
14+
* Handles worker cron callbacks for email reporting.
15+
*
16+
* @since n.e.x.t
17+
* @access private
18+
* @ignore
19+
*/
20+
class Worker_Task {
21+
22+
/**
23+
* Email log batch query helper.
24+
*
25+
* @since n.e.x.t
26+
*
27+
* @var Email_Log_Batch_Query
28+
*/
29+
private $batch_query;
30+
31+
/**
32+
* Scheduler instance.
33+
*
34+
* @since n.e.x.t
35+
*
36+
* @var Email_Reporting_Scheduler
37+
*/
38+
private $scheduler;
39+
40+
/**
41+
* Max execution limiter.
42+
*
43+
* @since n.e.x.t
44+
*
45+
* @var Max_Execution_Limiter
46+
*/
47+
private $max_execution_limiter;
48+
49+
/**
50+
* Constructor.
51+
*
52+
* @since n.e.x.t
53+
*
54+
* @param Max_Execution_Limiter $max_execution_limiter Execution limiter instance.
55+
* @param Email_Log_Batch_Query $batch_query Batch query helper.
56+
* @param Email_Reporting_Scheduler $scheduler Scheduler instance.
57+
*/
58+
public function __construct(
59+
Max_Execution_Limiter $max_execution_limiter,
60+
Email_Log_Batch_Query $batch_query,
61+
Email_Reporting_Scheduler $scheduler
62+
) {
63+
$this->max_execution_limiter = $max_execution_limiter;
64+
$this->batch_query = $batch_query;
65+
$this->scheduler = $scheduler;
66+
}
67+
68+
/**
69+
* Handles worker cron executions for email reporting.
70+
*
71+
* @since n.e.x.t
72+
*
73+
* @param string $batch_id Batch identifier.
74+
* @param string $frequency Frequency slug.
75+
* @param int $initiator_timestamp Initiator timestamp.
76+
*/
77+
public function handle_callback_action( $batch_id, $frequency, $initiator_timestamp ) {
78+
$lock_handle = $this->acquire_lock( $frequency );
79+
if ( ! $lock_handle ) {
80+
return;
81+
}
82+
83+
try {
84+
if ( $this->should_abort( $initiator_timestamp ) ) {
85+
return;
86+
}
87+
88+
if ( $this->batch_query->is_complete( $batch_id ) ) {
89+
return;
90+
}
91+
92+
$pending_ids = $this->batch_query->get_pending_ids( $batch_id );
93+
94+
if ( empty( $pending_ids ) ) {
95+
return;
96+
}
97+
98+
$this->schedule_follow_up( $batch_id, $frequency, $initiator_timestamp );
99+
100+
if ( $this->should_abort( $initiator_timestamp ) ) {
101+
return;
102+
}
103+
104+
foreach ( $pending_ids as $post_id ) {
105+
if ( $this->should_abort( $initiator_timestamp ) ) {
106+
return;
107+
}
108+
109+
$this->batch_query->increment_attempt( $post_id );
110+
}
111+
112+
if ( $this->should_abort( $initiator_timestamp ) ) {
113+
return;
114+
}
115+
} finally {
116+
delete_transient( $lock_handle );
117+
}
118+
}
119+
120+
/**
121+
* Attempts to acquire a frequency-scoped worker lock.
122+
*
123+
* @since n.e.x.t
124+
*
125+
* @param string $frequency Frequency slug.
126+
* @return string|false Transient name on success, false if lock already held.
127+
*/
128+
private function acquire_lock( $frequency ) {
129+
$transient_name = sprintf( 'googlesitekit_email_reporting_worker_lock_%s', $frequency );
130+
131+
if ( get_transient( $transient_name ) ) {
132+
return false;
133+
}
134+
135+
set_transient( $transient_name, time(), MINUTE_IN_SECONDS );
136+
137+
return $transient_name;
138+
}
139+
140+
/**
141+
* Determines if the current worker run should abort.
142+
*
143+
* @since n.e.x.t
144+
*
145+
* @param int $initiator_timestamp Initiator timestamp.
146+
* @return bool True if processing should stop immediately.
147+
*/
148+
private function should_abort( $initiator_timestamp ) {
149+
return $this->max_execution_limiter->should_abort( $initiator_timestamp );
150+
}
151+
152+
/**
153+
* Schedules the follow-up worker event.
154+
*
155+
* @since n.e.x.t
156+
*
157+
* @param string $batch_id Batch identifier.
158+
* @param string $frequency Frequency slug.
159+
* @param int $initiator_timestamp Initiator timestamp.
160+
*/
161+
private function schedule_follow_up( $batch_id, $frequency, $initiator_timestamp ) {
162+
$target_time = time() + ( 11 * MINUTE_IN_SECONDS );
163+
$delay = max( 0, $target_time - (int) $initiator_timestamp );
164+
165+
$this->scheduler->schedule_worker( $batch_id, $frequency, $initiator_timestamp, $delay );
166+
}
167+
}

0 commit comments

Comments
 (0)