2020-05-05 01:34:31 +12:00
|
|
|
<?php
|
2020-07-17 00:04:06 +12:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
require_once __DIR__ . '/../worker.php';
|
|
|
|
|
|
|
|
use Utopia\Queue\Message;
|
2022-02-06 08:49:57 +13:00
|
|
|
use Appwrite\Event\Event;
|
2022-11-16 07:13:17 +13:00
|
|
|
use Appwrite\Event\Func;
|
2022-02-06 08:49:57 +13:00
|
|
|
use Appwrite\Messaging\Adapter\Realtime;
|
2022-08-09 18:28:38 +12:00
|
|
|
use Appwrite\Usage\Stats;
|
2022-02-06 08:49:57 +13:00
|
|
|
use Appwrite\Utopia\Response\Model\Execution;
|
2022-11-16 17:17:46 +13:00
|
|
|
use Domnikl\Statsd\Client;
|
2022-02-06 08:49:57 +13:00
|
|
|
use Executor\Executor;
|
2020-07-20 02:43:59 +12:00
|
|
|
use Utopia\App;
|
2020-05-10 10:12:00 +12:00
|
|
|
use Utopia\CLI\Console;
|
2020-05-05 01:34:31 +12:00
|
|
|
use Utopia\Config\Config;
|
2021-05-05 09:45:41 +12:00
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\Document;
|
2022-08-15 02:22:38 +12:00
|
|
|
use Utopia\Database\ID;
|
2022-08-15 23:24:31 +12:00
|
|
|
use Utopia\Database\Permission;
|
2022-08-11 01:43:05 +12:00
|
|
|
use Utopia\Database\Query;
|
2022-08-15 23:24:31 +12:00
|
|
|
use Utopia\Database\Role;
|
2022-11-10 06:01:43 +13:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
|
|
|
use Utopia\Logger\Log;
|
2022-11-16 17:17:46 +13:00
|
|
|
use Utopia\Queue\Adapter\Swoole;
|
|
|
|
use Utopia\Queue\Server;
|
2022-11-10 06:01:43 +13:00
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
Authorization::setDefaultStatus(false);
|
|
|
|
|
2022-11-16 05:03:42 +13:00
|
|
|
global $connection;
|
2022-11-10 06:01:43 +13:00
|
|
|
global $workerNumber;
|
2022-11-16 23:33:11 +13:00
|
|
|
|
2022-11-16 17:17:46 +13:00
|
|
|
$adapter = new Swoole($connection, $workerNumber, Event::FUNCTIONS_QUEUE_NAME);
|
|
|
|
$server = new Server($adapter);
|
2022-11-10 06:01:43 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
Server::setResource('execute', function () {
|
|
|
|
return function (
|
|
|
|
Document $project,
|
|
|
|
Document $function,
|
|
|
|
Database $dbForProject,
|
2022-11-16 19:08:01 +13:00
|
|
|
Func $queueForFunctions,
|
2022-11-16 17:40:34 +13:00
|
|
|
string $trigger,
|
|
|
|
string $executionId = null,
|
|
|
|
string $event = null,
|
|
|
|
string $eventData = null,
|
|
|
|
string $data = null,
|
|
|
|
?Document $user = null,
|
|
|
|
string $jwt = null,
|
|
|
|
Client $statsd
|
|
|
|
) {
|
|
|
|
|
|
|
|
$user ??= new Document();
|
|
|
|
$functionId = $function->getId();
|
|
|
|
$deploymentId = $function->getAttribute('deployment', '');
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Check if deployment exists */
|
|
|
|
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($deployment->getAttribute('resourceId') !== $functionId) {
|
|
|
|
throw new Exception('Deployment not found. Create deployment before trying to execute a function');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($deployment->isEmpty()) {
|
|
|
|
throw new Exception('Deployment not found. Create deployment before trying to execute a function');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Check if build has exists */
|
|
|
|
$build = $dbForProject->getDocument('builds', $deployment->getAttribute('buildId', ''));
|
|
|
|
if ($build->isEmpty()) {
|
|
|
|
throw new Exception('Build not found');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($build->getAttribute('status') !== 'ready') {
|
|
|
|
throw new Exception('Build not ready');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Check if runtime is supported */
|
|
|
|
$runtimes = Config::getParam('runtimes', []);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if (!\array_key_exists($function->getAttribute('runtime'), $runtimes)) {
|
|
|
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
$runtime = $runtimes[$function->getAttribute('runtime')];
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Create execution or update execution status */
|
|
|
|
$execution = $dbForProject->getDocument('executions', $executionId ?? '');
|
2022-11-10 06:01:43 +13:00
|
|
|
if ($execution->isEmpty()) {
|
2022-11-16 17:40:34 +13:00
|
|
|
$executionId = ID::unique();
|
|
|
|
$execution = $dbForProject->createDocument('executions', new Document([
|
|
|
|
'$id' => $executionId,
|
|
|
|
'$permissions' => $user->isEmpty() ? [] : [Permission::read(Role::user($user->getId()))],
|
|
|
|
'functionId' => $functionId,
|
|
|
|
'deploymentId' => $deploymentId,
|
|
|
|
'trigger' => $trigger,
|
|
|
|
'status' => 'waiting',
|
|
|
|
'statusCode' => 0,
|
|
|
|
'response' => '',
|
|
|
|
'stderr' => '',
|
|
|
|
'duration' => 0.0,
|
|
|
|
'search' => implode(' ', [$functionId, $executionId]),
|
|
|
|
]));
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($execution->isEmpty()) {
|
|
|
|
throw new Exception('Failed to create or read execution');
|
|
|
|
}
|
2022-11-10 06:01:43 +13:00
|
|
|
}
|
2022-11-16 17:40:34 +13:00
|
|
|
$execution->setAttribute('status', 'processing');
|
|
|
|
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($build->getAttribute('status') !== 'ready') {
|
|
|
|
throw new Exception('Build not ready');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Check if runtime is supported */
|
|
|
|
$runtimes = Config::getParam('runtimes', []);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if (!\array_key_exists($function->getAttribute('runtime'), $runtimes)) {
|
|
|
|
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
$runtime = $runtimes[$function->getAttribute('runtime')];
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Create execution or update execution status */
|
|
|
|
$execution = $dbForProject->getDocument('executions', $executionId ?? '');
|
2022-11-10 06:01:43 +13:00
|
|
|
if ($execution->isEmpty()) {
|
2022-11-16 17:40:34 +13:00
|
|
|
$executionId = ID::unique();
|
|
|
|
$execution = $dbForProject->createDocument('executions', new Document([
|
|
|
|
'$id' => $executionId,
|
|
|
|
'$permissions' => $user->isEmpty() ? [] : [Permission::read(Role::user($user->getId()))],
|
|
|
|
'functionId' => $functionId,
|
|
|
|
'deploymentId' => $deploymentId,
|
|
|
|
'trigger' => $trigger,
|
|
|
|
'status' => 'waiting',
|
|
|
|
'statusCode' => 0,
|
|
|
|
'response' => '',
|
|
|
|
'stderr' => '',
|
|
|
|
'duration' => 0.0,
|
|
|
|
'search' => implode(' ', [$functionId, $executionId]),
|
|
|
|
]));
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
if ($execution->isEmpty()) {
|
|
|
|
throw new Exception('Failed to create or read execution');
|
|
|
|
}
|
2022-11-10 06:01:43 +13:00
|
|
|
}
|
2022-11-16 17:40:34 +13:00
|
|
|
$execution->setAttribute('status', 'processing');
|
|
|
|
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 23:33:11 +13:00
|
|
|
$vars = array_reduce($function->getAttribute('vars', []), function (array $carry, Document $var) {
|
2022-11-16 17:40:34 +13:00
|
|
|
$carry[$var->getAttribute('key')] = $var->getAttribute('value');
|
|
|
|
return $carry;
|
|
|
|
}, []);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Collect environment variables */
|
|
|
|
$vars = \array_merge($vars, [
|
|
|
|
'APPWRITE_FUNCTION_ID' => $functionId,
|
|
|
|
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name'),
|
|
|
|
'APPWRITE_FUNCTION_DEPLOYMENT' => $deploymentId,
|
|
|
|
'APPWRITE_FUNCTION_TRIGGER' => $trigger,
|
|
|
|
'APPWRITE_FUNCTION_PROJECT_ID' => $project->getId(),
|
|
|
|
'APPWRITE_FUNCTION_RUNTIME_NAME' => $runtime['name'] ?? '',
|
|
|
|
'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'] ?? '',
|
|
|
|
'APPWRITE_FUNCTION_EVENT' => $event ?? '',
|
|
|
|
'APPWRITE_FUNCTION_EVENT_DATA' => $eventData ?? '',
|
|
|
|
'APPWRITE_FUNCTION_DATA' => $data ?? '',
|
|
|
|
'APPWRITE_FUNCTION_USER_ID' => $user->getId() ?? '',
|
|
|
|
'APPWRITE_FUNCTION_JWT' => $jwt ?? '',
|
|
|
|
]);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Execute function */
|
|
|
|
$executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
|
|
|
|
try {
|
|
|
|
$executionResponse = $executor->createExecution(
|
|
|
|
projectId: $project->getId(),
|
|
|
|
deploymentId: $deployment->getId(),
|
|
|
|
payload: $vars['APPWRITE_FUNCTION_DATA'] ?? '',
|
|
|
|
variables: $vars,
|
|
|
|
timeout: $function->getAttribute('timeout', 0),
|
|
|
|
image: $runtime['image'],
|
|
|
|
source: $build->getAttribute('outputPath', ''),
|
|
|
|
entrypoint: $deployment->getAttribute('entrypoint', ''),
|
|
|
|
);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Update execution status */
|
|
|
|
$execution
|
|
|
|
->setAttribute('status', $executionResponse['status'])
|
|
|
|
->setAttribute('statusCode', $executionResponse['statusCode'])
|
|
|
|
->setAttribute('response', $executionResponse['response'])
|
|
|
|
->setAttribute('stdout', $executionResponse['stdout'])
|
|
|
|
->setAttribute('stderr', $executionResponse['stderr'])
|
|
|
|
->setAttribute('duration', $executionResponse['duration']);
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$interval = (new \DateTime())->diff(new \DateTime($execution->getCreatedAt()));
|
|
|
|
$execution
|
|
|
|
->setAttribute('duration', (float)$interval->format('%s.%f'))
|
|
|
|
->setAttribute('status', 'failed')
|
|
|
|
->setAttribute('statusCode', $th->getCode())
|
|
|
|
->setAttribute('stderr', $th->getMessage());
|
|
|
|
Console::error($th->getMessage());
|
|
|
|
}
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
$execution = $dbForProject->updateDocument('executions', $executionId, $execution);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Trigger Webhook */
|
|
|
|
$executionModel = new Execution();
|
|
|
|
$executionUpdate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME);
|
|
|
|
$executionUpdate
|
|
|
|
->setProject($project)
|
|
|
|
->setUser($user)
|
|
|
|
->setEvent('functions.[functionId].executions.[executionId].update')
|
|
|
|
->setParam('functionId', $function->getId())
|
|
|
|
->setParam('executionId', $execution->getId())
|
|
|
|
->setPayload($execution->getArrayCopy(array_keys($executionModel->getRules())))
|
|
|
|
->trigger();
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Trigger Functions */
|
2022-11-16 19:08:01 +13:00
|
|
|
$queueForFunctions
|
2022-11-16 17:40:34 +13:00
|
|
|
->setData($data ?? '')
|
|
|
|
->setProject($project)
|
|
|
|
->setUser($user)
|
|
|
|
->setEvent('functions.[functionId].executions.[executionId].update')
|
|
|
|
->setParam('functionId', $function->getId())
|
|
|
|
->setParam('executionId', $execution->getId())
|
|
|
|
->trigger();
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Trigger realtime event */
|
|
|
|
$allEvents = Event::generateEvents('functions.[functionId].executions.[executionId].update', [
|
|
|
|
'functionId' => $function->getId(),
|
|
|
|
'executionId' => $execution->getId()
|
|
|
|
]);
|
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $allEvents[0],
|
|
|
|
payload: $execution
|
|
|
|
);
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $execution->getArrayCopy(),
|
|
|
|
events: $allEvents,
|
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles']
|
|
|
|
);
|
|
|
|
Realtime::send(
|
2022-11-10 06:01:43 +13:00
|
|
|
projectId: $project->getId(),
|
2022-11-16 17:40:34 +13:00
|
|
|
payload: $execution->getArrayCopy(),
|
|
|
|
events: $allEvents,
|
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles']
|
2022-11-10 06:01:43 +13:00
|
|
|
);
|
2022-11-16 18:30:57 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
/** Update usage stats */
|
|
|
|
if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') {
|
|
|
|
$usage = new Stats($statsd);
|
|
|
|
$usage
|
|
|
|
->setParam('projectId', $project->getId())
|
|
|
|
->setParam('projectInternalId', $project->getInternalId())
|
|
|
|
->setParam('functionId', $function->getId())
|
|
|
|
->setParam('executions.{scope}.compute', 1)
|
|
|
|
->setParam('executionStatus', $execution->getAttribute('status', ''))
|
|
|
|
->setParam('executionTime', $execution->getAttribute('duration'))
|
|
|
|
->setParam('networkRequestSize', 0)
|
|
|
|
->setParam('networkResponseSize', 0)
|
|
|
|
->submit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2022-11-10 06:01:43 +13:00
|
|
|
|
|
|
|
$server->job()
|
|
|
|
->inject('message')
|
|
|
|
->inject('dbForProject')
|
2022-11-16 19:08:01 +13:00
|
|
|
->inject('queueForFunctions')
|
2022-11-16 17:17:46 +13:00
|
|
|
->inject('statsd')
|
2022-11-16 17:40:34 +13:00
|
|
|
->inject('execute')
|
2022-11-16 19:08:01 +13:00
|
|
|
->action(function (Message $message, Database $dbForProject, Func $queueForFunctions, Client $statsd, callable $execute) {
|
2022-11-16 17:17:46 +13:00
|
|
|
$payload = $message->getPayload() ?? [];
|
|
|
|
|
|
|
|
if (empty($payload)) {
|
|
|
|
throw new Exception('Missing payload');
|
|
|
|
}
|
|
|
|
|
|
|
|
var_dump(json_encode($payload));
|
|
|
|
$type = $payload['type'] ?? '';
|
|
|
|
$events = $payload['events'] ?? [];
|
|
|
|
$data = $payload['data'] ?? '';
|
2022-11-16 18:30:57 +13:00
|
|
|
$eventData = $payload['payload'] ?? '';
|
2022-11-16 17:19:35 +13:00
|
|
|
$project = new Document($payload['project'] ?? []);
|
|
|
|
$function = new Document($payload['function'] ?? []);
|
2022-11-16 17:17:46 +13:00
|
|
|
$user = new Document($payload['user'] ?? []);
|
2022-04-14 00:39:31 +12:00
|
|
|
|
2022-06-20 21:22:53 +12:00
|
|
|
if ($project->getId() === 'console') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-18 08:34:32 +12:00
|
|
|
/**
|
|
|
|
* Handle Event execution.
|
|
|
|
*/
|
|
|
|
if (!empty($events)) {
|
|
|
|
$limit = 30;
|
2022-11-16 17:17:46 +13:00
|
|
|
$sum = 30;
|
|
|
|
$offset = 0;
|
|
|
|
$functions = [];
|
|
|
|
/** @var Document[] $functions */
|
|
|
|
while ($sum >= $limit) {
|
|
|
|
$functions = $dbForProject->find('functions', [
|
|
|
|
Query::limit($limit),
|
|
|
|
Query::offset($offset),
|
|
|
|
Query::orderAsc('name'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$sum = \count($functions);
|
|
|
|
$offset = $offset + $limit;
|
2022-04-18 08:34:32 +12:00
|
|
|
|
|
|
|
Console::log('Fetched ' . $sum . ' functions...');
|
|
|
|
|
2022-11-16 17:17:46 +13:00
|
|
|
foreach ($functions as $function) {
|
2022-04-18 08:34:32 +12:00
|
|
|
if (!array_intersect($events, $function->getAttribute('events', []))) {
|
|
|
|
continue;
|
2020-08-05 17:18:45 +12:00
|
|
|
}
|
2022-04-18 08:34:32 +12:00
|
|
|
Console::success('Iterating function: ' . $function->getAttribute('name'));
|
2022-11-16 17:40:34 +13:00
|
|
|
$execute(
|
|
|
|
statsd: $statsd,
|
|
|
|
dbForProject: $dbForProject,
|
2022-11-16 18:30:57 +13:00
|
|
|
project: $project,
|
2022-11-16 17:40:34 +13:00
|
|
|
function: $function,
|
2022-11-16 19:08:01 +13:00
|
|
|
queueForFunctions: $queueForFunctions,
|
2022-11-16 18:30:57 +13:00
|
|
|
trigger: 'event',
|
|
|
|
event: $events[0],
|
|
|
|
eventData: $eventData,
|
2022-11-16 17:40:34 +13:00
|
|
|
user: $user,
|
2022-11-16 18:30:57 +13:00
|
|
|
data: null,
|
|
|
|
executionId: null,
|
2022-11-16 17:40:34 +13:00
|
|
|
jwt: null
|
|
|
|
);
|
2022-04-18 08:34:32 +12:00
|
|
|
Console::success('Triggered function: ' . $events[0]);
|
2020-08-05 08:09:01 +12:00
|
|
|
}
|
2022-04-18 08:34:32 +12:00
|
|
|
}
|
2022-02-06 08:49:57 +13:00
|
|
|
|
2022-04-18 08:34:32 +12:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Schedule and HTTP execution.
|
|
|
|
*/
|
|
|
|
switch ($type) {
|
2022-04-14 00:39:31 +12:00
|
|
|
case 'http':
|
2022-11-16 17:17:46 +13:00
|
|
|
$jwt = $payload['jwt'] ?? '';
|
|
|
|
$execution = new Document($payload['execution'] ?? []);
|
|
|
|
$user = new Document($payload['user'] ?? []);
|
2022-11-16 17:40:34 +13:00
|
|
|
$execute(
|
2022-11-16 18:30:57 +13:00
|
|
|
project: $project,
|
2022-11-16 17:40:34 +13:00
|
|
|
function: $function,
|
|
|
|
dbForProject: $dbForProject,
|
2022-11-16 19:08:01 +13:00
|
|
|
queueForFunctions: $queueForFunctions,
|
2022-11-16 17:40:34 +13:00
|
|
|
trigger: 'http',
|
2022-11-16 18:30:57 +13:00
|
|
|
executionId: $execution->getId(),
|
|
|
|
event: null,
|
2022-11-16 17:40:34 +13:00
|
|
|
eventData: null,
|
2022-11-16 18:30:57 +13:00
|
|
|
data: $data,
|
2022-11-16 17:40:34 +13:00
|
|
|
user: $user,
|
|
|
|
jwt: $jwt,
|
|
|
|
statsd: $statsd,
|
|
|
|
);
|
2020-08-04 17:23:38 +12:00
|
|
|
break;
|
|
|
|
case 'schedule':
|
2022-11-16 17:40:34 +13:00
|
|
|
$execute(
|
2022-11-16 18:30:57 +13:00
|
|
|
project: $project,
|
2022-11-16 17:40:34 +13:00
|
|
|
function: $function,
|
|
|
|
dbForProject: $dbForProject,
|
2022-11-16 19:08:01 +13:00
|
|
|
queueForFunctions: $queueForFunctions,
|
2022-11-16 17:40:34 +13:00
|
|
|
trigger: 'http',
|
2022-11-16 18:30:57 +13:00
|
|
|
executionId: null,
|
|
|
|
event: null,
|
2022-11-16 17:40:34 +13:00
|
|
|
eventData: null,
|
2022-11-16 18:30:57 +13:00
|
|
|
data: null,
|
2022-11-16 17:40:34 +13:00
|
|
|
user: null,
|
|
|
|
jwt: null,
|
|
|
|
statsd: $statsd,
|
|
|
|
);
|
2020-08-04 17:23:38 +12:00
|
|
|
break;
|
2020-07-17 00:04:06 +12:00
|
|
|
}
|
2022-11-10 06:01:43 +13:00
|
|
|
});
|
2020-08-04 17:23:38 +12:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
$server
|
|
|
|
->error()
|
|
|
|
->inject('error')
|
|
|
|
->inject('logger')
|
|
|
|
->inject('register')
|
|
|
|
->action(function ($error, $logger, $register) {
|
2022-02-06 08:49:57 +13:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
|
2022-02-06 08:49:57 +13:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
if ($error instanceof PDOException) {
|
|
|
|
throw $error;
|
2022-02-06 08:49:57 +13:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
if ($error->getCode() >= 500 || $error->getCode() === 0) {
|
|
|
|
$log = new Log();
|
|
|
|
|
|
|
|
$log->setNamespace("appwrite-worker");
|
|
|
|
$log->setServer(\gethostname());
|
|
|
|
$log->setVersion($version);
|
|
|
|
$log->setType(Log::TYPE_ERROR);
|
|
|
|
$log->setMessage($error->getMessage());
|
|
|
|
$log->setAction('appwrite-worker-functions');
|
|
|
|
$log->addTag('verboseType', get_class($error));
|
|
|
|
$log->addTag('code', $error->getCode());
|
|
|
|
$log->addExtra('file', $error->getFile());
|
|
|
|
$log->addExtra('line', $error->getLine());
|
|
|
|
$log->addExtra('trace', $error->getTraceAsString());
|
|
|
|
$log->addExtra('detailedTrace', $error->getTrace());
|
|
|
|
$log->addExtra('roles', \Utopia\Database\Validator\Authorization::$roles);
|
|
|
|
|
|
|
|
$isProduction = App::getEnv('_APP_ENV', 'development') === 'production';
|
|
|
|
$log->setEnvironment($isProduction ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING);
|
|
|
|
|
|
|
|
$logger->addLog($log);
|
2022-02-06 08:49:57 +13:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
Console::error('[Error] Type: ' . get_class($error));
|
|
|
|
Console::error('[Error] Message: ' . $error->getMessage());
|
|
|
|
Console::error('[Error] File: ' . $error->getFile());
|
|
|
|
Console::error('[Error] Line: ' . $error->getLine());
|
2022-02-06 08:49:57 +13:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
$register->get('pools')->reclaim();
|
|
|
|
});
|
2020-07-21 22:33:23 +12:00
|
|
|
|
2022-11-10 06:01:43 +13:00
|
|
|
$server->workerStart();
|
|
|
|
$server->start();
|