Skip to content

Harden the scheduled HBNJ updater - #4

Merged
ColinGamez merged 1 commit into
mainfrom
agent/reliable-daily-updater
Jul 23, 2026
Merged

Harden the scheduled HBNJ updater#4
ColinGamez merged 1 commit into
mainfrom
agent/reliable-daily-updater

Conversation

@ColinGamez

@ColinGamez ColinGamez commented Jul 23, 2026

Copy link
Copy Markdown
Owner

What changed

  • retry each region up to three times with exponential backoff
  • use fixed Japan Standard Time without optional Windows tzdata
  • let PowerShell judge Python by its real exit code instead of treating harmless native stderr as fatal
  • retain atomic validation and publishing behavior

Why

The 04:15 Scheduled Task failed before collection because Windows PowerShell promoted a harmless Python stderr warning to a terminating error. After that was exposed, the incomplete Python installation also lacked IANA timezone data. Both startup failures are removed and transient region failures now retry safely.

Checks

  • executed the actual Windows Scheduled Task end to end
  • collected and validated all 54 regions
  • Scheduled Task completed with result 0
  • native national and regional payload validation passed

Summary by Sourcery

Harden the daily HBNJ collection and update workflow to be more resilient on Windows scheduled runs.

New Features:

  • Add configurable per-region retry count and exponential backoff delay to the HBNJ collection tool.
  • Propagate retry configuration from the daily updater into the region collection process.

Bug Fixes:

  • Ensure the daily updater uses a fixed Japan Standard Time offset instead of relying on Windows IANA/tzdata timezone data.
  • Prevent Windows PowerShell from treating harmless Python stderr output as a terminating error by basing success on the process exit code.

@sourcery-ai

sourcery-ai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds configurable per-region retry handling with exponential backoff to the HBNJ daily updater, switches date handling to a fixed Japan Standard Time implementation, and hardens the Windows PowerShell wrapper to judge success by Python’s exit code while preserving existing validation and publishing flow.

Sequence diagram for per-region retry with exponential backoff in HBNJ collection

sequenceDiagram
    participant collect_hbnj_all_py as collect_hbnj_all.py_main
    participant fetch
    participant parse_region

    collect_hbnj_all_py->>collect_hbnj_all_py: for each region
    loop attempts 1..retries
        collect_hbnj_all_py->>fetch: fetch(group_id, date)
        alt fetch and parse succeed
            fetch-->>collect_hbnj_all_py: source_url, source
            collect_hbnj_all_py->>parse_region: parse_region(source, group_id, area_id, area_name, prefecture_raw, source_url)
            parse_region-->>collect_hbnj_all_py: region
            collect_hbnj_all_py->>collect_hbnj_all_py: break
        else Exception during fetch or parse_region
            collect_hbnj_all_py->>collect_hbnj_all_py: [attempt < retries] compute wait = retry_delay * 2^(attempt-1)
            collect_hbnj_all_py->>collect_hbnj_all_py: time.sleep(wait)
        end
    end
    collect_hbnj_all_py->>collect_hbnj_all_py: append area and channels
Loading

Sequence diagram for hardened Windows PowerShell wrapper around HBNJ daily updater

sequenceDiagram
    actor ScheduledTask
    participant run_hbnj_daily_ps1 as run_hbnj_daily.ps1
    participant update_hbnj_daily_py as update_hbnj_daily.py

    ScheduledTask->>run_hbnj_daily_ps1: start script
    run_hbnj_daily_ps1->>run_hbnj_daily_ps1: set PYTHONIOENCODING
    run_hbnj_daily_ps1->>run_hbnj_daily_ps1: save previousErrorActionPreference
    run_hbnj_daily_ps1->>run_hbnj_daily_ps1: set ErrorActionPreference = Continue
    run_hbnj_daily_ps1->>update_hbnj_daily_py: & py -3 tools\update_hbnj_daily.py
    update_hbnj_daily_py-->>run_hbnj_daily_ps1: LASTEXITCODE
    run_hbnj_daily_ps1->>run_hbnj_daily_ps1: $updaterExitCode = $LASTEXITCODE
    run_hbnj_daily_ps1->>run_hbnj_daily_ps1: restore ErrorActionPreference
    alt updaterExitCode == 0
        run_hbnj_daily_ps1-->>ScheduledTask: success
    else updaterExitCode != 0
        run_hbnj_daily_ps1->>run_hbnj_daily_ps1: throw "HBNJ updater exited with code $updaterExitCode"
        run_hbnj_daily_ps1-->>ScheduledTask: failure
    end
Loading

File-Level Changes

