2021-06-12 02:20:18 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Resque;
|
|
|
|
|
2021-07-26 11:14:19 +12:00
|
|
|
use Utopia\Cache\Cache;
|
|
|
|
use Utopia\Cache\Adapter\Redis as RedisCache;
|
|
|
|
use Utopia\CLI\Console;
|
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\Adapter\MariaDB;
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
abstract class Worker
|
|
|
|
{
|
2021-09-01 21:09:04 +12:00
|
|
|
public array $args = [];
|
2021-06-12 02:20:18 +12:00
|
|
|
|
|
|
|
abstract public function init(): void;
|
|
|
|
|
|
|
|
abstract public function run(): void;
|
|
|
|
|
|
|
|
abstract public function shutdown(): void;
|
|
|
|
|
2021-07-27 02:59:38 +12:00
|
|
|
const MAX_ATTEMPTS = 10;
|
|
|
|
const SLEEP_TIME = 2;
|
|
|
|
|
2021-12-28 01:45:23 +13:00
|
|
|
const DATABASE_PROJECT = 'project';
|
2021-07-27 02:59:38 +12:00
|
|
|
const DATABASE_CONSOLE = 'console';
|
|
|
|
|
2021-06-12 02:20:18 +12:00
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:09:04 +12:00
|
|
|
public function perform(): void
|
2021-06-12 02:20:18 +12:00
|
|
|
{
|
|
|
|
$this->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
$this->shutdown();
|
|
|
|
}
|
2021-07-26 11:14:19 +12:00
|
|
|
/**
|
|
|
|
* Get internal project database
|
|
|
|
* @param string $projectId
|
|
|
|
* @return Database
|
|
|
|
*/
|
2021-12-28 01:45:23 +13:00
|
|
|
protected function getProjectDB(string $projectId): Database
|
2021-07-26 11:14:19 +12:00
|
|
|
{
|
2021-12-28 01:45:23 +13:00
|
|
|
return $this->getDB(self::DATABASE_PROJECT, $projectId);
|
2021-07-26 11:14:19 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get console database
|
|
|
|
* @return Database
|
|
|
|
*/
|
|
|
|
protected function getConsoleDB(): Database
|
2021-07-27 02:59:38 +12:00
|
|
|
{
|
|
|
|
return $this->getDB(self::DATABASE_CONSOLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get console database
|
|
|
|
* @param string $type One of (internal, external, console)
|
|
|
|
* @param string $projectId of internal or external DB
|
|
|
|
* @return Database
|
|
|
|
*/
|
|
|
|
private function getDB($type, $projectId = ''): Database
|
2021-07-26 11:14:19 +12:00
|
|
|
{
|
|
|
|
global $register;
|
|
|
|
|
2021-07-27 02:59:38 +12:00
|
|
|
$namespace = '';
|
|
|
|
$sleep = self::SLEEP_TIME; // overwritten when necessary
|
|
|
|
|
|
|
|
switch ($type) {
|
2021-12-28 01:45:23 +13:00
|
|
|
case self::DATABASE_PROJECT:
|
2021-07-27 02:59:38 +12:00
|
|
|
if (!$projectId) {
|
|
|
|
throw new \Exception('ProjectID not provided - cannot get database');
|
|
|
|
}
|
2021-12-24 13:27:55 +13:00
|
|
|
$namespace = "_project_{$projectId}";
|
2021-07-27 02:59:38 +12:00
|
|
|
break;
|
|
|
|
case self::DATABASE_CONSOLE:
|
2021-12-24 13:27:55 +13:00
|
|
|
$namespace = "_project_console";
|
2021-07-27 02:59:38 +12:00
|
|
|
$sleep = 5; // ConsoleDB needs extra sleep time to ensure tables are created
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown database type: ' . $type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-26 11:14:19 +12:00
|
|
|
$attempts = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
$attempts++;
|
|
|
|
$cache = new Cache(new RedisCache($register->get('cache')));
|
2021-07-27 02:59:38 +12:00
|
|
|
$database = new Database(new MariaDB($register->get('db')), $cache);
|
2021-12-23 14:51:49 +13:00
|
|
|
$database->setDefaultDatabase('appwrite');
|
2021-07-27 02:59:38 +12:00
|
|
|
$database->setNamespace($namespace); // Main DB
|
2021-12-23 14:51:49 +13:00
|
|
|
if (!empty($projectId) && !$database->getDocument('projects', $projectId)->isEmpty()) {
|
|
|
|
throw new \Exception("Project does not exist: {$projectId}");
|
2021-07-26 11:14:19 +12:00
|
|
|
}
|
|
|
|
break; // leave loop if successful
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
Console::warning("Database not ready. Retrying connection ({$attempts})...");
|
2021-07-27 02:59:38 +12:00
|
|
|
if ($attempts >= self::MAX_ATTEMPTS) {
|
2021-07-26 11:14:19 +12:00
|
|
|
throw new \Exception('Failed to connect to database: '. $e->getMessage());
|
|
|
|
}
|
|
|
|
sleep($sleep);
|
|
|
|
}
|
2021-07-27 02:59:38 +12:00
|
|
|
} while ($attempts < self::MAX_ATTEMPTS);
|
2021-07-26 11:14:19 +12:00
|
|
|
|
2021-07-27 02:59:38 +12:00
|
|
|
return $database;
|
2021-07-26 11:14:19 +12:00
|
|
|
}
|
2021-03-10 21:08:17 +13:00
|
|
|
}
|