Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cloudflare_worker_purge
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
cloudflare_worker_purge
Commits
f5bbe1e2
Commit
f5bbe1e2
authored
6 months ago
by
David Barratt
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3467741
by davidwbarratt: When an error is thrown the status is never updated
parent
601cc959
No related branches found
No related tags found
1 merge request
!4
Update State on success/failure of each chunk.
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/Purge/Purger/CloudflareWorkerPurger.php
+57
-37
57 additions, 37 deletions
src/Plugin/Purge/Purger/CloudflareWorkerPurger.php
with
57 additions
and
37 deletions
src/Plugin/Purge/Purger/CloudflareWorkerPurger.php
+
57
−
37
View file @
f5bbe1e2
...
...
@@ -5,11 +5,10 @@ namespace Drupal\cloudflare_worker_purge\Plugin\Purge\Purger;
use
Drupal\Core\Config\Config
;
use
Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface
;
use
Drupal\purge\Plugin\Purge\Purger\PurgerBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
GuzzleHttp\ClientInterface
;
use
GuzzleHttp\Exception\ClientException
;
use
GuzzleHttp\Promise\Utils
;
use
Psr\Log\LoggerInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Cloudflare Worker purger.
...
...
@@ -65,7 +64,7 @@ class CloudflareWorkerPurger extends PurgerBase {
Config
$cloudflareConfig
,
Config
$config
,
ClientInterface
$client
,
LoggerInterface
$logger
LoggerInterface
$logger
,
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
...
...
@@ -108,6 +107,10 @@ class CloudflareWorkerPurger extends PurgerBase {
* {@inheritdoc}
*/
public
function
invalidate
(
array
$invalidations
)
{
if
(
empty
(
$invalidations
))
{
return
;
}
$url
=
$this
->
config
->
get
(
'url'
);
if
(
empty
(
$url
))
{
...
...
@@ -121,47 +124,64 @@ class CloudflareWorkerPurger extends PurgerBase {
return
;
}
if
(
empty
(
$invalidations
))
{
return
;
// Group the messages by tag.
/** @var array<string,InvalidationInterface[]> */
$grouped
=
array_reduce
(
$invalidations
,
function
(
$carry
,
$item
)
{
$expression
=
$item
->
getExpression
();
if
(
empty
(
$expression
)
||
!
is_string
(
$expression
))
{
$item
->
setState
(
InvalidationInterface
::
NOT_SUPPORTED
);
return
$carry
;
}
if
(
!
array_key_exists
(
$expression
,
$carry
))
{
$carry
[
$expression
]
=
[
$item
];
}
else
{
$carry
[
$expression
][]
=
$item
;
}
return
$carry
;
},
[]);
/** @var array<int,array<string,InvalidationInterface[]>> */
$chunks
=
array_chunk
(
$grouped
,
self
::
MAX_TAG_PURGES_PER_REQUEST
,
TRUE
);
/** @var array<string,string> */
$headers
=
[];
if
(
$this
->
cloudflareConfig
->
get
(
'zone_id'
))
{
$headers
[
'CF-Zone'
]
=
$this
->
cloudflareConfig
->
get
(
'zone_id'
);
}
if
(
$this
->
cloudflareConfig
->
get
(
'email'
))
{
$headers
[
'X-Auth-Email'
]
=
$this
->
cloudflareConfig
->
get
(
'email'
);
}
if
(
$this
->
cloudflareConfig
->
get
(
'apikey'
))
{
$headers
[
'X-Auth-Key'
]
=
$this
->
cloudflareConfig
->
get
(
'apikey'
);
}
// Get the unique tags to purge.
$tags
=
array_unique
(
array_map
(
function
(
$invalidation
)
{
return
$invalidation
->
getExpression
();
},
$invalidations
));
$promises
=
array_map
(
function
(
$chunk
)
use
(
$url
)
{
return
$this
->
client
->
postAsync
(
$url
,
[
'headers'
=>
[
'CF-Zone'
=>
$this
->
cloudflareConfig
->
get
(
'zone_id'
),
'X-Auth-Email'
=>
$this
->
cloudflareConfig
->
get
(
'email'
),
'X-Auth-Key'
=>
$this
->
cloudflareConfig
->
get
(
'apikey'
),
],
$promises
=
array_map
(
function
(
$chunk
)
use
(
$url
,
$headers
)
{
return
$this
->
client
->
requestAsync
(
'POST'
,
$url
,
[
'headers'
=>
$headers
,
'json'
=>
[
'tags'
=>
$chunk
,
'tags'
=>
array_keys
(
$chunk
)
,
],
])
->
then
(
function
(
$response
)
use
(
$chunk
)
{
return
$chunk
;
},
function
(
ClientException
$e
)
{
$this
->
logger
->
error
(
$e
->
getMessage
());
// None of the tags were resolved.
return
[];
])
->
then
(
function
()
use
(
$chunk
)
{
foreach
(
$chunk
as
$invalidations
)
{
foreach
(
$invalidations
as
$invalidation
)
{
$invalidation
->
setState
(
InvalidationInterface
::
SUCCEEDED
);
}
}
},
function
(
\Throwable
$e
)
use
(
$chunk
)
{
$this
->
logger
->
critical
(
$e
->
getMessage
());
foreach
(
$chunk
as
$invalidations
)
{
foreach
(
$invalidations
as
$invalidation
)
{
$invalidation
->
setState
(
InvalidationInterface
::
FAILED
);
}
}
});
},
array_chunk
(
$tags
,
self
::
MAX_TAG_PURGES_PER_REQUEST
)
);
},
$chunks
);
// Execute the promises.
$resolved
=
array_merge
(
...
Utils
::
unwrap
(
$promises
));
// Set the state of each invalidation.
foreach
(
$invalidations
as
$invalidation
)
{
if
(
in_array
(
$invalidation
->
getExpression
(),
$resolved
))
{
$invalidation
->
setState
(
InvalidationInterface
::
SUCCEEDED
);
}
else
{
$invalidation
->
setState
(
InvalidationInterface
::
FAILED
);
}
}
Utils
::
unwrap
(
$promises
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment