Skip to content

fix: clear a stale offload limit warning once the account is below the limit - #1176

Open
selul wants to merge 7 commits into
developmentfrom
bugfix/1118
Open

selul wants to merge 7 commits into
developmentfrom
bugfix/1118

Conversation

@selul

@selul selul commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Related issue: #1118

The "offload limit reached" warning stayed after the account usage dropped, with the 50,000 default as the limit. The flag behind it was only cleared when a bulk offload started, and while it was set new uploads silently stopped being offloaded. The daily sync now reconciles it with the account usage.

What changed

  • Optml_Admin::daily_sync() — the account details already include offload_limit and offloaded_images. The sync stores the limit, and clears the flag when the account is below it.
  • Upload limit check (Optml_Media_Offload::generate_image_meta())
    • stores the account's limit with the flag, as the upload-exception path already did, so the warning shows the real limit, not the 50,000 default;
    • counts the images a bulk offload still has to upload (optml_process_meta "remaining") only while that offload is going. "remaining" stays in the progress after a run, and a rollback records the images it moves back in the same place, so a single upload could set the flag although the account was far below the limit.
  • Progress of a transfer (record_process_meta()) — also stores which transfer it belongs to (offload_images or rollback_images) and whether it runs in the foreground (WP-CLI). The new get_pending_offload_count() uses that:
    • a rollback's progress never counts;
    • a dashboard offload counts while offloading_status is on (the plugin switches it off on finish, cancel and limit stop);
    • a WP-CLI offload has no such status, so the command marks its progress with the time of its last activity. Each offloaded image refreshes the mark, the command clears it when it ends (end_foreground_process_meta(), from a shutdown function, so also on an error exit), and it counts only while it is younger than 10 minutes. An interrupted command (Ctrl+C) therefore stops counting on its own, and a later upload does not bring an expired mark back;
    • progress recorded before this change has no transfer: it counts only while a dashboard offload runs.

Note

If the account really is at its limit, the next upload sets the flag again (preflight or upload exception), so clearing it daily cannot hide a real limit for long.

Tests

  • tests/test-offload-limit-sync.php (new): the daily sync clears the warning and stores the limit when usage is below it, keeps it at the limit, and changes nothing without usage details.
  • tests/test-media.php:
    • a provider with nine cases for the upload check, with 4990 images recorded and 50 of 5000 used: a rollback (with and without a stale offload status), a dashboard offload running and ended, a WP-CLI offload running, ended and interrupted an hour ago, and old progress with and without a running offload. Only a running offload trips the limit, and then the account's limit is stored;
    • a bulk transfer records which transfer its progress belongs to; a WP-CLI run marks its progress until it ends; each offloaded image refreshes that mark, and does not mark a dashboard offload; an upload an hour after an interrupted WP-CLI run clears the expired mark, and the next upload is offloaded.
  • Mutations: <= instead of < in the sync, limit not stored by the sync or the check, a rollback counting, an ended offload counting, a running WP-CLI or dashboard offload not counting, the mark never cleared, never recorded, never refreshed or never going stale, an expired mark being refreshed, and a dashboard offload getting the mark. Each fails a test.

QA

  1. On a connected site, trigger the warning (or set the offload_limit_reached setting to enabled with WP-CLI: wp eval '( new Optml_Settings() )->update( "offload_limit_reached", "enabled" );').

  2. Run the daily sync: wp cron event run optml_daily_sync.

    Expect: in WP Admin → Optimole → Settings → Image Storage, the limit warning is gone, and a new upload is offloaded.

Verification (head a34edda3)

Check Result
PHPUnit, full suite, PHP 8.4 (WP 7.1.3) PASS: 382 tests (baseline 364 + 18 new)
tests/test-media.php and tests/test-offload-limit-sync.php, each run alone PASS: 38 and 3 tests
phpcs, phpstan (build-only include errors excluded locally; CI builds assets first) PASS
Codex review (gpt-6-astra, high) Rounds 1 (a9fcc18f) and 2 (3d41c762): no findings. Round 3 (e2b4f490): the WP-CLI mark stayed after Ctrl+C, because a shutdown function does not run then (fixed: the mark now goes stale by itself). Round 4 (c16bb44c): a later upload refreshed a mark that had already expired (fixed). Rounds 5 (0906a79f), 6 (3dddf4af) and 7 (a34edda3): no findings.
pirate-bot Round 1 (a9fcc18f): changes requested, a WP-CLI offload no longer counted its pending images, and test types (fixed). Round 2 (3d41c762): changes requested, an offload that already ended still counted, and two native types (fixed). Round 3 (0906a79f): approved, with a note that one provider row dated the WP-CLI mark when the suite is built and would fail on a run slower than 10 minutes (fixed in 3dddf4af, test only). Round 4 (3dddf4af): changes requested, the return type of that provider (fixed in a34edda3). Round 5 (a34edda3): approved.
CI on a34edda3 PASS: PHPCS, PHPStan, PHPUnit on PHP 8.0–8.3, ESLint and Jest, build, translation, e2e. The e2e job passed on its third attempt: the first two failed on a Firefox lazy-load check of the Envira gallery page on the shared QA site. The commit only changes a docblock in a PHPUnit test, and e2e passed on the previous head, which has the same plugin code.

