Skip to content

Commit ef20d5f

Browse files
Merge pull request appwrite#5084 from appwrite/automate-hamster-script
feat: automate hamster
2 parents 28e6de8 + 4672b31 commit ef20d5f

File tree

4 files changed

+115
-66
lines changed

4 files changed

+115
-66
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ _APP_SMTP_SECURE=
4646
_APP_SMTP_USERNAME=
4747
_APP_SMTP_PASSWORD=
4848
_APP_HAMSTER_RECIPIENTS=
49+
_APP_HAMSTER_INTERVAL=86400
4950
_APP_SMS_PROVIDER=sms://username:password@mock
5051
_APP_SMS_FROM=+123456789
5152
_APP_STORAGE_LIMIT=30000000

app/console

Submodule console updated 72 files

docker-compose.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,39 @@ services:
544544
- _APP_LOGGING_PROVIDER
545545
- _APP_LOGGING_CONFIG
546546

547+
appwrite-hamster:
548+
entrypoint: hamster
549+
<<: *x-logging
550+
container_name: appwrite-hamster
551+
image: appwrite-dev
552+
networks:
553+
- appwrite
554+
volumes:
555+
- ./app:/usr/src/code/app
556+
- ./src:/usr/src/code/src
557+
depends_on:
558+
- redis
559+
environment:
560+
- _APP_ENV
561+
- _APP_WORKER_PER_CORE
562+
- _APP_CONNECTIONS_MAX
563+
- _APP_POOL_CLIENTS
564+
- _APP_OPENSSL_KEY_V1
565+
- _APP_DB_HOST
566+
- _APP_DB_PORT
567+
- _APP_DB_SCHEMA
568+
- _APP_DB_USER
569+
- _APP_DB_PASS
570+
- _APP_REDIS_HOST
571+
- _APP_REDIS_PORT
572+
- _APP_REDIS_USER
573+
- _APP_REDIS_PASS
574+
- _APP_CONNECTIONS_DB_CONSOLE
575+
- _APP_CONNECTIONS_DB_PROJECT
576+
- _APP_CONNECTIONS_CACHE
577+
- _APP_HAMSTER_RECIPIENTS
578+
- _APP_HAMSTER_INTERVAL
579+
547580
appwrite-maintenance:
548581
entrypoint: maintenance
549582
<<: *x-logging

