Repository navigation
Expand file tree
/
Copy pathclass-spacefast-sync-state.php
More file actions
182 lines (168 loc) · 5.48 KB
/
Copy pathclass-spacefast-sync-state.php
File metadata and controls
182 lines (168 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
defined( 'ABSPATH' ) || exit;
final class Spacefast_Sync_State {
const OPTION = 'spacefast_wordpress_sync_state';
const HOOK = 'spacefast_wordpress_deliver';
/**
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int}
*/
public static function defaults(): array {
return array(
'desired' => 0,
'delivered' => 0,
'event_id' => '',
'reasons' => array(),
'attempts' => 0,
'next_at' => 0,
'last_status' => 'idle',
'last_message' => '',
'last_build_id' => '',
'last_version_id' => '',
'last_change_at' => 0,
'last_attempt_at' => 0,
'last_success_at' => 0,
);
}
/**
* @return array{desired:int,delivered:int,event_id:string,reasons:array<int,string>,attempts:int,next_at:int,last_status:string,last_message:string,last_build_id:string,last_version_id:string,last_change_at:int}
*/
public static function get(): array {
$value = get_option( self::OPTION, array() );
return array_merge( self::defaults(), is_array( $value ) ? $value : array() );
}
/**
* Pure state transition used by WordPress hooks and tests.
*
* @param array<string,mixed> $state State.
* @return array<string,mixed>
*/
public static function record_change( array $state, string $reason, string $event_id ): array {
$state = array_merge( self::defaults(), $state );
$state['desired'] = (int) $state['desired'] + 1;
$state['event_id'] = $event_id;
$state['reasons'] = array_values(
array_unique(
array_slice(
array_merge( (array) $state['reasons'], array( sanitize_key( $reason ) ) ),
-10
)
)
);
$state['attempts'] = 0;
$state['next_at'] = 0;
$state['last_change_at'] = time();
$state['last_status'] = 'pending';
$state['last_message'] = '';
return $state;
}
/**
* Only acknowledges the generation that was actually delivered.
*
* @param array<string,mixed> $state State after HTTP returns.
* @return array<string,mixed>
*/
public static function acknowledge( array $state, int $generation, string $build_id ): array {
$state = array_merge( self::defaults(), $state );
$state['delivered'] = max( (int) $state['delivered'], $generation );
$state['attempts'] = 0;
$state['next_at'] = 0;
$state['last_status'] = (int) $state['desired'] > $generation ? 'pending' : 'delivered';
$state['last_message'] = '';
$state['last_build_id'] = $build_id;
$state['last_success_at'] = time();
return $state;
}
/**
* @param array<string,mixed> $state State.
* @return array<string,mixed>
*/
public static function acknowledge_static( array $state, string $version_id, string $status ): array {
$state = array_merge( self::defaults(), $state );
$state['delivered'] = (int) $state['desired'];
$state['attempts'] = 0;
$state['next_at'] = 0;
$state['last_status'] = $status;
$state['last_message'] = '';
$state['last_version_id'] = $version_id;
$state['last_success_at'] = time();
return $state;
}
public static function retry_delay( int $attempt ): int {
$delays = array( 60, 300, 900, 3600, 21600, DAY_IN_SECONDS );
return $delays[ min( max( 1, $attempt ), count( $delays ) ) - 1 ];
}
public static function save( array $state ): void {
update_option( self::OPTION, $state, false );
}
/**
* Apply a compare-and-swap transition so concurrent content and cron
* requests cannot overwrite one another.
*
* @param callable(array<string,mixed>):array<string,mixed> $transition Transition.
* @return array<string,mixed>
*/
public static function mutate( callable $transition ): array {
for ( $attempt = 0; $attempt < 10; $attempt++ ) {
$current = self::get();
$next = $transition( $current );
if ( self::compare_and_swap_option( self::OPTION, $current, $next ) ) {
return $next;
}
}
throw new RuntimeException( 'Spacefast state changed too many times; retrying later.' );
}
public static function claim_lock( string $option, string $owner, int $now ): bool {
$next = array(
'owner' => $owner,
'expires' => $now + 120,
);
$current = get_option( $option, false );
if ( false === $current ) {
return add_option( $option, $next, '', false );
}
if ( is_array( $current ) && (int) ( $current['expires'] ?? 0 ) > $now ) {
return false;
}
return self::compare_and_swap_option( $option, $current, $next );
}
public static function release_lock( string $option, string $owner ): void {
global $wpdb;
$current = get_option( $option, false );
if ( ! is_array( $current ) || $owner !== ( $current['owner'] ?? '' ) ) {
return;
}
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name = %s AND option_value = %s",
$option,
maybe_serialize( $current )
)
);
wp_cache_delete( $option, 'options' );
}
/**
* @param mixed $expected Expected value.
* @param mixed $next Next value.
*/
private static function compare_and_swap_option( string $option, $expected, $next ): bool {
global $wpdb;
$updated = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND option_value = %s",
maybe_serialize( $next ),
$option,
maybe_serialize( $expected )
)
);
if ( 1 === $updated ) {
wp_cache_delete( $option, 'options' );
return true;
}
return false;
}
public static function reset(): void {
wp_clear_scheduled_hook( self::HOOK );
delete_option( self::OPTION );
delete_option( 'spacefast_wordpress_worker_lock' );
}
}