Not proven

  • Against the live account API (mocked in the tests with the fields the service sends).

🤖 Generated with Claude Code

…e limit

The "offload limit reached" flag was set by the upload check and cleared
only when a bulk offload started, so the warning stayed after the account
usage dropped (for example after duplicate sites were removed). It also
silently stopped new uploads from offloading.

- The daily sync stores the account's offload limit and clears the flag
  when the account is below it again.
- The upload check stores the account limit with the flag, so the warning
  no longer shows the 50,000 default.
- The upload check counts the images still to upload only while a bulk
  offload runs. The progress of a finished offload or of a rollback (which
  records how many images it moves back) no longer counts toward the limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pirate-bot

pirate-bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Plugin build for a34edda is ready 🛎️!

@selul
selul requested a review from pirate-bot October 11, 2026 12:52

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The new limit guard ignores the pending images of a WP-CLI bulk offload. Fix that and the two type gaps in the new tests.

Validation details
  • Files reviewed: 4/4 changed files.
  • I ran the five new tests with sandbox-phpunit-ab.sh on PHP 8.3 and the WordPress 7.1.3 test library. HEAD reported OK (5 tests, 11 assertions).
  • The base tree reported Tests: 5, Assertions: 9, Failures: 4. Four new tests detect the change.
  • The security check found no new trust boundary. The stored values come from the account API through cron or admin-only paths.
  • The SDK getUsage() reads the same offloaded_images and offload_limit fields as the daily sync.
Untested areas
  • test_daily_sync_without_usage_details_keeps_the_warning also passes on the base tree. It guards only the branch where nothing changes.
  • The live account API response was not captured. The tests use a mocked response.

🤖 Automated review · run code-review-agent_6acb86aca88ff0.61986987.


🤖 Review agent — review posted ✅ on a9fcc18f · changes requested · 3 findings · 13 min

Run code-review-agent_6acb86aca88ff0.61986987 · trail

Comment thread inc/media_offload.php Outdated
Comment thread tests/test-offload-limit-sync.php Outdated
Comment thread tests/test-offload-limit-sync.php Outdated
…limit

The upload check counted the "remaining" images only while the dashboard
offload status was on, and `wp optimole media move_to_optimole` never
sets that status, so a CLI offload no longer stopped before the limit.

record_process_meta() now stores which transfer the progress belongs to,
and the check counts it for offloads only, from the dashboard or WP-CLI.
Progress recorded before this change has no transfer: it counts only
while a dashboard offload runs, so stale rollback progress on existing
sites stops tripping the limit.

Also types the new tests, and moves the media tests away from the place
where two other open PRs add theirs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@selul
selul requested a review from pirate-bot October 11, 2026 14:19

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The WP-CLI fix works, but progress from a stopped offload now always counts. Fix that and add two native types.

Validation details
  • Files reviewed: 5/5 changed files. The new checks cover the 4 files changed since the prior review.
  • The new and changed tests ran on HEAD and on a base tree with sandbox-phpunit-ab.sh, PHP 8.3. HEAD passed 9 tests. The base tree reported 2 errors and 6 failures, and only the sync test without usage details passed.
  • The focused limit test passed on HEAD: cd /repo; set -o pipefail; phpunit --do-not-cache-result --filter 'Test_Media::test_only_a_pending_offload_counts_toward… printed OK (4 tests, 10 assertions) (run R3)
  • The security check comes from run code-review-agent_6acb86aca88ff0.61986987 and was not rerun. The new action key comes only from internal callers.
Untested areas
  • The WP-CLI command cannot run under PHPUnit. Its path was traced in the source only.
  • The live account API response was not captured.

🤖 Automated review · run code-review-agent_6acb9afce22107.31181124.


🤖 Review agent — review posted ✅ on 3d41c762 · changes requested · 3 findings · 15 min

Run code-review-agent_6acb9afce22107.31181124 · trail

Comment thread inc/media_offload.php Outdated
Comment thread inc/media_offload.php Outdated
Comment thread inc/media_offload.php Outdated
selul and others added 3 commits October 11, 2026 18:13
"remaining" stays in the progress after an offload finished, was
cancelled or stopped at the limit, so it kept counting toward the limit
of later uploads: after the daily sync cleared the warning, the next
upload set it again.

