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';
|
|
|
|
|
2022-12-13 20:35:05 +13:00
|
|
|
use Appwrite\Event\Usage;
|
2022-11-10 06:01:43 +13:00
|
|
|
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;
|
|
|
|
use Appwrite\Utopia\Response\Model\Execution;
|
|
|
|
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-12-15 04:42:25 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
2022-12-15 05:04:06 +13:00
|
|
|
use Utopia\Database\Helpers\Permission;
|
2022-08-11 01:43:05 +12:00
|
|
|
use Utopia\Database\Query;
|
2022-11-10 06:01:43 +13:00
|
|
|
use Utopia\Database\Validator\Authorization;
|
2022-11-16 17:17:46 +13:00
|
|
|
use Utopia\Queue\Server;
|
2022-12-15 05:04:06 +13:00
|
|
|
use Utopia\Database\Helpers\Role;
|
2022-11-10 06:01:43 +13:00
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
Authorization::setDefaultStatus(false);
|
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
Server::setResource('execute', function () {
|
|
|
|
return function (
|
2022-11-17 02:50:12 +13:00
|
|
|
Func $queueForFunctions,
|
|
|
|
Database $dbForProject,
|
2022-12-13 20:35:05 +13:00
|
|
|
Usage $queueForUsage,
|
2022-11-16 17:40:34 +13:00
|
|
|
Document $project,
|
|
|
|
Document $function,
|
|
|
|
string $trigger,
|
|
|
|
string $data = null,
|
|
|
|
?Document $user = null,
|
|
|
|
string $jwt = null,
|
2022-11-17 02:50:12 +13:00
|
|
|
string $event = null,
|
|
|
|
string $eventData = null,
|
|
|
|
string $executionId = null,
|
2022-11-16 17:40:34 +13:00
|
|
|
) {
|
2022-12-13 20:35:05 +13:00
|
|
|
|
2022-11-16 17:40:34 +13:00
|
|
|
$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-17 02:11:00 +13:00
|
|
|
// TODO: @Meldiron Trigger executions.create event here
|
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-12-15 20:56:06 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Usage
|
|
|
|
*/
|
2022-12-15 22:45:43 +13:00
|
|
|
|
2022-12-15 20:56:06 +13:00
|
|
|
$queueForUsage
|
2023-02-03 07:16:01 +13:00
|
|
|
->addMetric(METRIC_EXECUTIONS, 1) // per project
|
|
|
|
->addMetric(str_replace('{functionInternalId}', $function->getId(), METRIC_FUNCTION_ID_EXECUTIONS), 1); // per function
|
2022-11-10 06:01:43 +13:00
|
|
|
}
|
2022-11-17 02:11:00 +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 */
|
|
|
|
try {
|
2022-11-17 02:50:12 +13:00
|
|
|
$client = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
|
|
|
|
$executionResponse = $client->createExecution(
|
2022-11-16 17:40:34 +13:00
|
|
|
projectId: $project->getId(),
|
2022-11-17 02:50:12 +13:00
|
|
|
deploymentId: $deploymentId,
|
2022-11-16 17:40:34 +13:00
|
|
|
payload: $vars['APPWRITE_FUNCTION_DATA'] ?? '',
|
|
|
|
variables: $vars,
|
|
|
|
timeout: $function->getAttribute('timeout', 0),
|
|
|
|
image: $runtime['image'],
|
2022-12-21 01:56:56 +13:00
|
|
|
source: $build->getAttribute('outputPath', ''),
|
2022-11-16 17:40:34 +13:00
|
|
|
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());
|
2022-11-23 04:36:15 +13:00
|
|
|
|
|
|
|
Console::error($th->getTraceAsString());
|
|
|
|
Console::error($th->getFile());
|
|
|
|
Console::error($th->getLine());
|
2022-11-16 17:40:34 +13:00
|
|
|
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-17 02:50:12 +13:00
|
|
|
->from($executionUpdate)
|
2022-11-16 17:40:34 +13:00
|
|
|
->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-12-13 20:35:05 +13:00
|
|
|
/** Trigger usage queue */
|
|
|
|
$queueForUsage
|
|
|
|
->setProject($project)
|
2023-02-03 07:16:01 +13:00
|
|
|
->addMetric(METRIC_EXECUTIONS_COMPUTE, (int)($execution->getAttribute('duration') * 1000))// per project
|
|
|
|
->addMetric(str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS_COMPUTE), (int)($execution->getAttribute('duration') * 1000))
|
2022-12-15 20:56:06 +13:00
|
|
|
->trigger()
|
|
|
|
;
|
2022-11-16 17:40:34 +13:00
|
|
|
};
|
|
|
|
});
|
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-12-13 20:35:05 +13:00
|
|
|
->inject('queueForUsage')
|
2022-11-16 17:40:34 +13:00
|
|
|
->inject('execute')
|
2022-12-13 20:35:05 +13:00
|
|
|
->action(function (Message $message, Database $dbForProject, Func $queueForFunctions, Usage $queueForUsage, callable $execute) {
|
2022-11-16 17:17:46 +13:00
|
|
|
$payload = $message->getPayload() ?? [];
|
|
|
|
|
|
|
|
if (empty($payload)) {
|
|
|
|
throw new Exception('Missing 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-11-17 01:44:14 +13:00
|
|
|
if (!empty($events)) {
|
|
|
|
$limit = 30;
|
|
|
|
$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;
|
|
|
|
|
|
|
|
Console::log('Fetched ' . $sum . ' functions...');
|
|
|
|
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
if (!array_intersect($events, $function->getAttribute('events', []))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Console::success('Iterating function: ' . $function->getAttribute('name'));
|
2022-12-15 20:56:06 +13:00
|
|
|
|
2022-11-17 01:44:14 +13:00
|
|
|
$execute(
|
2022-12-13 20:35:05 +13:00
|
|
|
queueForUsage: $queueForUsage,
|
2022-11-17 01:44:14 +13:00
|
|
|
dbForProject: $dbForProject,
|
|
|
|
project: $project,
|
|
|
|
function: $function,
|
|
|
|
queueForFunctions: $queueForFunctions,
|
2022-11-17 02:11:00 +13:00
|
|
|
trigger: 'event',
|
2022-11-17 01:44:14 +13:00
|
|
|
event: $events[0],
|
2022-12-10 07:51:21 +13:00
|
|
|
eventData: \is_string($eventData) ? $eventData : \json_encode($eventData),
|
2022-11-17 01:44:14 +13:00
|
|
|
user: $user,
|
|
|
|
data: null,
|
|
|
|
executionId: null,
|
|
|
|
jwt: null
|
|
|
|
);
|
|
|
|
Console::success('Triggered function: ' . $events[0]);
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 02:11:00 +13:00
|
|
|
return;
|
2022-11-17 01:44:14 +13:00
|
|
|
}
|
|
|
|
|
2022-04-18 08:34:32 +12:00
|
|
|
/**
|
|
|
|
* 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-17 02:11:00 +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,
|
2022-12-13 20:35:05 +13:00
|
|
|
queueForUsage: $queueForUsage,
|
2022-11-16 17:40:34 +13:00
|
|
|
);
|
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-17 02:11:00 +13:00
|
|
|
trigger: 'schedule',
|
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,
|
2022-12-13 20:35:05 +13:00
|
|
|
queueForUsage: $queueForUsage,
|
2022-11-16 17:40:34 +13:00
|
|
|
);
|
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->workerStart();
|
|
|
|
$server->start();
|