src/Appwrite/Platform/Tasks/Hamster.php

Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -145,79 +145,94 @@ private function getStats(Database $dbForConsole, Database $dbForProject, Docume
145145

146146
public function action(Registry $register, Group $pools, Cache $cache, Database $dbForConsole): void
147147
{
148-
Console::info('Getting stats...');
149-
150-
/* Initialise new Utopia app */
151-
$app = new App('UTC');
152-
$console = $app->getResource('console');
153-
154-
/** CSV stuff */
155-
$this->date = date('Y-m-d');
156-
$this->path = "{$this->directory}/stats_{$this->date}.csv";
157-
$csv = Writer::createFromPath($this->path, 'w');
158-
$csv->insertOne($this->columns);
159-
160-
/** Database connections */
161-
$totalProjects = $dbForConsole->count('projects') + 1;
162-
Console::success("Found a total of: {$totalProjects} projects");
163-
164-
$projects = [$console];
165-
$count = 0;
166-
$limit = 30;
167-
$sum = 30;
168-
$offset = 0;
169-
while (!empty($projects)) {
170-
foreach ($projects as $project) {
171-
/**
172-
* Skip user projects with id 'console'
173-
*/
174-
if ($project->getId() === 'console') {
175-
continue;
176-
}
177148

178-
Console::info("Getting stats for {$project->getId()}");
179-
180-
try {
181-
$db = $project->getAttribute('database');
182-
$adapter = $pools
183-
->get($db)
184-
->pop()
185-
->getResource();
186-
187-
$dbForProject = new Database($adapter, $cache);
188-
$dbForProject->setDefaultDatabase('appwrite');
189-
$dbForProject->setNamespace('_' . $project->getInternalId());
190-
191-
$statsPerProject = $this->getStats($dbForConsole, $dbForProject, $project);
192-
$csv->insertOne(array_values($statsPerProject));
193-
} catch (\Throwable $th) {
194-
throw $th;
195-
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
196-
} finally {
197-
$pools
198-
->get($db)
199-
->reclaim();
149+
Console::title('Cloud Hamster V1');
150+
Console::success(APP_NAME . ' cloud hamster process v1 has started');
151+
152+
$interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default)
153+
154+
Console::loop(function () use ($register, $pools, $cache, $dbForConsole, $interval) {
155+
156+
$now = date('d-m-Y H:i:s', time());
157+
Console::info("[{$now}] Getting Cloud Usage Stats every {$interval} seconds");
158+
$loopStart = microtime(true);
159+
160+
/* Initialise new Utopia app */
161+
$app = new App('UTC');
162+
$console = $app->getResource('console');
163+
164+
/** CSV stuff */
165+
$this->date = date('Y-m-d');
166+
$this->path = "{$this->directory}/stats_{$this->date}.csv";
167+
$csv = Writer::createFromPath($this->path, 'w');
168+
$csv->insertOne($this->columns);
169+
170+
/** Database connections */
171+
$totalProjects = $dbForConsole->count('projects') + 1;
172+
Console::success("Found a total of: {$totalProjects} projects");
173+
174+
$projects = [$console];
175+
$count = 0;
176+
$limit = 30;
177+
$sum = 30;
178+
$offset = 0;
179+
while (!empty($projects)) {
180+
foreach ($projects as $project) {
181+
/**
182+
* Skip user projects with id 'console'
183+
*/
184+
if ($project->getId() === 'console') {
185+
continue;
186+
}
187+
188+
Console::info("Getting stats for {$project->getId()}");
189+
190+
try {
191+
$db = $project->getAttribute('database');
192+
$adapter = $pools
193+
->get($db)
194+
->pop()
195+
->getResource();
196+
197+
$dbForProject = new Database($adapter, $cache);
198+
$dbForProject->setDefaultDatabase('appwrite');
199+
$dbForProject->setNamespace('_' . $project->getInternalId());
200+
201+
$statsPerProject = $this->getStats($dbForConsole, $dbForProject, $project);
202+
$csv->insertOne(array_values($statsPerProject));
203+
} catch (\Throwable $th) {
204+
throw $th;
205+
Console::error('Failed to update project ("' . $project->getId() . '") version with error: ' . $th->getMessage());
206+
} finally {
207+
$pools
208+
->get($db)
209+
->reclaim();
210+
}
200211
}
201-
}
202212

203-
$sum = \count($projects);
213+
$sum = \count($projects);
204214

205-
$projects = $dbForConsole->find('projects', [
206-
Query::limit($limit),
207-
Query::offset($offset),
208-
]);
215+
$projects = $dbForConsole->find('projects', [
216+
Query::limit($limit),
217+
Query::offset($offset),
218+
]);
209219

210-
$offset = $offset + $limit;
211-
$count = $count + $sum;
220+
$offset = $offset + $limit;
221+
$count = $count + $sum;
212222

213-
Console::log('Iterated through ' . $count . '/' . $totalProjects . ' projects...');
214-
}
223+
Console::log('Iterated through ' . $count . '/' . $totalProjects . ' projects...');
224+
}
225+
226+
$this->sendEmail($register);
215227

216-
$this->sendEmail($register);
228+
$pools
229+
->get('console')
230+
->reclaim();
217231

218-
$pools
219-
->get('console')
220-
->reclaim();
232+
$loopTook = microtime(true) - $loopStart;
233+
$now = date('d-m-Y H:i:s', time());
234+
Console::info("[{$now}] Cloud Stats took {$loopTook} seconds");
235+
}, $interval);
221236
}
222237

223238
private function sendEmail(Registry $register)

0 commit comments

Comments
 (0)