Change Details Files
Introduce per-region retry handling with exponential backoff in the HBNJ region collection workflow.
  • Add --retries and --retry-delay CLI options with validation for minimum/valid values.
  • Wrap region fetch/parse logic in a retry loop that catches generic exceptions and retries up to the configured count.
  • Log retry attempts and errors to stderr, including delay before next attempt.
  • Apply exponential backoff using the configured initial retry delay before each subsequent attempt.
tools/collect_hbnj_all.py
Propagate retry configuration from the daily updater into the region collector while keeping atomic validation/publish behavior.
  • Add --retries and --retry-delay CLI options to the daily updater with sensible defaults.
  • Compute the broadcast date using a fixed JST timezone instead of zoneinfo Asia/Tokyo.
  • Forward delay, retries, and retry-delay arguments when invoking the region collection script.
  • Leave the subsequent validate and pack steps unchanged to preserve existing atomic behavior.
tools/update_hbnj_daily.py
Make the Windows PowerShell scheduled wrapper base success/failure on the Python process exit code rather than benign stderr, avoiding spurious task failures.
  • Temporarily set $ErrorActionPreference to Continue around the Python invocation to prevent stderr from becoming a terminating error.
  • Capture the Python process exit code into a local variable after execution.
  • Restore the previous ErrorActionPreference after the Python call.
  • Throw an explicit error only when the captured exit code is non-zero, keeping logging behavior via Tee-Object unchanged.
tools/run_hbnj_daily.ps1

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ColinGamez
ColinGamez marked this pull request as ready for review July 23, 2026 22:17
@ColinGamez
ColinGamez merged commit 0a8b266 into main Jul 23, 2026
3 checks passed
@ColinGamez
ColinGamez deleted the agent/reliable-daily-updater branch July 23, 2026 22:17

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The per-region retry loop in collect_hbnj_all.py catches Exception; consider narrowing this to network/HTTP/parsing-related exceptions so that truly unexpected errors (or interrupts) still fail fast.
  • You validate --retries and --retry-delay in collect_hbnj_all.py but not in update_hbnj_daily.py; mirroring the basic validation there would prevent obviously invalid values from propagating into the downstream tool.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The per-region retry loop in `collect_hbnj_all.py` catches `Exception`; consider narrowing this to network/HTTP/parsing-related exceptions so that truly unexpected errors (or interrupts) still fail fast.
- You validate `--retries` and `--retry-delay` in `collect_hbnj_all.py` but not in `update_hbnj_daily.py`; mirroring the basic validation there would prevent obviously invalid values from propagating into the downstream tool.

## Individual Comments

### Comment 1
<location path="tools/run_hbnj_daily.ps1" line_range="14-19" />
<code_context>
+    # Windows PowerShell promotes native stderr to an ErrorRecord. This Python
+    # installation emits a harmless prefix warning on stderr, so temporarily
+    # allow the process to finish and judge success by its actual exit code.
+    $previousErrorActionPreference = $ErrorActionPreference
+    $ErrorActionPreference = "Continue"
     & py -3 "tools\update_hbnj_daily.py" *>&1 | Tee-Object -FilePath $log
-    if ($LASTEXITCODE -ne 0) {
-        throw "HBNJ updater exited with code $LASTEXITCODE"
+    $updaterExitCode = $LASTEXITCODE
+    $ErrorActionPreference = $previousErrorActionPreference
+    if ($updaterExitCode -ne 0) {
+        throw "HBNJ updater exited with code $updaterExitCode"
     }
</code_context>
<issue_to_address>
**suggestion:** ErrorActionPreference is restored only in the success path of capturing $updaterExitCode; consider guarding restoration with try/finally inside the inner block.

If an error occurs between changing `$ErrorActionPreference` and setting `$updaterExitCode` (e.g., a terminating error in the pipeline), the script will abort while still using the modified preference. Enclosing the `py` call and `$LASTEXITCODE` assignment in `try { ... } finally { $ErrorActionPreference = $previousErrorActionPreference }` ensures the original preference is always restored, even when that inner logic fails.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread tools/run_hbnj_daily.ps1
Comment on lines +14 to +19
$previousErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
& py -3 "tools\update_hbnj_daily.py" *>&1 | Tee-Object -FilePath $log
if ($LASTEXITCODE -ne 0) {
throw "HBNJ updater exited with code $LASTEXITCODE"
$updaterExitCode = $LASTEXITCODE
$ErrorActionPreference = $previousErrorActionPreference
if ($updaterExitCode -ne 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: ErrorActionPreference is restored only in the success path of capturing $updaterExitCode; consider guarding restoration with try/finally inside the inner block.

If an error occurs between changing $ErrorActionPreference and setting $updaterExitCode (e.g., a terminating error in the pipeline), the script will abort while still using the modified preference. Enclosing the py call and $LASTEXITCODE assignment in try { ... } finally { $ErrorActionPreference = $previousErrorActionPreference } ensures the original preference is always restored, even when that inner logic fails.

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.

1 participant