From 515b5904b849136ff755a4c936cb328c1834e4f6 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 10 Dec 2024 11:34:51 +0000 Subject: [PATCH 1/8] update project accessed at from router --- app/controllers/general.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/general.php b/app/controllers/general.php index 32cc68fecf..2c43116c10 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -91,6 +91,15 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $project = Authorization::skip( fn () => $dbForPlatform->getDocument('projects', $projectId) ); + + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForConsole->updateDocument('projects', $project->getId(), $project)); + } + } + if (array_key_exists('proxy', $project->getAttribute('services', []))) { $status = $project->getAttribute('services', [])['proxy']; if (!$status) { From ab4a4441545ac30517610797e1adc0274103f195 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 10 Dec 2024 11:13:09 +0000 Subject: [PATCH 2/8] Fix: project accessed at on workers --- app/worker.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/worker.php b/app/worker.php index 2c7d8acb22..6fdb49d35a 100644 --- a/app/worker.php +++ b/app/worker.php @@ -374,6 +374,20 @@ try { $worker = $platform->getWorker(); +$worker + ->init() + ->inject('project') + ->inject('dbForConsole') + ->action(function (Document $project, Database $dbForConsole) { + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForConsole->updateDocument('projects', $project->getId(), $project)); + } + } + }); + $worker ->shutdown() ->inject('pools') From 06c09503df70bb2e5677cf4d390864fc4f718ce0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 04:27:28 +0000 Subject: [PATCH 3/8] update project rsource --- app/worker.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/worker.php b/app/worker.php index 6fdb49d35a..06dc4c44d8 100644 --- a/app/worker.php +++ b/app/worker.php @@ -62,7 +62,13 @@ Server::setResource('project', function (Message $message, Database $dbForPlatfo return $project; } - return $dbForPlatform->getDocument('projects', $project->getId()); + // NOT all workers need valid DB config so we return empty if this fails + try { + return $dbForPlatform->getDocument('projects', $project->getId()); + } catch (\Throwable $e) { + return new Document([]); + } + }, ['message', 'dbForPlatform']); Server::setResource('dbForProject', function (Cache $cache, Registry $register, Message $message, Document $project, Database $dbForPlatform) { From c7713f58dd4b0fc33f4d083a183d5ea925ac1233 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 13:52:58 +0000 Subject: [PATCH 4/8] remove worker hook --- app/worker.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/app/worker.php b/app/worker.php index 06dc4c44d8..fd9681b37e 100644 --- a/app/worker.php +++ b/app/worker.php @@ -380,20 +380,6 @@ try { $worker = $platform->getWorker(); -$worker - ->init() - ->inject('project') - ->inject('dbForConsole') - ->action(function (Document $project, Database $dbForConsole) { - if (!$project->isEmpty() && $project->getId() !== 'console') { - $accessedAt = $project->getAttribute('accessedAt', ''); - if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $project->setAttribute('accessedAt', DateTime::now()); - Authorization::skip(fn () => $dbForConsole->updateDocument('projects', $project->getId(), $project)); - } - } - }); - $worker ->shutdown() ->inject('pools') From 0cdb2a98be906e86e6248ef78394c32ae9dcf48a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 14:05:37 +0000 Subject: [PATCH 5/8] fix db name --- app/controllers/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 2c43116c10..bb60b01ddd 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -96,7 +96,7 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $accessedAt = $project->getAttribute('accessedAt', ''); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { $project->setAttribute('accessedAt', DateTime::now()); - Authorization::skip(fn () => $dbForConsole->updateDocument('projects', $project->getId(), $project)); + Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); } } From 2fb9ebe71df40a258f6c89d8d16a0ba8fc99f331 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 14:05:49 +0000 Subject: [PATCH 6/8] update project accessed at in schedules --- .../Platform/Tasks/ScheduleExecutions.php | 12 ++++++++++++ src/Appwrite/Platform/Tasks/ScheduleFunctions.php | 15 +++++++++++++-- src/Appwrite/Platform/Tasks/ScheduleMessages.php | 14 +++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 50beb48e9d..9be7950fd6 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -5,6 +5,8 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Func; use Swoole\Coroutine as Co; use Utopia\Database\Database; +use Utopia\Database\DateTime; +use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleExecutions extends ScheduleBase @@ -57,6 +59,16 @@ class ScheduleExecutions extends ScheduleBase $delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp(); + $project = $schedule['project']; + + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); + } + } + \go(function () use ($queueForFunctions, $schedule, $delay, $data) { Co::sleep($delay); diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index 156ff1e31d..d1a9662967 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -7,6 +7,7 @@ use Cron\CronExpression; use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; +use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleFunctions extends ScheduleBase @@ -70,7 +71,7 @@ class ScheduleFunctions extends ScheduleBase } foreach ($delayedExecutions as $delay => $scheduleKeys) { - \go(function () use ($delay, $scheduleKeys, $pools) { + \go(function () use ($delay, $scheduleKeys, $pools, $dbForPlatform) { \sleep($delay); // in seconds $queue = $pools->get('queue')->pop(); @@ -84,6 +85,16 @@ class ScheduleFunctions extends ScheduleBase $schedule = $this->schedules[$scheduleKey]; + $project = $schedule['project']; + + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); + } + } + $queueForFunctions = new Func($connection); $queueForFunctions @@ -91,7 +102,7 @@ class ScheduleFunctions extends ScheduleBase ->setFunction($schedule['resource']) ->setMethod('POST') ->setPath('/') - ->setProject($schedule['project']) + ->setProject($project) ->trigger(); } diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 2136e62f08..3beb8c48fb 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -4,6 +4,8 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Messaging; use Utopia\Database\Database; +use Utopia\Database\DateTime; +use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleMessages extends ScheduleBase @@ -45,10 +47,20 @@ class ScheduleMessages extends ScheduleBase $connection = $queue->getResource(); $queueForMessaging = new Messaging($connection); + $project = $schedule['project']; + + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); + } + } + $queueForMessaging ->setType(MESSAGE_SEND_TYPE_EXTERNAL) ->setMessageId($schedule['resourceId']) - ->setProject($schedule['project']) + ->setProject($project) ->trigger(); $dbForPlatform->deleteDocument( From d7d69b63a8f95eefc895a532079fbc56ea741045 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 14:15:27 +0000 Subject: [PATCH 7/8] reset worker --- app/worker.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/worker.php b/app/worker.php index fd9681b37e..563a801b6b 100644 --- a/app/worker.php +++ b/app/worker.php @@ -62,13 +62,7 @@ Server::setResource('project', function (Message $message, Database $dbForPlatfo return $project; } - // NOT all workers need valid DB config so we return empty if this fails - try { - return $dbForPlatform->getDocument('projects', $project->getId()); - } catch (\Throwable $e) { - return new Document([]); - } - + return $dbForPlatform->getDocument('projects', $project->getId()); }, ['message', 'dbForPlatform']); Server::setResource('dbForProject', function (Cache $cache, Registry $register, Message $message, Document $project, Database $dbForPlatform) { @@ -435,4 +429,4 @@ $worker->workerStart() Console::info("Worker $workerName started"); }); -$worker->start(); +$worker->start(); \ No newline at end of file From e24cce4e48aee24b6fc8e6713565e82bf1b340d1 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 17 Dec 2024 14:19:26 +0000 Subject: [PATCH 8/8] refactor --- app/worker.php | 2 +- src/Appwrite/Platform/Tasks/ScheduleBase.php | 12 ++++++++++++ src/Appwrite/Platform/Tasks/ScheduleExecutions.php | 12 +----------- src/Appwrite/Platform/Tasks/ScheduleFunctions.php | 13 ++----------- src/Appwrite/Platform/Tasks/ScheduleMessages.php | 14 ++------------ 5 files changed, 18 insertions(+), 35 deletions(-) diff --git a/app/worker.php b/app/worker.php index 563a801b6b..2c7d8acb22 100644 --- a/app/worker.php +++ b/app/worker.php @@ -429,4 +429,4 @@ $worker->workerStart() Console::info("Worker $workerName started"); }); -$worker->start(); \ No newline at end of file +$worker->start(); diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 10623e7fa5..dad2db0d9a 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -9,6 +9,7 @@ use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Exception; use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; use Utopia\Platform\Action; use Utopia\Pools\Group; use Utopia\System\System; @@ -39,6 +40,17 @@ abstract class ScheduleBase extends Action ->callback(fn (Group $pools, Database $dbForPlatform, callable $getProjectDB) => $this->action($pools, $dbForPlatform, $getProjectDB)); } + protected function updateProjectAccess(Document $project, Database $dbForPlatform): void + { + if (!$project->isEmpty() && $project->getId() !== 'console') { + $accessedAt = $project->getAttribute('accessedAt', ''); + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { + $project->setAttribute('accessedAt', DateTime::now()); + Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); + } + } + } + /** * 1. Load all documents from 'schedules' collection to create local copy * 2. Create timer that sync all changes from 'schedules' collection to local copy. Only reading changes thanks to 'resourceUpdatedAt' attribute diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 9be7950fd6..086bad513e 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -5,8 +5,6 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Func; use Swoole\Coroutine as Co; use Utopia\Database\Database; -use Utopia\Database\DateTime; -use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleExecutions extends ScheduleBase @@ -59,15 +57,7 @@ class ScheduleExecutions extends ScheduleBase $delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp(); - $project = $schedule['project']; - - if (!$project->isEmpty() && $project->getId() !== 'console') { - $accessedAt = $project->getAttribute('accessedAt', ''); - if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $project->setAttribute('accessedAt', DateTime::now()); - Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); - } - } + $this->updateProjectAccess($schedule['project'], $dbForPlatform); \go(function () use ($queueForFunctions, $schedule, $delay, $data) { Co::sleep($delay); diff --git a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php index d1a9662967..c443bb6c2d 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleFunctions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleFunctions.php @@ -7,7 +7,6 @@ use Cron\CronExpression; use Utopia\CLI\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; -use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleFunctions extends ScheduleBase @@ -85,15 +84,7 @@ class ScheduleFunctions extends ScheduleBase $schedule = $this->schedules[$scheduleKey]; - $project = $schedule['project']; - - if (!$project->isEmpty() && $project->getId() !== 'console') { - $accessedAt = $project->getAttribute('accessedAt', ''); - if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $project->setAttribute('accessedAt', DateTime::now()); - Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); - } - } + $this->updateProjectAccess($schedule['project'], $dbForPlatform); $queueForFunctions = new Func($connection); @@ -102,7 +93,7 @@ class ScheduleFunctions extends ScheduleBase ->setFunction($schedule['resource']) ->setMethod('POST') ->setPath('/') - ->setProject($project) + ->setProject($schedule['project']) ->trigger(); } diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 3beb8c48fb..5d997fc5bb 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -4,8 +4,6 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Messaging; use Utopia\Database\Database; -use Utopia\Database\DateTime; -use Utopia\Database\Validator\Authorization; use Utopia\Pools\Group; class ScheduleMessages extends ScheduleBase @@ -47,20 +45,12 @@ class ScheduleMessages extends ScheduleBase $connection = $queue->getResource(); $queueForMessaging = new Messaging($connection); - $project = $schedule['project']; - - if (!$project->isEmpty() && $project->getId() !== 'console') { - $accessedAt = $project->getAttribute('accessedAt', ''); - if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $project->setAttribute('accessedAt', DateTime::now()); - Authorization::skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $project)); - } - } + $this->updateProjectAccess($schedule['project'], $dbForPlatform); $queueForMessaging ->setType(MESSAGE_SEND_TYPE_EXTERNAL) ->setMessageId($schedule['resourceId']) - ->setProject($project) + ->setProject($schedule['project']) ->trigger(); $dbForPlatform->deleteDocument(