From b19d7cb35f0bbba06879e95f2a383198d4da936a Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Thu, 27 Jun 2024 18:12:23 -0400 Subject: [PATCH] feat: Update scheduled when deleting an execution --- app/controllers/api/functions.php | 29 ++++++++++++++++--- .../Functions/FunctionsCustomServerTest.php | 29 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 09baf8c4d7..f58bdcd02f 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -11,6 +11,7 @@ use Appwrite\Event\Validator\FunctionEvent; use Appwrite\Extend\Exception; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Messaging\Adapter\Realtime; +use Appwrite\Platform\Tasks\ScheduleExecutions; use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Database\Validator\Queries\Deployments; @@ -1725,7 +1726,7 @@ App::post('/v1/functions/:functionId/executions') 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentId' => $deployment->getId(), 'trigger' => (!is_null($scheduledAt)) ? 'schedule' : 'http', - 'status' => $status, // waiting / processing / completed / failed + 'status' => $status, // waiting / processing / completed / failed / scheduled 'responseStatusCode' => 0, 'responseHeaders' => [], 'requestPath' => $path, @@ -1774,7 +1775,7 @@ App::post('/v1/functions/:functionId/executions') $dbForConsole->createDocument('schedules', new Document([ 'region' => System::getEnv('_APP_REGION', 'default'), - 'resourceType' => 'execution', + 'resourceType' => ScheduleExecutions::getSupportedResource(), 'resourceId' => $execution->getId(), 'resourceInternalId' => $execution->getInternalId(), 'resourceUpdatedAt' => DateTime::now(), @@ -2061,8 +2062,9 @@ App::delete('/v1/functions/:functionId/executions/:executionId') ->param('executionId', '', new UID(), 'Execution ID.') ->inject('response') ->inject('dbForProject') + ->inject('dbForConsole') ->inject('queueForEvents') - ->action(function (string $functionId, string $executionId, Response $response, Database $dbForProject, Event $queueForEvents) { + ->action(function (string $functionId, string $executionId, Response $response, Database $dbForProject, Database $dbForConsole, Event $queueForEvents) { $function = $dbForProject->getDocument('functions', $functionId); if ($function->isEmpty()) { @@ -2077,8 +2079,9 @@ App::delete('/v1/functions/:functionId/executions/:executionId') if ($execution->getAttribute('functionId') !== $function->getId()) { throw new Exception(Exception::EXECUTION_NOT_FOUND); } + $status = $execution->getAttribute('status'); - if (!in_array($execution->getAttribute('status'), ['completed', 'failed', 'scheduled'])) { + if (!in_array($status, ['completed', 'failed', 'scheduled'])) { throw new Exception(Exception::EXECUTION_IN_PROGRESS); } @@ -2086,6 +2089,24 @@ App::delete('/v1/functions/:functionId/executions/:executionId') throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove execution from DB'); } + if ($status === 'scheduled') { + $results = $dbForConsole->find('schedules', [ + Query::equal('resourceId', [$execution->getId()]), + Query::equal('resourceType', [ScheduleExecutions::getSupportedResource()]), + Query::equal('active', [true]), + ]); + + if (count($results) === 1) { + $schedule = $results[0]; + + $schedule + ->setAttribute('resourceUpdatedAt', DateTime::now()) + ->setAttribute('active', false); + + Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule)); + } + } + $queueForEvents ->setParam('functionId', $function->getId()) ->setParam('executionId', $execution->getId()); diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 441775c525..b7014546a2 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1038,6 +1038,35 @@ class FunctionsCustomServerTest extends Scope return $data; } + /** + * @depends testGetExecution + */ + public function testDeleteScheduledExecution($data): array + { + $futureTime = (new \DateTime())->add(new \DateInterval('PT10H'))->format('Y-m-d H:i:s'); + + $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $data['functionId'] . '/executions', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'async' => true, + 'scheduledAt' => $futureTime, + ]); + + $executionId = $execution['body']['$id'] ?? ''; + $this->assertEquals(202, $execution['headers']['status-code']); + sleep(5); + $execution = $this->client->call(Client::METHOD_DELETE, '/functions/' . $data['functionId'] . '/executions/' . $executionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(204, $execution['headers']['status-code']); + $this->assertEmpty($execution['body']); + var_dump('==========================================='); + return $data; + } + /** * @depends testGetExecution */