1
0
Fork 0
mirror of synced 2024-05-17 11:12:41 +12:00
appwrite/app/workers/builds.php

251 lines
9.4 KiB
PHP
Raw Normal View History

2022-01-24 11:25:46 +13:00
<?php
2022-04-19 04:21:45 +12:00
use Appwrite\Event\Event;
2022-03-01 00:50:27 +13:00
use Appwrite\Messaging\Adapter\Realtime;
2022-01-24 11:25:46 +13:00
use Appwrite\Resque\Worker;
2022-04-19 04:21:45 +12:00
use Appwrite\Utopia\Response\Model\Deployment;
2022-01-24 11:25:46 +13:00
use Cron\CronExpression;
2022-02-04 14:29:40 +13:00
use Executor\Executor;
2022-08-09 18:28:38 +12:00
use Appwrite\Usage\Stats;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
2022-01-24 11:25:46 +13:00
use Utopia\App;
use Utopia\CLI\Console;
2022-08-15 02:22:38 +12:00
use Utopia\Database\ID;
2022-01-24 11:25:46 +13:00
use Utopia\Storage\Storage;
use Utopia\Database\Document;
use Utopia\Config\Config;
2022-08-10 21:32:48 +12:00
use Utopia\Database\Query;
2022-01-24 11:25:46 +13:00
2022-04-20 01:13:55 +12:00
require_once __DIR__ . '/../init.php';
2022-01-24 11:25:46 +13:00
Console::title('Builds V1 Worker');
2022-04-20 01:13:55 +12:00
Console::success(APP_NAME . ' build worker v1 has started');
2022-01-24 11:25:46 +13:00
2022-01-25 09:06:29 +13:00
// TODO: Executor should return appropriate response codes.
2022-01-24 11:25:46 +13:00
class BuildsV1 extends Worker
{
2022-04-20 01:13:55 +12:00
private ?Executor $executor = null;
2022-01-24 11:25:46 +13:00
2022-04-20 01:13:55 +12:00
public function getName(): string
2022-01-26 12:45:41 +13:00
{
2022-01-24 11:25:46 +13:00
return "builds";
}
2022-05-10 00:36:29 +12:00
public function init(): void
{
$this->executor = new Executor(App::getEnv('_APP_EXECUTOR_HOST'));
2022-02-04 14:29:40 +13:00
}
2022-01-24 11:25:46 +13:00
public function run(): void
{
$type = $this->args['type'] ?? '';
2022-04-20 01:13:55 +12:00
$project = new Document($this->args['project'] ?? []);
$resource = new Document($this->args['resource'] ?? []);
$deployment = new Document($this->args['deployment'] ?? []);
2022-01-24 11:25:46 +13:00
switch ($type) {
case BUILD_TYPE_DEPLOYMENT:
2022-02-17 09:06:22 +13:00
case BUILD_TYPE_RETRY:
2022-04-20 01:13:55 +12:00
Console::info('Creating build for deployment: ' . $deployment->getId());
$this->buildDeployment($project, $resource, $deployment);
2022-01-24 11:25:46 +13:00
break;
2022-01-24 11:25:46 +13:00
default:
throw new \Exception('Invalid build type');
2022-01-24 11:25:46 +13:00
break;
}
}
2022-04-20 01:13:55 +12:00
protected function buildDeployment(Document $project, Document $function, Document $deployment)
2022-01-24 11:25:46 +13:00
{
$dbForProject = $this->getProjectDB($project->getId());
2022-04-20 01:13:55 +12:00
$function = $dbForProject->getDocument('functions', $function->getId());
2022-01-24 11:25:46 +13:00
if ($function->isEmpty()) {
throw new Exception('Function not found', 404);
}
2022-04-20 01:13:55 +12:00
$deployment = $dbForProject->getDocument('deployments', $deployment->getId());
if ($deployment->isEmpty()) {
throw new Exception('Deployment not found', 404);
2022-01-24 11:25:46 +13:00
}
$runtimes = Config::getParam('runtimes', []);
$key = $function->getAttribute('runtime');
$runtime = isset($runtimes[$key]) ? $runtimes[$key] : null;
if (\is_null($runtime)) {
throw new Exception('Runtime "' . $function->getAttribute('runtime', '') . '" is not supported');
}
$buildId = $deployment->getAttribute('buildId', '');
2022-07-14 02:02:49 +12:00
$startTime = DateTime::now();
2022-01-24 11:25:46 +13:00
if (empty($buildId)) {
2022-08-15 02:22:38 +12:00
$buildId = ID::unique();
$build = $dbForProject->createDocument('builds', new Document([
'$id' => $buildId,
'$permissions' => [],
2022-02-14 10:26:36 +13:00
'startTime' => $startTime,
2022-04-20 01:13:55 +12:00
'deploymentId' => $deployment->getId(),
'status' => 'processing',
'outputPath' => '',
'runtime' => $function->getAttribute('runtime'),
'source' => $deployment->getAttribute('path'),
2022-02-18 00:40:02 +13:00
'sourceType' => App::getEnv('_APP_STORAGE_DEVICE', Storage::DEVICE_LOCAL),
'stdout' => '',
'stderr' => '',
2022-07-12 03:12:41 +12:00
'endTime' => null,
'duration' => 0
]));
2022-01-28 01:50:22 +13:00
$deployment->setAttribute('buildId', $buildId);
2022-04-20 01:13:55 +12:00
$deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), $deployment);
} else {
$build = $dbForProject->getDocument('builds', $buildId);
}
/** Request the executor to build the code... */
$build->setAttribute('status', 'building');
$build = $dbForProject->updateDocument('builds', $buildId, $build);
2022-04-19 04:21:45 +12:00
/** Trigger Webhook */
$deploymentModel = new Deployment();
2022-09-16 21:54:59 +12:00
2022-04-19 04:21:45 +12:00
$deploymentUpdate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME);
$deploymentUpdate
->setProject($project)
->setEvent('functions.[functionId].deployments.[deploymentId].update')
->setParam('functionId', $function->getId())
->setParam('deploymentId', $deployment->getId())
->setPayload($deployment->getArrayCopy(array_keys($deploymentModel->getRules())))
->trigger();
/** Trigger Functions */
$deploymentUpdate
->setClass(Event::FUNCTIONS_CLASS_NAME)
->setQueue(Event::FUNCTIONS_QUEUE_NAME)
->trigger();
/** Trigger Realtime */
$allEvents = Event::generateEvents('functions.[functionId].deployments.[deploymentId].update', [
'functionId' => $function->getId(),
'deploymentId' => $deployment->getId()
]);
2022-05-10 00:36:29 +12:00
$target = Realtime::fromPayload(
// Pass first, most verbose event pattern
event: $allEvents[0],
payload: $build,
project: $project
);
2022-03-01 00:50:27 +13:00
Realtime::send(
projectId: 'console',
payload: $build->getArrayCopy(),
2022-04-19 04:21:45 +12:00
events: $allEvents,
2022-03-01 00:50:27 +13:00
channels: $target['channels'],
roles: $target['roles']
);
2022-02-14 10:26:36 +13:00
$source = $deployment->getAttribute('path');
2022-08-10 21:32:48 +12:00
2022-08-31 00:30:52 +12:00
$vars = array_reduce($function['vars'] ?? [], function (array $carry, Document $var) {
$carry[$var->getAttribute('key')] = $var->getAttribute('value');
return $carry;
}, []);
2022-08-10 21:32:48 +12:00
$baseImage = $runtime['image'];
2022-02-14 10:26:36 +13:00
try {
$response = $this->executor->createRuntime(
2022-04-20 01:13:55 +12:00
projectId: $project->getId(),
deploymentId: $deployment->getId(),
2022-02-24 20:28:59 +13:00
entrypoint: $deployment->getAttribute('entrypoint'),
2022-02-14 10:26:36 +13:00
source: $source,
2022-04-20 01:13:55 +12:00
destination: APP_STORAGE_BUILDS . "/app-{$project->getId()}",
vars: $vars,
runtime: $key,
baseImage: $baseImage,
2022-02-18 10:40:28 +13:00
workdir: '/usr/code',
2022-02-18 13:43:04 +13:00
remove: true,
commands: [
'sh', '-c',
2022-02-19 12:46:39 +13:00
'tar -zxf /tmp/code.tar.gz -C /usr/code && \
2022-02-20 06:33:27 +13:00
cd /usr/local/src/ && ./build.sh'
]
2022-02-14 10:26:36 +13:00
);
/** Update the build document */
$build->setAttribute('endTime', $response['endTime']);
$build->setAttribute('duration', $response['duration']);
$build->setAttribute('status', $response['status']);
$build->setAttribute('outputPath', $response['outputPath']);
$build->setAttribute('stderr', $response['stderr']);
2022-05-20 11:43:35 +12:00
$build->setAttribute('stdout', $response['response']);
2022-02-17 00:43:21 +13:00
2022-10-19 14:43:40 +13:00
/* Also update the deployment buildTime */
2022-10-19 14:50:48 +13:00
$deployment->setAttribute('buildTime', $response['duration']);
2022-10-19 14:43:40 +13:00
2022-02-17 00:43:21 +13:00
Console::success("Build id: $buildId created");
/** Set auto deploy */
if ($deployment->getAttribute('activate') === true) {
$function->setAttribute('deployment', $deployment->getId());
2022-08-19 22:26:09 +12:00
$function = $dbForProject->updateDocument('functions', $function->getId(), $function);
2022-02-17 00:43:21 +13:00
}
/** Update function schedule */
$schedule = $function->getAttribute('schedule', '');
$cron = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null;
2022-07-14 02:02:49 +12:00
$next = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null;
$function->setAttribute('scheduleNext', $next);
2022-04-20 01:13:55 +12:00
$function = $dbForProject->updateDocument('functions', $function->getId(), $function);
2022-02-14 10:26:36 +13:00
} catch (\Throwable $th) {
2022-09-03 02:19:36 +12:00
$endTime = DateTime::now();
$interval = (new \DateTime($endTime))->diff(new \DateTime($startTime));
$build->setAttribute('endTime', $endTime);
$build->setAttribute('duration', $interval->format('%s') + 0);
2022-02-14 10:26:36 +13:00
$build->setAttribute('status', 'failed');
$build->setAttribute('stderr', $th->getMessage());
Console::error($th->getMessage());
2022-02-17 00:43:21 +13:00
} finally {
$build = $dbForProject->updateDocument('builds', $buildId, $build);
2022-03-01 00:50:27 +13:00
2022-05-24 02:54:50 +12:00
/**
* Send realtime Event
2022-03-01 00:50:27 +13:00
*/
2022-05-10 00:36:29 +12:00
$target = Realtime::fromPayload(
// Pass first, most verbose event pattern
event: $allEvents[0],
payload: $build,
project: $project
);
2022-03-01 00:50:27 +13:00
Realtime::send(
projectId: 'console',
payload: $build->getArrayCopy(),
2022-04-19 04:21:45 +12:00
events: $allEvents,
2022-03-01 00:50:27 +13:00
channels: $target['channels'],
roles: $target['roles']
);
2022-06-29 18:30:24 +12:00
/** Update usage stats */
global $register;
if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') {
$statsd = $register->get('statsd');
$usage = new Stats($statsd);
$usage
->setParam('projectId', $project->getId())
->setParam('functionId', $function->getId())
->setParam('builds.{scope}.compute', 1)
->setParam('buildStatus', $build->getAttribute('status', ''))
2022-08-11 18:49:20 +12:00
->setParam('buildTime', $build->getAttribute('duration'))
2022-06-29 18:30:24 +12:00
->setParam('networkRequestSize', 0)
->setParam('networkResponseSize', 0)
->submit();
}
2022-02-14 10:26:36 +13:00
}
2022-01-24 11:25:46 +13:00
}
2022-04-20 01:13:55 +12:00
public function shutdown(): void
{
}
2022-01-24 11:25:46 +13:00
}