-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
EdgeSync.php
76 lines (64 loc) · 2.45 KB
/
EdgeSync.php
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
<?php
namespace Appwrite\Platform\Tasks;
use Utopia\App;
use Utopia\Platform\Action;
use Utopia\CLI\Console;
use Utopia\Database\DateTime;
use Utopia\Database\Query;
use Utopia\Database\Database;
use Utopia\Pools\Group;
use Utopia\Queue\Client;
class EdgeSync extends Action
{
public static function getName(): string
{
return 'edge-sync';
}
public function __construct()
{
$this
->desc('Schedules edge sync tasks')
->inject('pools')
->inject('dbForConsole')
->inject('queueForCacheSyncOut')
->callback(fn (Group $pools, Database $dbForConsole, Client $queueForCacheSyncOut) => $this->action($pools, $dbForConsole, $queueForCacheSyncOut));
}
public function action(Group $pools, Database $dbForConsole, Client $queueForCacheSyncOut): void
{
Console::title('Edge-sync V1');
Console::success(APP_NAME . ' Edge-sync v1 has started');
$interval = (int) App::getEnv('_APP_SYNC_EDGE_INTERVAL', '180');
Console::loop(function () use ($interval, $dbForConsole, $queueForCacheSyncOut) {
$time = DateTime::now();
$count = 0;
$chunk = 0;
$limit = 50;
$sum = $limit;
Console::success("[{$time}] New task every {$interval} seconds");
while ($sum === $limit) {
$chunk++;
$results = $dbForConsole->find('syncs', [
Query::equal('region', [App::getEnv('_APP_REGION')]),
Query::limit($limit)
]);
$sum = count($results);
if ($sum > 0) {
foreach ($results as $document) {
Console::info("[{$time}] Enqueueing keys chunk {$count} to {$document->getAttribute('target')}");
$queueForCacheSyncOut
->enqueue([
'value' => [
'region' => $document->getAttribute('target'),
'keys' => $document->getAttribute('keys')
]
]);
$dbForConsole->deleteDocument('syncs', $document->getId());
$count++;
}
} else {
Console::info("[{$time}] No cache keys where found.");
}
}
}, $interval);
}
}