It now counts only while the offload is going: a dashboard offload while
its status is on, and one from WP-CLI while the command runs. The
command marks its progress as a foreground run and clears the mark when
it ends (shutdown function, so also on an error exit). A rollback never
counts.

Also adds native types to record_process_meta() and the new helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…terrupted

The shutdown function that clears the mark does not run on Ctrl+C or
SIGTERM, so an interrupted command kept counting its pending images
toward the offload limit.

The mark is now the time of the run's last activity: each offloaded image
refreshes it, and it counts only while it is younger than the lifetime of
a transfer step (10 minutes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…LI offload

The mark of a WP-CLI offload expires when the command stops refreshing it.
A later offloaded image refreshed the mark even after it had expired, which
made the stale progress count again. The mark is now refreshed only while it
is still alive and cleared otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@selul
selul requested a review from pirate-bot October 11, 2026 16:41

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

The PR passes. The three earlier findings are fixed, and the new commit adds no new problem.

Validation details
  • Files reviewed: 5/5 changed files. New checks cover the 3 files changed since 3d41c76.
  • sandbox-phpunit-ab.sh ran the new and changed tests on PHP 8.3 against pr-base. HEAD reported OK (18 tests, 42 assertions). The base tree reported 13 assertion failures and 4 missing-method errors.
  • The same script ran against 3d41c76. HEAD reported OK (11 tests, 27 assertions). The base tree failed the ended-offload, interrupted-run and refresh cases.
  • The security check from run code-review-agent_6acb86aca88ff0.61986987 was not rerun. The new commit changes no trust boundary.
Untested areas
  • The WP-CLI command was not executed. Its path was traced in the source only.
  • The provider row 'WP-CLI offload that is running' at tests/test-media.php:789 takes time() when the suite is built. The row fails when more than 600 seconds pass before it runs. The full-suite timing in CI was not measured.
  • The live account API response was not captured.

🤖 Automated review · run code-review-agent_6acbbc3beacce9.38528771.


🤖 Review agent — review posted ✅ on 0906a79f · approved · 0 findings · 17 min

Run code-review-agent_6acbbc3beacce9.38528771 · trail

…built

Data providers run when the suite is built. The 'running WP-CLI offload' row
took time() there, so it would fail once more than ten minutes pass before
the test runs. The provider now gives the age of the mark and the test dates it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@selul
selul requested a review from pirate-bot October 11, 2026 17:28

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes requested

The new commit fixes the time-dependent provider rows. Update the provider's @return shape for the new fourth element.

Validation details
  • Files reviewed: 1/1 file changed since 0906a79. The other files were reviewed in run code-review-agent_6acbbc3beacce9.38528771 and not rerun.
  • sandbox-phpunit-ab.sh ran Test_Media::test_only_a_running_offload_counts_toward_the_offload_limit on PHP 8.3 against pr-base.
  • HEAD reported OK (9 tests, 21 assertions).
  • The base tree reported Tests: 9, Assertions: 15, Failures: 9., including both changed WP-CLI rows.
Untested areas
  • The WP-CLI command was not executed. Its path was traced in the source only.
  • The live account API response was not captured.

🤖 Automated review · run code-review-agent_6acbc7523d71f4.50596281.


🤖 Review agent — review posted ✅ on 3dddf4af · changes requested · 1 finding · 4 min

Run code-review-agent_6acbc7523d71f4.50596281 · trail

Comment thread tests/test-media.php Outdated
…vider

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@selul
selul requested a review from pirate-bot October 11, 2026 18:17

@pirate-bot pirate-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

The PR passes. The new commit declares the optional fourth provider element, which fixes the last open finding.

Validation details
  • Files reviewed: 1/1 file changed since 3dddf4a. The other files were reviewed in earlier runs.
  • The provider docblock at tests/test-media.php:780 now matches its rows and the ?int $mark_age parameter.
  • cd /repo; php -l tests/test-media.php printed No syntax errors detected in tests/test-media.php (run R1)
  • Run code-review-agent_6acbc7523d71f4.50596281 ran the provider test on PHP 8.3, not rerun. HEAD reported OK (9 tests, 21 assertions). The base tree reported nine failures.
Untested areas
  • The WP-CLI command was not executed. Its path was traced in the source only.
  • The live account API response was not captured.

🤖 Automated review · run code-review-agent_6acbd2b632da57.59390431.


🤖 Review agent — review posted ✅ on a34edda3 · approved · 0 findings · 1 min

Run code-review-agent_6acbd2b632da57.59390431 · trail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants