Problem / Motivation
We are processing 25k+ items across multiple Brightcove queues using drush queue:run. During long-running executions the queue workers consistently begin to fail after ~300 seconds.
Logs show that once the execution approaches the 5-minute mark, Brightcove API requests start returning 401 UNAUTHORIZED responses:
[info] Processing item 842778799 from brightcove_video_delete_queue_worker queue. [283.2 sec, 52.62 MB] [error] Invalid status code: expected 200–299, got 401. Stopping and restarting the drush queue:run process allows the queue to continue for another ~5 minutes, which indicates the Brightcove access token is expiring during the worker execution and only being refreshed on process restart.
Steps to reproduce
- Populate any Brightcove-related queue with enough items for execution to exceed 5 minutes.
- Run the queue with:
drush queue:run - Observe that after ~300s API calls begin returning 401 UNAUTHORIZED and processing fails until the process is restarted.
Proposed resolution
Detect 401 responses during queue processing and refresh the Brightcove access token in-process so long-running queue runs do not require manual restarts.
Implementation notes / considerations:
- Refresh the token via the existing Brightcove auth service (or add a small token-refresh helper) when a 401 is received or when the token is near expiry.
- Retry the failed request after a successful token refresh (with a safe retry limit to avoid tight failure loops).
Issue fork brightcove-3568814
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
nickolajWhen a 401 error occurs during queue processing, the patch now clears the cached API clients and releases the item back to the queue for retry. This allows fresh tokens to be obtained automatically on the next attempt instead of deleting items when tokens expire.
Comment #5
pablonicolasThank you for looking at it @nickolaj!
I tested the changes on my end, and unfurtunatelly it is still an issue:
But one thing that I noticed after raising this issue is that the 5-minute token won't be a problem if the queue run is triggered by Drupal cron. Since the Queue worker plugin annotation sets the cron interval to 30 seconds, it would never expire at runtime.
The solution for my case was to set a cron job at the server level to trigger the queue run every 5 minutes, with a maximum run time of 250 seconds.