-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAgentKVAndLocksExample.php
More file actions
37 lines (29 loc) · 974 Bytes
/
Copy pathAgentKVAndLocksExample.php
File metadata and controls
37 lines (29 loc) · 974 Bytes
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
<?php
declare(strict_types=1);
namespace imperazim\easylibrary\examples\agent;
use imperazim\agent\api\AgentKVAPI;
use imperazim\agent\api\AgentLocksAPI;
use imperazim\agent\api\AgentRuntimeAPI;
final class AgentKVAndLocksExample{
public function storeGlobalEvent(string $eventId, int $ttlSeconds = 3600) : bool{
if(!AgentRuntimeAPI::isAvailable()){
return false;
}
return AgentKVAPI::set('lumen', 'current_event', $eventId, $ttlSeconds);
}
public function runOnceWithLock(callable $callback) : bool{
if(!AgentRuntimeAPI::isAvailable()){
return false;
}
$lock = AgentLocksAPI::lock('lumen.locks', 'mine_reset', 60, 'Example reset');
if(($lock['changed'] ?? false) !== true){
return false;
}
try{
$callback();
return true;
}finally{
AgentLocksAPI::unlock('lumen.locks', 'mine_reset');
}
}
}