1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00
appwrite/app/workers/builds.php

170 lines
6.2 KiB
PHP
Raw Normal View History

2022-01-24 11:25:46 +13:00
<?php
use Appwrite\Resque\Worker;
use Cron\CronExpression;
2022-02-04 14:29:40 +13:00
use Executor\Executor;
2022-01-24 11:25:46 +13:00
use Utopia\Database\Validator\Authorization;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Storage\Storage;
use Utopia\Database\Document;
use Utopia\Config\Config;
require_once __DIR__.'/../init.php';
2022-01-25 09:06:29 +13:00
// Disable Auth since we already validate it in the API
Authorization::disable();
2022-01-24 11:25:46 +13:00
Console::title('Builds V1 Worker');
Console::success(APP_NAME.' build worker v1 has started');
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-02-04 14:29:40 +13:00
/**
* @var Executor
*/
private $executor = null;
2022-01-24 11:25:46 +13:00
2022-01-26 12:45:41 +13:00
public function getName(): string
{
2022-01-24 11:25:46 +13:00
return "builds";
}
2022-02-04 14:29:40 +13:00
public function init(): void {
$this->executor = new Executor();
}
2022-01-24 11:25:46 +13:00
public function run(): void
{
$type = $this->args['type'] ?? '';
$projectId = $this->args['projectId'] ?? '';
2022-02-23 00:28:26 +13:00
$functionId = $this->args['resourceId'] ?? '';
2022-02-17 09:06:22 +13:00
$deploymentId = $this->args['deploymentId'] ?? '';
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-02-01 12:44:55 +13:00
Console::info("Creating build for deployment: $deploymentId");
$this->buildDeployment($projectId, $functionId, $deploymentId);
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;
}
}
protected function buildDeployment(string $projectId, string $functionId, string $deploymentId)
2022-01-24 11:25:46 +13:00
{
$dbForProject = $this->getProjectDB($projectId);
2022-01-25 09:06:29 +13:00
$function = $dbForProject->getDocument('functions', $functionId);
2022-01-24 11:25:46 +13:00
if ($function->isEmpty()) {
throw new Exception('Function not found', 404);
}
$deployment = $dbForProject->getDocument('deployments', $deploymentId);
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', '');
$build = null;
2022-02-14 10:26:36 +13:00
$startTime = \time();
2022-01-24 11:25:46 +13:00
if (empty($buildId)) {
$buildId = $dbForProject->getId();
$build = $dbForProject->createDocument('builds', new Document([
'$id' => $buildId,
'$read' => [],
'$write' => [],
2022-02-14 10:26:36 +13:00
'startTime' => $startTime,
'deploymentId' => $deploymentId,
'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' => '',
'endTime' => 0,
'duration' => 0
]));
2022-01-28 01:50:22 +13:00
$deployment->setAttribute('buildId', $buildId);
$deployment = $dbForProject->updateDocument('deployments', $deploymentId, $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-02-14 10:26:36 +13:00
$source = $deployment->getAttribute('path');
$vars = $function->getAttribute('vars', []);
$baseImage = $runtime['image'];
2022-02-14 10:26:36 +13:00
try {
$response = $this->executor->createRuntime(
projectId: $projectId,
deploymentId: $deploymentId,
2022-02-24 20:28:59 +13:00
entrypoint: $deployment->getAttribute('entrypoint'),
2022-02-14 10:26:36 +13:00
source: $source,
2022-02-18 13:43:04 +13:00
destination: APP_STORAGE_BUILDS . "/app-$projectId",
2022-02-14 10:26:36 +13:00
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']);
$build->setAttribute('stdout', $response['stdout']);
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());
$function = $dbForProject->updateDocument('functions', $functionId, $function);
}
/** Update function schedule */
$schedule = $function->getAttribute('schedule', '');
$cron = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null;
$next = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? $cron->getNextRunDate()->format('U') : 0;
$function->setAttribute('scheduleNext', (int)$next);
$function = $dbForProject->updateDocument('functions', $functionId, $function);
2022-02-14 10:26:36 +13:00
} catch (\Throwable $th) {
$endtime = \time();
$build->setAttribute('endTime', $endtime);
$build->setAttribute('duration', $endtime - $startTime);
$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-02-14 10:26:36 +13:00
}
2022-01-24 11:25:46 +13:00
}
2022-01-26 12:45:41 +13:00
public function shutdown(): void {}
2022-01-24 11:25:46 +13:00
}