mirror of
https://github.com/appwrite/appwrite
synced 2026-05-24 09:28:40 +00:00
refactor
This commit is contained in:
parent
d7d69b63a8
commit
e24cce4e48
5 changed files with 18 additions and 35 deletions
|
|
@ -429,4 +429,4 @@ $worker->workerStart()
|
||||||
Console::info("Worker $workerName started");
|
Console::info("Worker $workerName started");
|
||||||
});
|
});
|
||||||
|
|
||||||
$worker->start();
|
$worker->start();
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use Utopia\Database\DateTime;
|
||||||
use Utopia\Database\Document;
|
use Utopia\Database\Document;
|
||||||
use Utopia\Database\Exception;
|
use Utopia\Database\Exception;
|
||||||
use Utopia\Database\Query;
|
use Utopia\Database\Query;
|
||||||
|
use Utopia\Database\Validator\Authorization;
|
||||||
use Utopia\Platform\Action;
|
use Utopia\Platform\Action;
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
use Utopia\System\System;
|
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));
|
->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
|
* 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
|
* 2. Create timer that sync all changes from 'schedules' collection to local copy. Only reading changes thanks to 'resourceUpdatedAt' attribute
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,6 @@ namespace Appwrite\Platform\Tasks;
|
||||||
use Appwrite\Event\Func;
|
use Appwrite\Event\Func;
|
||||||
use Swoole\Coroutine as Co;
|
use Swoole\Coroutine as Co;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\DateTime;
|
|
||||||
use Utopia\Database\Validator\Authorization;
|
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
|
|
||||||
class ScheduleExecutions extends ScheduleBase
|
class ScheduleExecutions extends ScheduleBase
|
||||||
|
|
@ -59,15 +57,7 @@ class ScheduleExecutions extends ScheduleBase
|
||||||
|
|
||||||
$delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp();
|
$delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp();
|
||||||
|
|
||||||
$project = $schedule['project'];
|
$this->updateProjectAccess($schedule['project'], $dbForPlatform);
|
||||||
|
|
||||||
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) {
|
\go(function () use ($queueForFunctions, $schedule, $delay, $data) {
|
||||||
Co::sleep($delay);
|
Co::sleep($delay);
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ use Cron\CronExpression;
|
||||||
use Utopia\CLI\Console;
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\DateTime;
|
use Utopia\Database\DateTime;
|
||||||
use Utopia\Database\Validator\Authorization;
|
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
|
|
||||||
class ScheduleFunctions extends ScheduleBase
|
class ScheduleFunctions extends ScheduleBase
|
||||||
|
|
@ -85,15 +84,7 @@ class ScheduleFunctions extends ScheduleBase
|
||||||
|
|
||||||
$schedule = $this->schedules[$scheduleKey];
|
$schedule = $this->schedules[$scheduleKey];
|
||||||
|
|
||||||
$project = $schedule['project'];
|
$this->updateProjectAccess($schedule['project'], $dbForPlatform);
|
||||||
|
|
||||||
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 = new Func($connection);
|
||||||
|
|
||||||
|
|
@ -102,7 +93,7 @@ class ScheduleFunctions extends ScheduleBase
|
||||||
->setFunction($schedule['resource'])
|
->setFunction($schedule['resource'])
|
||||||
->setMethod('POST')
|
->setMethod('POST')
|
||||||
->setPath('/')
|
->setPath('/')
|
||||||
->setProject($project)
|
->setProject($schedule['project'])
|
||||||
->trigger();
|
->trigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ namespace Appwrite\Platform\Tasks;
|
||||||
|
|
||||||
use Appwrite\Event\Messaging;
|
use Appwrite\Event\Messaging;
|
||||||
use Utopia\Database\Database;
|
use Utopia\Database\Database;
|
||||||
use Utopia\Database\DateTime;
|
|
||||||
use Utopia\Database\Validator\Authorization;
|
|
||||||
use Utopia\Pools\Group;
|
use Utopia\Pools\Group;
|
||||||
|
|
||||||
class ScheduleMessages extends ScheduleBase
|
class ScheduleMessages extends ScheduleBase
|
||||||
|
|
@ -47,20 +45,12 @@ class ScheduleMessages extends ScheduleBase
|
||||||
$connection = $queue->getResource();
|
$connection = $queue->getResource();
|
||||||
$queueForMessaging = new Messaging($connection);
|
$queueForMessaging = new Messaging($connection);
|
||||||
|
|
||||||
$project = $schedule['project'];
|
$this->updateProjectAccess($schedule['project'], $dbForPlatform);
|
||||||
|
|
||||||
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
|
$queueForMessaging
|
||||||
->setType(MESSAGE_SEND_TYPE_EXTERNAL)
|
->setType(MESSAGE_SEND_TYPE_EXTERNAL)
|
||||||
->setMessageId($schedule['resourceId'])
|
->setMessageId($schedule['resourceId'])
|
||||||
->setProject($project)
|
->setProject($schedule['project'])
|
||||||
->trigger();
|
->trigger();
|
||||||
|
|
||||||
$dbForPlatform->deleteDocument(
|
$dbForPlatform->deleteDocument(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue