Skip to content

Commit

Permalink
sync with main
Browse files Browse the repository at this point in the history
  • Loading branch information
shimonewman committed Oct 11, 2023
1 parent 893b1d0 commit 2cd4514
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 269 deletions.
2 changes: 1 addition & 1 deletion app/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@

try {
$platform->init(Service::TYPE_WORKER, [
'workersNum' => 1,
'workersNum' => strtolower($workerName) === 'databases'? 1 :swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6)),
'connection' => $pools->get('queue')->pop()->getResource(),
'workerName' => strtolower($workerName) ?? null,
]);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
"utopia-php/image": "0.5.*",
"utopia-php/locale": "0.4.*",
"utopia-php/logger": "0.3.*",
"utopia-php/messaging": "0.2.*",
"utopia-php/messaging": "0.1.*",
"utopia-php/migration": "0.3.*",
"utopia-php/orchestration": "0.9.*",
"utopia-php/platform": "dev-integrate-workers as 0.3.3",
"utopia-php/pools": "0.4.*",
"utopia-php/preloader": "0.2.*",
"utopia-php/queue": "dev-feat-get-worker-start as 0.5.3",
"utopia-php/registry": "0.5.*",
"utopia-php/storage": "0.17.*",
"utopia-php/storage": "0.14.*",
"utopia-php/swoole": "0.5.*",
"utopia-php/vcs": "0.5.*",
"utopia-php/websocket": "0.1.*",
Expand Down
1 change: 0 additions & 1 deletion src/Appwrite/Utopia/Response/Model/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function __construct()
->addRule('errors', [
'type' => self::TYPE_STRING,
'description' => 'All errors that occurred during the migration process.',
'array' => true,
'default' => [],
'example' => [],
])
Expand Down
13 changes: 4 additions & 9 deletions src/Executor/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function createRuntime(
array $variables = [],
string $command = null,
) {
$runtimeId = "$projectId-$deploymentId-build";
$runtimeId = "$projectId-$deploymentId";
$route = "/runtimes";
$params = [
'runtimeId' => $runtimeId,
Expand Down Expand Up @@ -113,7 +113,7 @@ public function getLogs(
) {
$timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900);

$runtimeId = "$projectId-$deploymentId-build";
$runtimeId = "$projectId-$deploymentId";
$route = "/runtimes/{$runtimeId}/logs";
$params = [
'timeout' => $timeout
Expand Down Expand Up @@ -177,7 +177,6 @@ public function createExecution(
string $method,
array $headers,
string $runtimeEntrypoint = null,
int $requestTimeout = null
) {
if (empty($headers['host'])) {
$headers['host'] = App::getEnv('_APP_DOMAIN', '');
Expand All @@ -203,13 +202,9 @@ public function createExecution(
'runtimeEntrypoint' => $runtimeEntrypoint,
];

// Safety timeout. Executor has timeout, and open runtime has soft timeout.
// This one shouldn't really happen, but prevents from unexpected networking behaviours.
if ($requestTimeout == null) {
$requestTimeout = $timeout + 15;
}
$timeout = (int) App::getEnv('_APP_FUNCTIONS_BUILD_TIMEOUT', 900);

$response = $this->call(self::METHOD_POST, $route, [ 'x-opr-runtime-id' => $runtimeId ], $params, true, $requestTimeout);
$response = $this->call(self::METHOD_POST, $route, [ 'x-opr-runtime-id' => $runtimeId ], $params, true, $timeout);

$status = $response['headers']['status-code'];
if ($status >= 400) {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/Services/Avatars/AvatarsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testGetImage(): array
$response = $this->client->call(Client::METHOD_GET, '/avatars/image', [
'x-appwrite-project' => $this->getProject()['$id'],
], [
'url' => 'https://appwrite.io/images/open-graph/website.png',
'url' => 'https://appwrite.io/images/apple.png',
]);

$this->assertEquals(200, $response['headers']['status-code']);
Expand Down
125 changes: 6 additions & 119 deletions tests/e2e/Services/Functions/FunctionsCustomServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function testCreate(): array
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'buckets.*.create',
'buckets.*.delete',
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
Expand All @@ -50,8 +50,8 @@ public function testCreate(): array
$this->assertEquals(true, $dateValidator->isValid($response1['body']['$updatedAt']));
$this->assertEquals('', $response1['body']['deployment']);
$this->assertEquals([
'buckets.*.create',
'buckets.*.delete',
'users.*.create',
'users.*.delete',
], $response1['body']['events']);
$this->assertEquals('0 0 1 1 *', $response1['body']['schedule']);
$this->assertEquals(10, $response1['body']['timeout']);
Expand Down Expand Up @@ -191,8 +191,8 @@ public function testList(array $data): array
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'buckets.*.create',
'buckets.*.delete',
'users.*.create',
'users.*.delete',
],
'schedule' => '0 0 1 1 *',
'timeout' => 10,
Expand Down Expand Up @@ -1231,117 +1231,4 @@ public function testGetRuntimes()
$this->assertArrayHasKey('base', $runtime);
$this->assertArrayHasKey('supports', $runtime);
}


public function testEventTrigger()
{
$timeout = 5;
$code = realpath(__DIR__ . '/../../../resources/functions') . "/php-event/code.tar.gz";
$this->packageCode('php-event');

$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'functionId' => ID::unique(),
'name' => 'Test PHP Event executions',
'runtime' => 'php-8.0',
'entrypoint' => 'index.php',
'events' => [
'users.*.create',
],
'timeout' => $timeout,
]);

$functionId = $function['body']['$id'] ?? '';

$this->assertEquals(201, $function['headers']['status-code']);

$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'entrypoint' => 'index.php',
'code' => new CURLFile($code, 'application/x-gzip', basename($code)),
'activate' => true
]);

$deploymentId = $deployment['body']['$id'] ?? '';
$this->assertEquals(202, $deployment['headers']['status-code']);

// Poll until deployment is built
while (true) {
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);

if (
$deployment['headers']['status-code'] >= 400
|| \in_array($deployment['body']['status'], ['ready', 'failed'])
) {
break;
}

\sleep(1);
}

$deployment = $this->client->call(Client::METHOD_PATCH, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), []);

$this->assertEquals(200, $deployment['headers']['status-code']);

// Wait a little for activation to finish
sleep(5);

// Create user to trigger event
$user = $this->client->call(Client::METHOD_POST, '/users', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'userId' => 'unique()',
'name' => 'Event User'
]);

$userId = $user['body']['$id'];

$this->assertEquals(201, $user['headers']['status-code']);

// Wait for execution to occur
sleep(15);

$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));

$execution = $executions['body']['executions'][0];

$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertEquals('completed', $execution['status']);
$this->assertEquals(204, $execution['responseStatusCode']);
$this->assertStringContainsString($userId, $execution['logs']);
$this->assertStringContainsString('Event User', $execution['logs']);

// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);

$this->assertEquals(204, $response['headers']['status-code']);

// Cleanup : Delete user
$response = $this->client->call(Client::METHOD_DELETE, '/users/' . $userId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);

$this->assertEquals(204, $response['headers']['status-code']);
}
}
2 changes: 1 addition & 1 deletion tests/e2e/Services/GraphQL/AvatarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function testGetInitials()
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);

$this->assertEquals(5041, \strlen($initials['body']));
$this->assertEquals(4959, \strlen($initials['body']));

return $initials['body'];
}
Expand Down
Loading

0 comments on commit 2cd4514

Please sign in to comment.