From 06c8902008442eeb90dda1fc2179698a4be09124 Mon Sep 17 00:00:00 2001 From: Antonio Carlos Ribeiro Date: Mon, 23 Sep 2024 09:41:57 +0200 Subject: [PATCH] Implement debugging level --- config/edge-flush.php | 4 ++- src/Behaviours/MakeTag.php | 14 +++++----- src/Services/Akamai/Service.php | 15 ++++++----- src/Services/BaseService.php | 18 ++++++++----- src/Services/CdnBaseService.php | 24 +++++++++-------- src/Services/CloudFront/Service.php | 42 ++++++++++++++--------------- src/Services/Entity.php | 12 ++++++--- src/Services/Tags.php | 27 ++++++++++--------- src/Services/Warmer.php | 23 ++++++++-------- src/Support/Helpers.php | 18 +++++++++---- 10 files changed, 112 insertions(+), 85 deletions(-) diff --git a/config/edge-flush.php b/config/edge-flush.php index 67e7d40..03ec22b 100644 --- a/config/edge-flush.php +++ b/config/edge-flush.php @@ -23,5 +23,7 @@ /** * Enable/disable the pacakge */ - 'debug' => env('EDGE_FLUSH_DEBUG', false), + 'debug_level' => ($level = env('EDGE_FLUSH_DEBUG_LEVEL', 0)), + + 'debug' => env('EDGE_FLUSH_DEBUG', $level > 0), ]; diff --git a/src/Behaviours/MakeTag.php b/src/Behaviours/MakeTag.php index 23bda54..0ae4b19 100644 --- a/src/Behaviours/MakeTag.php +++ b/src/Behaviours/MakeTag.php @@ -1,4 +1,6 @@ -getCDNCacheTag($key, $type) : $this->getCDNCacheTagFromModel($model, $key, $type); } catch (\Exception $exception) { - Helpers::debug("Exception on makeModelName: ".$exception->getMessage()); + Helpers::debug('MAKE-TAG:EXCEPTION: on makeModelName: ' . $exception->getMessage()); return null; } @@ -66,9 +68,7 @@ public function tagIsExcluded(string $tag): bool /** * @param callable(string $pattern): boolean $pattern */ - return $this->excludedModels->contains( - fn(string $pattern) => EdgeFlush::match($pattern, $tag), - ); + return $this->excludedModels->contains(fn(string $pattern) => EdgeFlush::match($pattern, $tag)); } public function tagIsNotExcluded(string $tag): bool @@ -147,9 +147,9 @@ protected function attributeMustBeIgnored(Model|string $model, string $attribute { $model = $model instanceof Model ? get_class($model) : $model; - $attributes = Helpers::configArray("edge-flush.invalidations.attributes.ignore", []); + $attributes = Helpers::configArray('edge-flush.invalidations.attributes.ignore', []); - $ignore = array_merge($attributes[$model] ?? [], ($attributes['*'] ?? [])); + $ignore = array_merge($attributes[$model] ?? [], $attributes['*'] ?? []); return in_array($attribute, $ignore); } diff --git a/src/Services/Akamai/Service.php b/src/Services/Akamai/Service.php index b858eea..931274f 100644 --- a/src/Services/Akamai/Service.php +++ b/src/Services/Akamai/Service.php @@ -1,4 +1,6 @@ -setUrls($invalidation); } - $urls = $invalidation->urls() + $urls = $invalidation + ->urls() ->map(function ($item) { return $item instanceof Url ? $item->url_hash : $item; }) @@ -104,7 +107,9 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul 'objects' => $urls->toArray(), ]; - Helpers::debug('[AKAMAI] dispatching invalidations for ' . $urls->count() . ' urls'); + Helpers::debug('AKAMAI: dispatching invalidations for ' . $urls->count() . ' urls'); + + Helpers::debug('AKAMAI: invalidating these URLs: ' . json_encode($body), 4); // Create an instance of the Timer $timer = new Timer(); @@ -143,9 +148,7 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul public function isProperlyConfigured(): bool { - return filled($this->getClientToken()) - && filled($this->getClientSecret()) - && filled($this->getAccessToken()); + return filled($this->getClientToken()) && filled($this->getClientSecret()) && filled($this->getAccessToken()); } public function getClientToken(): string|null diff --git a/src/Services/BaseService.php b/src/Services/BaseService.php index e51c720..11f2f6f 100644 --- a/src/Services/BaseService.php +++ b/src/Services/BaseService.php @@ -1,4 +1,6 @@ -getCachableMatrix($response))); + Helpers::debug( + 'RESPONSE:CACHABLE-MATRIX: ' . json_encode(EdgeFlush::cacheControl()->getCachableMatrix($response)), + 3, + ); return $this->addHeadersToResponse( $response, @@ -99,9 +105,9 @@ public function getInvalidationPathsForTags(Invalidation $invalidation): Collect public function addHeadersFromRequest(Response $response): void { - (new Collection(Helpers::configArray('edge-flush.strategies.headers.from-request')))->each(function (string $header) use ( - $response - ) { + (new Collection(Helpers::configArray('edge-flush.strategies.headers.from-request')))->each(function ( + string $header + ) use ($response) { if (filled($value = request()->header($header))) { $response->headers->set($header, $value); } diff --git a/src/Services/CdnBaseService.php b/src/Services/CdnBaseService.php index 518c431..0ef4d76 100644 --- a/src/Services/CdnBaseService.php +++ b/src/Services/CdnBaseService.php @@ -1,4 +1,6 @@ -createInvalidationRequest( - Helpers::configArray('edge-flush.services.'.static::$serviceName.'.invalidate_all_paths'), + Helpers::configArray('edge-flush.services.' . static::$serviceName . '.invalidate_all_paths'), ); } @@ -80,10 +82,10 @@ public function invalidatePaths(Invalidation $invalidation): Invalidation { return $this->createInvalidationRequest($invalidation); } - + public function maxUrls(): int { - return Helpers::configInt('edge-flush.services.'.static::$serviceName.'.max_urls') ?? 300; + return Helpers::configInt('edge-flush.services.' . static::$serviceName . '.max_urls') ?? 300; } public function enabled(): bool @@ -92,12 +94,10 @@ public function enabled(): bool return $this->enabled; } - $enabled = EdgeFlush::enabled() && - $this->serviceIsEnabled() && - $this->isProperlyConfigured(); + $enabled = EdgeFlush::enabled() && $this->serviceIsEnabled() && $this->isProperlyConfigured(); if (!$enabled) { - Helpers::debug('Service is not enabled or not properly configured: '.static::$serviceName); + Helpers::debug('CDN-SERVICE: service is not enabled or not properly configured: ' . static::$serviceName); } return $this->enabled = $enabled; @@ -105,14 +105,16 @@ public function enabled(): bool public function serviceIsEnabled(): bool { - return Helpers::configBool('edge-flush.services.'.static::$serviceName.'.enabled', true); + return Helpers::configBool('edge-flush.services.' . static::$serviceName . '.enabled', true); } public function canInvalidateAll(): bool { return collect( - Helpers::configString('edge-flush.services.'.static::$serviceName.'.invalidate_all_paths') ?? [] - )->filter()->isNotEmpty(); + Helpers::configString('edge-flush.services.' . static::$serviceName . '.invalidate_all_paths') ?? [], + ) + ->filter() + ->isNotEmpty(); } public function createInvalidationRequest(Invalidation|array $invalidation = null): Invalidation diff --git a/src/Services/CloudFront/Service.php b/src/Services/CloudFront/Service.php index e997e0d..9e182eb 100644 --- a/src/Services/CloudFront/Service.php +++ b/src/Services/CloudFront/Service.php @@ -1,4 +1,6 @@ - Helpers::configString('edge-flush.services.'.static::$serviceName.'.region'), + 'region' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.region'), - 'version' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.sdk_version'), + 'version' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.sdk_version'), 'credentials' => [ - 'key' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.key'), - 'secret' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.secret'), + 'key' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.key'), + 'secret' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.secret'), ], ]; @@ -57,13 +59,13 @@ public function getClient(): CloudFrontClient|null } return new CloudFrontClient([ - 'region' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.region'), + 'region' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.region'), - 'version' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.sdk_version'), + 'version' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.sdk_version'), 'credentials' => [ - 'key' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.key'), - 'secret' => Helpers::configString('edge-flush.services.'.static::$serviceName.'.secret'), + 'key' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.key'), + 'secret' => Helpers::configString('edge-flush.services.' . static::$serviceName . '.secret'), ], ]); } @@ -96,20 +98,16 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul $invalidation = $this->createInvalidation($invalidation); if ($this->client() === null) { - Helpers::debug('[CLOUD FRONT]: Service is disabled.'); + Helpers::debug('CLOUD-FRONT: Service is disabled.'); return $invalidation; } $paths = $invalidation->paths()->toArray(); - Helpers::debug( - '[CLOUD FRONT]: Invalidating ' . - count($paths) . - ' path(s): (' . - (new Collection($paths))->take(20)->implode(', ') . - ')...', - ); + Helpers::debug('CLOUD-FRONT:INVALIDATION: path count: ' . count($paths)); + + Helpers::debug('CLOUD-FRONT:INVALIDATION: paths: ' . (new Collection($paths))->take(20)->implode(', '), 5); try { $response = $this->client()->createInvalidation([ @@ -124,10 +122,10 @@ public function createInvalidationRequest(Invalidation|array $invalidation = nul ]); } catch (\Exception $e) { Log::error( - '[EDGE-FLUSH] [CLOUD FRONT] Invalidation request failed: ' . - $e->getMessage() . - ' - PATHS: ' . - json_encode($paths), + '[EDGE-FLUSH] CLOUD-FRONT Invalidation request failed: ' . + $e->getMessage() . + ' - PATHS: ' . + json_encode($paths), ); return $invalidation; diff --git a/src/Services/Entity.php b/src/Services/Entity.php index 441f4eb..8ade2dd 100644 --- a/src/Services/Entity.php +++ b/src/Services/Entity.php @@ -1,4 +1,6 @@ -attributes as $key => $value) { $castTypeNew = gettype($value); $castTypeOld = gettype($this->original[$key]); - + $original = $this->encodeValueForComparison($this->original[$key], $castTypeOld); $updated = $this->encodeValueForComparison($value, $castTypeNew); @@ -146,7 +148,9 @@ public function getDirtyModelNames(): Collection } if (!isset($this->modelNames[$modelName])) { - Helpers::debug("ATTRIBUTE CHANGED: {$modelName} - old: {$original}({$castTypeOld}) - new: {$updated}({$castTypeNew})"); + Helpers::debug( + "ENTITY:ATTRIBUTE-CHANGED: {$modelName} - old: {$original}({$castTypeOld}) - new: {$updated}({$castTypeNew})", + ); $this->modelNames[$modelName] = $modelName; } @@ -164,7 +168,7 @@ public function getDirtyModelNames(): Collection } if (!isset($this->modelNames[$modelName])) { - Helpers::debug("RELATION CHANGED: {$modelName}"); + Helpers::debug("ENTITY:RELATION-CHANGED: {$modelName}"); $this->modelNames[$modelName] = $modelName; } diff --git a/src/Services/Tags.php b/src/Services/Tags.php index 52b3566..c49daf6 100644 --- a/src/Services/Tags.php +++ b/src/Services/Tags.php @@ -111,7 +111,9 @@ public function getTagsHash(Response $response, Request $request): string $url = $this->getCurrentUrl($request); if (EdgeFlush::cacheControl()->isCachable($response) && EdgeFlush::storeTagsServiceIsEnabled()) { - Helpers::debug('DISPATCH STORE-TAGS: for ' . $url . ' having ' . $this->getTags()->count() . ' tags'); + Helpers::debug( + 'TAGS:STORE-TAGS: dispatching for ' . $url . ' having ' . $this->getTags()->count() . ' tags', + ); StoreTags::dispatch($this->getTags(), $url); } @@ -147,8 +149,9 @@ public function storeCacheTags(Collection $models, string $url): void return; } - Helpers::debug("[STORE-TAGS] Creating URL and storing {$models->count()} models for $url"); - Helpers::debug('[STORE-TAGS] Tags: ' . $models->join(', ')); + Helpers::debug("TAGS:STORE-TAGS: creating URL and storing {$models->count()} models for $url"); + + Helpers::debug('TAGS:STORE-TAGS: tags: ' . $models->join(', '), 4); // debug level = 5 $indexes = Helpers::collect(); @@ -226,7 +229,7 @@ public function dispatchInvalidationsForModel(Entity $entity): void return; } - Helpers::debug('DISPATCHING for model: ' . $entity->modelName); + Helpers::debug('TAGS:DISPATCHING: dispatching for model: ' . $entity->modelName); $this->dispatchInvalidationsForCrud($entity); } @@ -240,13 +243,13 @@ protected function dispatchInvalidationsForCrud(Entity $entity): void $strategy = $this->getCrudStrategy($entity); if ($strategy->name === Constants::INVALIDATION_STRATEGY_NONE) { - Helpers::debug('NO INVALIDATION needed for model ' . $entity->modelName); + Helpers::debug('TAGS:DISPATCHING: no invalidation needed for model ' . $entity->modelName); return; } if ($strategy->name === Constants::INVALIDATION_STRATEGY_ALL) { - Helpers::debug('INVALIDATING ALL tags'); + Helpers::debug('TAGS:DISPATCHING: invalidating all tags'); $this->markAsDispatched($entity); @@ -256,7 +259,7 @@ protected function dispatchInvalidationsForCrud(Entity $entity): void } if ($strategy->name === Constants::INVALIDATION_STRATEGY_DEPENDENTS) { - Helpers::debug('INVALIDATING tags for model ' . $entity->modelName); + Helpers::debug('TAGS:DISPATCHING: invalidating tags for model ' . $entity->modelName); $invalidation = new Invalidation(); @@ -270,7 +273,7 @@ protected function dispatchInvalidationsForCrud(Entity $entity): void } if ($strategy->name === Constants::INVALIDATION_STRATEGY_URLS) { - Helpers::debug('Invalidate URLs ' . json_encode($strategy->urls)); + Helpers::debug('TAGS:DISPATCHING:INVALIDATE-URLS: ' . json_encode($strategy->urls), 2); $invalidation = new Invalidation(); @@ -449,7 +452,7 @@ protected function markTagsAsObsolete(Invalidation $invalidation): void return; } - Helpers::debug("Marking tags as obsolete: {$type} in ({$list})"); + Helpers::debug("TAGS:OBSOLETE: marking tags as obsolete: {$type} in ({$list})"); $this->dbStatement($this->markTagsAsObsoleteSql($type, $list)); } @@ -468,7 +471,7 @@ protected function markUrlsAsObsolete(Invalidation $invalidation): void return; } - Helpers::debug("Marking urls as obsolete: {$type} in " . json_encode($list)); + Helpers::debug("TAGS:OBSOLETE: marking urls as obsolete: {$type} in " . json_encode($list)); $this->dbStatement($this->markUrlsAsObsoleteSql($list)); } @@ -573,7 +576,7 @@ protected function invalidateEntireCache(Invalidation $invalidation): void return; } - Helpers::debug('INVALIDATING: entire cache...'); + Helpers::debug('TAGS:INVALIDATION: entire cache...'); $invalidation->setMustInvalidateAll(true); @@ -611,7 +614,7 @@ public function invalidateAll(bool $force = false): Invalidation $count = 0; do { - Helpers::debug('Invalidating all tags... -> ' . $count); + Helpers::debug('TAGS:INVALIDATION: all tags: ' . $count); if ($count++ > 0) { sleep(2); diff --git a/src/Services/Warmer.php b/src/Services/Warmer.php index a33859a..1dd5ea7 100644 --- a/src/Services/Warmer.php +++ b/src/Services/Warmer.php @@ -1,4 +1,6 @@ -count() . ' URLs using ' . $count . ' concurrent requests'); + Helpers::debug('WARMER: warming up ' . $urls->count() . ' URLs using ' . $count . ' concurrent requests'); while ($urls->count() > 0) { $chunk = $urls->splice(0, $count); @@ -106,7 +108,7 @@ public function dispatchExternalWarmRequests(Collection $urls): void /** @var Url $url */ foreach ($urls as $url) { - Helpers::debug("WARMING: $url->url"); + Helpers::debug("WARMER: warming up $url->url", 4); $promises[$url->url] = $this->getGuzzle()->getAsync($url->url, [ 'headers' => $this->getHeaders($url->url), @@ -117,7 +119,7 @@ public function dispatchExternalWarmRequests(Collection $urls): void $executionTime = microtime(true) - $startTime; - Helpers::debug("WARMER-ELAPSED-TIME: {$executionTime}s - URLS: {$urls->count()}"); + Helpers::debug("WARMER:ELAPSED-TIME: {$executionTime}s - URLS: {$urls->count()}"); (new Collection($responses))->each(function ($response) { if ($response['state'] === 'rejected') { @@ -128,15 +130,14 @@ public function dispatchExternalWarmRequests(Collection $urls): void $url = $context['url'] ?? 'missing url'; if ($response['reason'] instanceof GuzzleConnectException) { - Helpers::debug("WARMER-ERROR: $error - $url"); + Helpers::debug("WARMER:ERROR: $error - $url"); } else { - Helpers::debug("WARMER-REJECTED: $error - $url - " . $response['reason']->getResponse()->getBody()); + Helpers::debug("WARMER:REJECTED: $error - $url"); + Helpers::debug('WARMER:REJECTED-BODY: ' . $response['reason']->getResponse()->getBody(), 5); } } else { - Helpers::debug( - "WARMER-SUCCESS : {$response['value']->getStatusCode()} - " . - json_encode($response['value']->getHeaders()), - ); + Helpers::debug("WARMER:SUCCESS: {$response['value']->getStatusCode()}"); + Helpers::debug('WARMER:SUCCESS-HEADERS: ' . json_encode($response['value']->getHeaders()), 5); } }); } @@ -147,7 +148,7 @@ public function getGuzzle(): Guzzle return $this->guzzle; } - Helpers::debug('WARMER-GUZZLE-CONFIG: ' . json_encode($this->getGuzzleConfiguration())); + Helpers::debug('WARMER:GUZZLE-CONFIG: ' . json_encode($this->getGuzzleConfiguration()), 5); return $this->guzzle = new Guzzle($this->getGuzzleConfiguration()); } diff --git a/src/Support/Helpers.php b/src/Support/Helpers.php index 36fc13a..983be6e 100644 --- a/src/Support/Helpers.php +++ b/src/Support/Helpers.php @@ -1,4 +1,6 @@ -has($key)) { - self::debug("Config key not found: $key"); + self::debug("CONFIG: config key not found: $key"); } return config($key, $default); } } -