1
0
Fork 0
mirror of synced 2024-06-09 14:24:44 +12:00

style worker code

This commit is contained in:
Torsten Dittmann 2021-09-01 11:13:23 +02:00
parent 424e153b80
commit 68bb12ad3e
6 changed files with 166 additions and 164 deletions

View file

@ -5,10 +5,10 @@ use Utopia\Audit\Audit;
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
use Utopia\CLI\Console;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Audits V1 Worker');
Console::success(APP_NAME.' audits worker v1 has started');
Console::success(APP_NAME . ' audits worker v1 has started');
class AuditsV1 extends Worker
{
@ -30,7 +30,7 @@ class AuditsV1 extends Worker
$db = $register->get('db', true);
$adapter = new AuditAdapter($db);
$adapter->setNamespace('app_'.$projectId);
$adapter->setNamespace('app_' . $projectId);
$audit = new Audit($adapter);
@ -41,4 +41,4 @@ class AuditsV1 extends Worker
{
// ... Remove environment for this job
}
}
}

View file

@ -11,10 +11,10 @@ use Utopia\CLI\Console;
use Utopia\Config\Config;
use Utopia\Domains\Domain;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Certificates V1 Worker');
Console::success(APP_NAME.' certificates worker v1 has started');
Console::success(APP_NAME . ' certificates worker v1 has started');
class CertificatesV1 extends Worker
{
@ -55,33 +55,33 @@ class CertificatesV1 extends Worker
// Validation Args
$validateTarget = $this->args['validateTarget'] ?? true;
$validateCNAME = $this->args['validateCNAME'] ?? true;
// Options
$domain = new Domain((!empty($domain)) ? $domain : '');
$expiry = 60 * 60 * 24 * 30 * 2; // 60 days
$safety = 60 * 60; // 1 hour
$renew = (\time() + $expiry);
if(empty($domain->get())) {
if (empty($domain->get())) {
throw new Exception('Missing domain');
}
if(!$domain->isKnown() || $domain->isTest()) {
if (!$domain->isKnown() || $domain->isTest()) {
throw new Exception('Unknown public suffix for domain');
}
if($validateTarget) {
if ($validateTarget) {
$target = new Domain(App::getEnv('_APP_DOMAIN_TARGET', ''));
if(!$target->isKnown() || $target->isTest()) {
throw new Exception('Unreachable CNAME target ('.$target->get().'), please use a domain with a public suffix.');
if (!$target->isKnown() || $target->isTest()) {
throw new Exception('Unreachable CNAME target (' . $target->get() . '), please use a domain with a public suffix.');
}
}
if($validateCNAME) {
if ($validateCNAME) {
$validator = new CNAME($target->get()); // Verify Domain with DNS records
if(!$validator->isValid($domain->get())) {
if (!$validator->isValid($domain->get())) {
throw new Exception('Failed to verify domain DNS records');
}
}
@ -90,8 +90,8 @@ class CertificatesV1 extends Worker
'limit' => 1,
'offset' => 0,
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_CERTIFICATES,
'domain='.$domain->get(),
'$collection=' . Database::SYSTEM_COLLECTION_CERTIFICATES,
'domain=' . $domain->get(),
],
]);
@ -104,16 +104,18 @@ class CertificatesV1 extends Worker
$certificate = (!empty($certificate) && $certificate instanceof $certificate) ? $certificate->getArrayCopy() : [];
if(!empty($certificate)
if (
!empty($certificate)
&& isset($certificate['issueDate'])
&& (($certificate['issueDate'] + ($expiry)) > \time())) { // Check last issue time
throw new Exception('Renew isn\'t required');
&& (($certificate['issueDate'] + ($expiry)) > \time())
) { // Check last issue time
throw new Exception('Renew isn\'t required');
}
$staging = (App::isProduction()) ? '' : ' --dry-run';
$email = App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS');
if(empty($email)) {
if (empty($email)) {
throw new Exception('You must set a valid security email address (_APP_SYSTEM_SECURITY_EMAIL_ADDRESS) to issue an SSL certificate');
}
@ -121,36 +123,36 @@ class CertificatesV1 extends Worker
$stderr = '';
$exit = Console::execute("certbot certonly --webroot --noninteractive --agree-tos{$staging}"
." --email ".$email
." -w ".APP_STORAGE_CERTIFICATES
." -d {$domain->get()}", '', $stdout, $stderr);
. " --email " . $email
. " -w " . APP_STORAGE_CERTIFICATES
. " -d {$domain->get()}", '', $stdout, $stderr);
if($exit !== 0) {
throw new Exception('Failed to issue a certificate with message: '.$stderr);
if ($exit !== 0) {
throw new Exception('Failed to issue a certificate with message: ' . $stderr);
}
$path = APP_STORAGE_CERTIFICATES.'/'.$domain->get();
$path = APP_STORAGE_CERTIFICATES . '/' . $domain->get();
if(!\is_readable($path)) {
if (!\is_readable($path)) {
if (!\mkdir($path, 0755, true)) {
throw new Exception('Failed to create path...');
}
}
if(!@\rename('/etc/letsencrypt/live/'.$domain->get().'/cert.pem', APP_STORAGE_CERTIFICATES.'/'.$domain->get().'/cert.pem')) {
throw new Exception('Failed to rename certificate cert.pem: '.\json_encode($stdout));
if (!@\rename('/etc/letsencrypt/live/' . $domain->get() . '/cert.pem', APP_STORAGE_CERTIFICATES . '/' . $domain->get() . '/cert.pem')) {
throw new Exception('Failed to rename certificate cert.pem: ' . \json_encode($stdout));
}
if(!@\rename('/etc/letsencrypt/live/'.$domain->get().'/chain.pem', APP_STORAGE_CERTIFICATES.'/'.$domain->get().'/chain.pem')) {
throw new Exception('Failed to rename certificate chain.pem: '.\json_encode($stdout));
if (!@\rename('/etc/letsencrypt/live/' . $domain->get() . '/chain.pem', APP_STORAGE_CERTIFICATES . '/' . $domain->get() . '/chain.pem')) {
throw new Exception('Failed to rename certificate chain.pem: ' . \json_encode($stdout));
}
if(!@\rename('/etc/letsencrypt/live/'.$domain->get().'/fullchain.pem', APP_STORAGE_CERTIFICATES.'/'.$domain->get().'/fullchain.pem')) {
throw new Exception('Failed to rename certificate fullchain.pem: '.\json_encode($stdout));
if (!@\rename('/etc/letsencrypt/live/' . $domain->get() . '/fullchain.pem', APP_STORAGE_CERTIFICATES . '/' . $domain->get() . '/fullchain.pem')) {
throw new Exception('Failed to rename certificate fullchain.pem: ' . \json_encode($stdout));
}
if(!@\rename('/etc/letsencrypt/live/'.$domain->get().'/privkey.pem', APP_STORAGE_CERTIFICATES.'/'.$domain->get().'/privkey.pem')) {
throw new Exception('Failed to rename certificate privkey.pem: '.\json_encode($stdout));
if (!@\rename('/etc/letsencrypt/live/' . $domain->get() . '/privkey.pem', APP_STORAGE_CERTIFICATES . '/' . $domain->get() . '/privkey.pem')) {
throw new Exception('Failed to rename certificate privkey.pem: ' . \json_encode($stdout));
}
$certificate = \array_merge($certificate, [
@ -168,30 +170,30 @@ class CertificatesV1 extends Worker
$certificate = $consoleDB->createDocument($certificate);
if(!$certificate) {
if (!$certificate) {
throw new Exception('Failed saving certificate to DB');
}
if(!empty($document)) {
if (!empty($document)) {
$document = \array_merge($document, [
'updated' => \time(),
'certificateId' => $certificate->getId(),
]);
$document = $consoleDB->updateDocument($document);
if(!$document) {
if (!$document) {
throw new Exception('Failed saving domain to DB');
}
}
$config =
"tls:
$config =
"tls:
certificates:
- certFile: /storage/certificates/{$domain->get()}/fullchain.pem
keyFile: /storage/certificates/{$domain->get()}/privkey.pem";
if(!\file_put_contents(APP_STORAGE_CONFIG.'/'.$domain->get().'.yml', $config)) {
if (!\file_put_contents(APP_STORAGE_CONFIG . '/' . $domain->get() . '.yml', $config)) {
throw new Exception('Failed to save SSL configuration');
}
@ -208,4 +210,4 @@ class CertificatesV1 extends Worker
public function shutdown(): void
{
}
}
}

View file

@ -14,14 +14,14 @@ use Utopia\Config\Config;
use Utopia\Audit\Audit;
use Utopia\Audit\Adapters\MySQL as AuditAdapter;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Deletes V1 Worker');
Console::success(APP_NAME.' deletes worker v1 has started'."\n");
Console::success(APP_NAME . ' deletes worker v1 has started' . "\n");
class DeletesV1 extends Worker
{
protected Database $consoleDB = null;
protected Database $consoleDB;
public function init(): void
{
@ -53,7 +53,7 @@ class DeletesV1 extends Worker
$this->deleteMemberships($document, $projectId);
break;
default:
Console::error('No lazy delete operation available for document of type: '.$document->getCollection());
Console::error('No lazy delete operation available for document of type: ' . $document->getCollection());
break;
}
break;
@ -74,33 +74,33 @@ class DeletesV1 extends Worker
$document = new Document($this->args['document']);
$this->deleteCertificates($document);
break;
default:
Console::error('No delete operation for type: '.$type);
Console::error('No delete operation for type: ' . $type);
break;
}
}
}
public function shutdown(): void
{
}
protected function deleteDocuments(Document $document, $projectId)
protected function deleteDocuments(Document $document, $projectId)
{
$collectionId = $document->getId();
// Delete Documents in the deleted collection
$this->deleteByGroup([
'$collection='.$collectionId
], $this->getProjectDB($projectId));
'$collection=' . $collectionId
], $this->getProjectDB($projectId));
}
protected function deleteMemberships(Document $document, $projectId) {
protected function deleteMemberships(Document $document, $projectId)
{
// Delete Memberships
$this->deleteByGroup([
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'teamId='.$document->getId(),
'$collection=' . Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'teamId=' . $document->getId(),
], $this->getProjectDB($projectId));
}
@ -108,8 +108,8 @@ class DeletesV1 extends Worker
{
// Delete all DBs
$this->getConsoleDB()->deleteNamespace($document->getId());
$uploads = new Local(APP_STORAGE_UPLOADS.'/app-'.$document->getId());
$cache = new Local(APP_STORAGE_CACHE.'/app-'.$document->getId());
$uploads = new Local(APP_STORAGE_UPLOADS . '/app-' . $document->getId());
$cache = new Local(APP_STORAGE_CACHE . '/app-' . $document->getId());
// Delete all storage directories
$uploads->delete($uploads->getRoot(), true);
@ -119,7 +119,7 @@ class DeletesV1 extends Worker
protected function deleteUser(Document $document, $projectId)
{
$tokens = $document->getAttribute('tokens', []);
foreach ($tokens as $token) {
if (!$this->getProjectDB($projectId)->deleteDocument($token->getId())) {
throw new Exception('Failed to remove token from DB');
@ -136,14 +136,14 @@ class DeletesV1 extends Worker
// Delete Memberships and decrement team membership counts
$this->deleteByGroup([
'$collection='.Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'userId='.$document->getId(),
], $this->getProjectDB($projectId), function(Document $document) use ($projectId) {
'$collection=' . Database::SYSTEM_COLLECTION_MEMBERSHIPS,
'userId=' . $document->getId(),
], $this->getProjectDB($projectId), function (Document $document) use ($projectId) {
if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId');
$team = $this->getProjectDB($projectId)->getDocument($teamId);
if(!$team->isEmpty()) {
if (!$team->isEmpty()) {
$team = $this->getProjectDB($projectId)->updateDocument(\array_merge($team->getArrayCopy(), [
'sum' => \max($team->getAttribute('sum', 0) - 1, 0), // Ensure that sum >= 0
]));
@ -152,37 +152,37 @@ class DeletesV1 extends Worker
});
}
protected function deleteExecutionLogs($timestamp)
protected function deleteExecutionLogs($timestamp)
{
$this->deleteForProjectIds(function($projectId) use ($timestamp) {
$this->deleteForProjectIds(function ($projectId) use ($timestamp) {
if (!($projectDB = $this->getProjectDB($projectId))) {
throw new Exception('Failed to get projectDB for project '.$projectId);
throw new Exception('Failed to get projectDB for project ' . $projectId);
}
// Delete Executions
$this->deleteByGroup([
'$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS,
'dateCreated<'.$timestamp
'$collection=' . Database::SYSTEM_COLLECTION_EXECUTIONS,
'dateCreated<' . $timestamp
], $projectDB);
});
}
protected function deleteAbuseLogs($timestamp)
protected function deleteAbuseLogs($timestamp)
{
global $register;
if($timestamp == 0) {
if ($timestamp == 0) {
throw new Exception('Failed to delete audit logs. No timestamp provided');
}
$timeLimit = new TimeLimit("", 0, 1, $register->get('db'));
$this->deleteForProjectIds(function($projectId) use ($timeLimit, $timestamp){
$timeLimit->setNamespace('app_'.$projectId);
$abuse = new Abuse($timeLimit);
$this->deleteForProjectIds(function ($projectId) use ($timeLimit, $timestamp) {
$timeLimit->setNamespace('app_' . $projectId);
$abuse = new Abuse($timeLimit);
$status = $abuse->cleanup($timestamp);
if (!$status) {
throw new Exception('Failed to delete Abuse logs for project '.$projectId);
throw new Exception('Failed to delete Abuse logs for project ' . $projectId);
}
});
}
@ -190,16 +190,16 @@ class DeletesV1 extends Worker
protected function deleteAuditLogs($timestamp)
{
global $register;
if($timestamp == 0) {
if ($timestamp == 0) {
throw new Exception('Failed to delete audit logs. No timestamp provided');
}
$this->deleteForProjectIds(function($projectId) use ($register, $timestamp){
$this->deleteForProjectIds(function ($projectId) use ($register, $timestamp) {
$adapter = new AuditAdapter($register->get('db'));
$adapter->setNamespace('app_'.$projectId);
$adapter->setNamespace('app_' . $projectId);
$audit = new Audit($adapter);
$status = $audit->cleanup($timestamp);
if (!$status) {
throw new Exception('Failed to delete Audit logs for project'.$projectId);
throw new Exception('Failed to delete Audit logs for project' . $projectId);
}
});
}
@ -207,26 +207,25 @@ class DeletesV1 extends Worker
protected function deleteFunction(Document $document, $projectId)
{
$projectDB = $this->getProjectDB($projectId);
$device = new Local(APP_STORAGE_FUNCTIONS.'/app-'.$projectId);
$device = new Local(APP_STORAGE_FUNCTIONS . '/app-' . $projectId);
// Delete Tags
$this->deleteByGroup([
'$collection='.Database::SYSTEM_COLLECTION_TAGS,
'functionId='.$document->getId(),
], $projectDB, function(Document $document) use ($device) {
'$collection=' . Database::SYSTEM_COLLECTION_TAGS,
'functionId=' . $document->getId(),
], $projectDB, function (Document $document) use ($device) {
if ($device->delete($document->getAttribute('path', ''))) {
Console::success('Delete code tag: '.$document->getAttribute('path', ''));
}
else {
Console::error('Failed to delete code tag: '.$document->getAttribute('path', ''));
Console::success('Delete code tag: ' . $document->getAttribute('path', ''));
} else {
Console::error('Failed to delete code tag: ' . $document->getAttribute('path', ''));
}
});
// Delete Executions
$this->deleteByGroup([
'$collection='.Database::SYSTEM_COLLECTION_EXECUTIONS,
'functionId='.$document->getId(),
'$collection=' . Database::SYSTEM_COLLECTION_EXECUTIONS,
'functionId=' . $document->getId(),
], $projectDB);
}
@ -234,17 +233,16 @@ class DeletesV1 extends Worker
{
Authorization::disable();
if($database->deleteDocument($document->getId())) {
Console::success('Deleted document "'.$document->getId().'" successfully');
if ($database->deleteDocument($document->getId())) {
Console::success('Deleted document "' . $document->getId() . '" successfully');
if(is_callable($callback)) {
if (is_callable($callback)) {
$callback($document);
}
return true;
}
else {
Console::error('Failed to delete document: '.$document->getId());
} else {
Console::error('Failed to delete document: ' . $document->getId());
return false;
}
@ -260,8 +258,8 @@ class DeletesV1 extends Worker
$sum = $limit;
$executionStart = \microtime(true);
while($sum === $limit) {
while ($sum === $limit) {
$chunk++;
Authorization::disable();
@ -270,18 +268,18 @@ class DeletesV1 extends Worker
'orderType' => 'ASC',
'orderCast' => 'string',
'filters' => [
'$collection='.Database::SYSTEM_COLLECTION_PROJECTS,
'$collection=' . Database::SYSTEM_COLLECTION_PROJECTS,
],
]);
Authorization::reset();
$projectIds = array_map (function ($project) {
return $project->getId();
$projectIds = array_map(function ($project) {
return $project->getId();
}, $projects);
$sum = count($projects);
Console::info('Executing delete function for chunk #'.$chunk.'. Found '.$sum.' projects');
Console::info('Executing delete function for chunk #' . $chunk . '. Found ' . $sum . ' projects');
foreach ($projectIds as $projectId) {
$callback($projectId);
$count++;
@ -301,8 +299,8 @@ class DeletesV1 extends Worker
$sum = $limit;
$executionStart = \microtime(true);
while($sum === $limit) {
while ($sum === $limit) {
$chunk++;
Authorization::disable();
@ -319,7 +317,7 @@ class DeletesV1 extends Worker
$sum = count($results);
Console::info('Deleting chunk #'.$chunk.'. Found '.$sum.' documents');
Console::info('Deleting chunk #' . $chunk . '. Found ' . $sum . ' documents');
foreach ($results as $document) {
$this->deleteById($document, $database, $callback);
@ -338,8 +336,8 @@ class DeletesV1 extends Worker
$directory = APP_STORAGE_CERTIFICATES . '/' . $domain;
$checkTraversal = realpath($directory) === $directory;
if($domain && $checkTraversal && is_dir($directory)) {
array_map('unlink', glob($directory.'/*.*'));
if ($domain && $checkTraversal && is_dir($directory)) {
array_map('unlink', glob($directory . '/*.*'));
rmdir($directory);
Console::info("Deleted certificate files for {$domain}");
} else {
@ -348,7 +346,8 @@ class DeletesV1 extends Worker
}
/**
* @return Database;
* @return Database
* @throws Exception
*/
protected function getConsoleDB(): Database
{
@ -357,7 +356,7 @@ class DeletesV1 extends Worker
$db = $register->get('db');
$cache = $register->get('cache');
if($this->consoleDB === null) {
if (!isset($this->consoleDB)) {
$this->consoleDB = new Database();
$this->consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache));;
$this->consoleDB->setNamespace('app_console'); // Main DB
@ -368,9 +367,11 @@ class DeletesV1 extends Worker
}
/**
* @return Database;
* @param string $projectId
* @return Database
* @throws Exception
*/
protected function getProjectDB($projectId): Database
protected function getProjectDB(string $projectId): Database
{
global $register;
@ -379,9 +380,9 @@ class DeletesV1 extends Worker
$projectDB = new Database();
$projectDB->setAdapter(new RedisAdapter(new MySQLAdapter($db, $cache), $cache));
$projectDB->setNamespace('app_'.$projectId); // Main DB
$projectDB->setNamespace('app_' . $projectId); // Main DB
$projectDB->setMocks(Config::getParam('collections', []));
return $projectDB;
}
}
}

View file

@ -10,10 +10,10 @@ use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Config\Config;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Tasks V1 Worker');
Console::success(APP_NAME.' tasks worker v1 has started');
Console::success(APP_NAME . ' tasks worker v1 has started');
class TasksV1 extends Worker
{
@ -86,8 +86,7 @@ class TasksV1 extends Worker
$task
->setAttribute('next', $next)
->setAttribute('previous', \time())
;
->setAttribute('previous', \time());
ResqueScheduler::enqueueAt($next, 'v1-tasks', 'TasksV1', $task->getArrayCopy()); // Async task rescheduale
@ -101,7 +100,8 @@ class TasksV1 extends Worker
\curl_setopt($ch, CURLOPT_POSTFIELDS, '');
\curl_setopt($ch, CURLOPT_HEADER, 0);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(APP_USERAGENT,
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(
APP_USERAGENT,
App::getEnv('_APP_VERSION', 'UNKNOWN'),
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
));
@ -109,8 +109,8 @@ class TasksV1 extends Worker
$ch,
CURLOPT_HTTPHEADER,
\array_merge($headers, [
'X-'.APP_NAME.'-Task-ID: '.$task->getAttribute('$id', ''),
'X-'.APP_NAME.'-Task-Name: '.$task->getAttribute('name', ''),
'X-' . APP_NAME . '-Task-ID: ' . $task->getAttribute('$id', ''),
'X-' . APP_NAME . '-Task-Name: ' . $task->getAttribute('name', ''),
])
);
\curl_setopt($ch, CURLOPT_HEADER, true); // we want headers
@ -133,7 +133,7 @@ class TasksV1 extends Worker
$response = \curl_exec($ch);
if (false === $response) {
$errors[] = \curl_error($ch).'Failed to execute task';
$errors[] = \curl_error($ch) . 'Failed to execute task';
}
$code = \curl_getinfo($ch, CURLINFO_HTTP_CODE);
@ -149,22 +149,21 @@ class TasksV1 extends Worker
switch ($codeFamily) {
case '2':
case '3':
break;
break;
default:
$errors[] = 'Request failed with status code '.$code;
$errors[] = 'Request failed with status code ' . $code;
}
if (empty($errors)) {
$task->setAttribute('failures', 0);
$alert = 'Task "'.$task->getAttribute('name').'" Executed Successfully';
$alert = 'Task "' . $task->getAttribute('name') . '" Executed Successfully';
} else {
$task
->setAttribute('failures', $task->getAttribute('failures', 0) + 1)
->setAttribute('status', ($task->getAttribute('failures') >= $errorLimit) ? 'pause' : 'play')
;
->setAttribute('status', ($task->getAttribute('failures') >= $errorLimit) ? 'pause' : 'play');
$alert = 'Task "'.$task->getAttribute('name').'" failed to execute with the following errors: '.\implode("\n", $errors);
$alert = 'Task "' . $task->getAttribute('name') . '" failed to execute with the following errors: ' . \implode("\n", $errors);
}
$log = \json_decode($task->getAttribute('log', '{}'), true);
@ -185,8 +184,7 @@ class TasksV1 extends Worker
$task
->setAttribute('log', \json_encode($log))
->setAttribute('duration', $totalTime)
->setAttribute('delay', $delay)
;
->setAttribute('delay', $delay);
Authorization::disable();
@ -206,4 +204,4 @@ class TasksV1 extends Worker
public function shutdown(): void
{
}
}
}

View file

@ -4,10 +4,10 @@ use Appwrite\Resque\Worker;
use Utopia\App;
use Utopia\CLI\Console;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Usage V1 Worker');
Console::success(APP_NAME.' usage worker v1 has started');
Console::success(APP_NAME . ' usage worker v1 has started');
class UsageV1 extends Worker
{
@ -37,30 +37,30 @@ class UsageV1 extends Worker
$functionExecutionTime = $this->args['functionExecutionTime'] ?? 0;
$functionStatus = $this->args['functionStatus'] ?? '';
$tags = ",project={$projectId},version=".App::getEnv('_APP_VERSION', 'UNKNOWN');
$tags = ",project={$projectId},version=" . App::getEnv('_APP_VERSION', 'UNKNOWN');
// the global namespace is prepended to every key (optional)
$statsd->setNamespace('appwrite.usage');
if($httpRequest >= 1) {
$statsd->increment('requests.all'.$tags.',method='.\strtolower($httpMethod));
}
if($functionExecution >= 1) {
$statsd->increment('executions.all'.$tags.',functionId='.$functionId.',functionStatus='.$functionStatus);
$statsd->count('executions.time'.$tags.',functionId='.$functionId, $functionExecutionTime);
if ($httpRequest >= 1) {
$statsd->increment('requests.all' . $tags . ',method=' . \strtolower($httpMethod));
}
$statsd->count('network.inbound'.$tags, $networkRequestSize);
$statsd->count('network.outbound'.$tags, $networkResponseSize);
$statsd->count('network.all'.$tags, $networkRequestSize + $networkResponseSize);
if ($functionExecution >= 1) {
$statsd->increment('executions.all' . $tags . ',functionId=' . $functionId . ',functionStatus=' . $functionStatus);
$statsd->count('executions.time' . $tags . ',functionId=' . $functionId, $functionExecutionTime);
}
if($storage >= 1) {
$statsd->count('storage.all'.$tags, $storage);
$statsd->count('network.inbound' . $tags, $networkRequestSize);
$statsd->count('network.outbound' . $tags, $networkResponseSize);
$statsd->count('network.all' . $tags, $networkRequestSize + $networkResponseSize);
if ($storage >= 1) {
$statsd->count('storage.all' . $tags, $storage);
}
}
public function shutdown(): void
{
}
}
}

View file

@ -4,10 +4,10 @@ use Appwrite\Resque\Worker;
use Utopia\App;
use Utopia\CLI\Console;
require_once __DIR__.'/../workers.php';
require_once __DIR__ . '/../workers.php';
Console::title('Webhooks V1 Worker');
Console::success(APP_NAME.' webhooks worker v1 has started');
Console::success(APP_NAME . ' webhooks worker v1 has started');
class WebhooksV1 extends Worker
{
@ -45,7 +45,8 @@ class WebhooksV1 extends Worker
\curl_setopt($ch, CURLOPT_POSTFIELDS, $eventData);
\curl_setopt($ch, CURLOPT_HEADER, 0);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(APP_USERAGENT,
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(
APP_USERAGENT,
App::getEnv('_APP_VERSION', 'UNKNOWN'),
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
));
@ -54,13 +55,13 @@ class WebhooksV1 extends Worker
CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: '.\strlen($eventData),
'X-'.APP_NAME.'-Webhook-Id: '.$id,
'X-'.APP_NAME.'-Webhook-Event: '.$event,
'X-'.APP_NAME.'-Webhook-Name: '.$name,
'X-'.APP_NAME.'-Webhook-User-Id: '.$userId,
'X-'.APP_NAME.'-Webhook-Project-Id: '.$projectId,
'X-'.APP_NAME.'-Webhook-Signature: '.$signature,
'Content-Length: ' . \strlen($eventData),
'X-' . APP_NAME . '-Webhook-Id: ' . $id,
'X-' . APP_NAME . '-Webhook-Event: ' . $event,
'X-' . APP_NAME . '-Webhook-Name: ' . $name,
'X-' . APP_NAME . '-Webhook-User-Id: ' . $userId,
'X-' . APP_NAME . '-Webhook-Project-Id: ' . $projectId,
'X-' . APP_NAME . '-Webhook-Signature: ' . $signature,
]
);
@ -75,7 +76,7 @@ class WebhooksV1 extends Worker
}
if (false === \curl_exec($ch)) {
$errors[] = \curl_error($ch).' in event '.$event.' for webhook '.$name;
$errors[] = \curl_error($ch) . ' in event ' . $event . ' for webhook ' . $name;
}
\curl_close($ch);
@ -89,4 +90,4 @@ class WebhooksV1 extends Worker
public function shutdown(): void
{
}
}
}