Skip to content

feat(api): extend every active Series each day with a timer function - #226

Merged
mwg-bender[bot] merged 2 commits into
devfrom
feat/204-series-timer
Nov 25, 2026
Merged

mwg-bender[bot] merged 2 commits into
devfrom
feat/204-series-timer

Conversation

@mwg-bender

@mwg-bender mwg-bender Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds the daily timer function from #204 (part 3). When a Series is created it makes its Occurrences for the next 8 weeks (at least the next one), but until now nothing moved that horizon forward. ExtendSeries runs every day at 04:00 UTC and extends every Series with the same call POST /api/series already makes.

Change

  • Services/SeriesExtender.cs (new). ExtendAllAsync(TimeProvider clock, CancellationToken ct) returns SeriesExtension(Seen, Created, Failed).
    • Reads "now" once per run.
    • Lists the Series row keys only, so one malformed row cannot break the listing.
    • For each Series it reads the row and its stored Occurrences, then calls SeriesOccurrences.ReconcileAsync(series, [], kept, now, ct), with every stored Occurrence's key as kept and no candidates. This is exactly what Create does.
    • It adds no second generator, no new id scheme and no special case for an ended Series: the Shared OccurrenceGenerator already returns nothing after the end.
    • It never writes the Series row.
  • Functions/SeriesTimerFunctions.cs (new). A thin [Function("ExtendSeries")] with [TimerTrigger("0 0 4 * * *", RunOnStartup = false)]. It only calls the extender.
  • Program.cs registers SeriesOccurrences and SeriesExtender as singletons over the existing table stores.
  • MW-GC.EventManager.API.csproj adds one new dependency: Microsoft.Azure.Functions.Worker.Extensions.Timer 4.3.1. That is the current stable release on nuget.org; it depends on Worker.Core 1.18.0 and Extensions.Abstractions 1.3.0, both satisfied by the Worker 2.52.0 already referenced. Cold build: no NU* or version warnings.
  • The anonymous HTTP triggers and the Static Web App link model are unchanged: the timer has no HTTP surface.

Failure policy. When one Series fails (a read error, a write error, a malformed schedule, a rule the generator refuses, a row key that is not an id):

  • The failure is logged at Error with its id, and the run goes on to the next Series.
  • The run ends with one summary line: Series extension: N Series seen, M Occurrences created, K Series failed. It is logged at Information, or at Error with the failed ids when K > 0.
  • The run does not throw. RouteFailures maps HTTP failures to status codes and has no timer counterpart, and a throw would only add a host-level failure on top of the per-Series errors. The next daily run retries.
  • A Series deleted between the listing and its read is skipped and not counted as a failure.
  • A cancellation (host shutdown) stops the run and is rethrown.

Validation

Base origin/dev moved from 41d3a04 to 02d09f5 (#221, Web only, no file in common) during the work, so the branch was rebased (one commit, no conflict) and everything below was re-run on the rebased head.

errors warnings Api.Tests Web.Tests E2E (no stack)
cold baseline 41d3a04 0 0 840 + 4 skipped 876 5 + 12 skipped
cold baseline 02d09f5 0 0 840 + 4 skipped 940 5 + 12 skipped
cold after (this head) 0 0 868 + 4 skipped 940 5 + 12 skipped
  • 28 new Api tests, all in new files under MW-GC.EventManager.Api.Tests/Series/. No existing test was changed or removed.
  • Red step: 26 of the 28 failed against stubs.
  • 17 throwaway mutations, all killed, each by a named test. The list is in the evidence file on the Kanban card.

Live run (worker, with-slot.sh, Azurite + Functions host + Web + SWA). The timer was invoked through POST /admin/functions/ExtendSeries (202; no key needed on the local host). Rows were read and deleted straight in Azurite.

flow this branch plain origin/dev (control)
F0 host lists ExtendSeries, no run on startup PASS FAIL (function absent, expected)
F1 create a weekly Series (8 Occurrences) PASS PASS
F2 invoke twice: both runs 1 seen, 0 created, 0 failed; every row and the Series row byte for byte unchanged PASS ERROR (admin invoke 404, expected)
F3 delete the last Occurrence row in Azurite, invoke: 1 created, same id and fields, the other 7 rows and the Series row unchanged PASS ERROR (expected)
F4 cancel an Occurrence through DELETE /api/events/{id}, invoke: 0 created, it stays gone, rows and Series row unchanged PASS ERROR (expected)
F5 a Series with a past end: 0 created, no rows PASS ERROR (expected)
E2E suite with the stack 17/17 not run

Notes

A Series makes its Occurrences when it is created, but nothing moved the
8-week horizon forward afterwards. A daily timer function now does it.

- SeriesExtender lists the Series row keys and extends each Series from one
  "now" read once per run. It makes the same call a create makes:
  ReconcileAsync(series, [], kept, now) with every stored Occurrence kept
  and no candidates. It reuses the Shared generator, the derived Occurrence
  id and the atomic add, so it only adds what the horizon reaches. It never
  rewrites, moves or deletes an Occurrence, never makes a cancelled key
  again and never writes the Series row. A second run, an overlapping run
  or one racing a create makes no twin and overwrites nothing.
- If one Series fails (a read or write error, a malformed row, a row key
  that is not an id), the failure is logged at error level with its id
  and the run moves on to the next Series. A Series deleted after the
  listing is skipped. A cancellation stops the run.
- Each run logs one line: Series seen, Occurrences created, Series failed
  (Information level, or Error with the failed ids when any failed). The run
  does not throw on a Series failure. RouteFailures maps HTTP failures to
  status codes and has no timer analogue; the next daily run retries.
- SeriesTimerFunctions.Run ("ExtendSeries") is a thin timer trigger, NCRONTAB
  "0 0 4 * * *" (04:00 UTC), RunOnStartup false. It has no HTTP surface, so
  the anonymous HTTP triggers and the Static Web App link are unchanged.
- New package: Microsoft.Azure.Functions.Worker.Extensions.Timer 4.3.1.

Refs #204
@mwg-bender

mwg-bender Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Worker notes for Nerd (#204 part 3, the daily timer).

New dependency. Microsoft.Azure.Functions.Worker.Extensions.Timer 4.3.1: the current stable release on nuget.org (versions 4.0.1 to 4.3.1, no newer preview). Its nuspec depends only on Microsoft.Azure.Functions.Worker.Core >= 1.18.0 and Microsoft.Azure.Functions.Worker.Extensions.Abstractions >= 1.3.0, both covered by Microsoft.Azure.Functions.Worker 2.52.0, which is already referenced. The cold build reports 0 warnings.

Schedule. NCRONTAB 0 0 4 * * *: once a day at 04:00. The host reads it in UTC because the app sets no WEBSITE_TIME_ZONE. RunOnStartup = false. Function name ExtendSeries. A reflection test pins all three.

DISPOSITION

  • #204 timer DONE: daily timer function ExtendSeries, 04:00 UTC, not on startup.
  • #204 timer.reuse DONE: makes the same ReconcileAsync(series, [], kept, now, ct) call as Create. No second generator, no new id scheme, no ended-Series special case.
  • #204 timer.untouched DONE: never rewrites, moves or deletes an Occurrence (Modified, played or past) and never writes the Series row. Tested byte for byte, eTag included, and shown live in F2 to F4.
  • #204 timer.idempotent DONE: a second run, two overlapping runs, a run racing a repeated create, and a row stored first by another writer all leave no duplicate and nothing overwritten. Tested, and shown live in F2 and F3.
  • #204 timer.cancelled DONE: a cancelled key is never made again. Tested, and shown live in F4.
  • #204 timer.failures DONE, as follows. Each failing Series is logged at Error with its id, and the run tries every other Series. The run then logs one summary line, at Error with the failed ids when any failed. It does not throw at the end. The repo's only failure pattern, RouteFailures, maps HTTP route failures to status codes; it has no timer counterpart and does not rethrow, and a throw at the end would only add a host-level failure on top of the per-Series error lines. A cancellation (host shutdown) is rethrown and is not counted as a failure. A Series deleted after the listing is skipped and not counted as a failure.
  • #204 timer.logline DONE: Series extension: N Series seen, M Occurrences created, K Series failed.
  • #204 timer.shared NOT NEEDED: the generator was reused as it is. No Shared change.

Doc lines wanted (for the docs card; I edited no docs):

  • docs/adr/0002-...md, Consequences: "The API gains a timer trigger package for the daily run" could name it: Microsoft.Azure.Functions.Worker.Extensions.Timer, function ExtendSeries, 04:00 UTC daily, not run on host start.
  • GLOSSARY.md Series/Occurrence: "Planned for 0.6.0" can drop "planned" for the horizon. A daily run at 04:00 UTC keeps every Series' Occurrences 8 weeks ahead, and at least the next one.
  • README or the API docs, local development: invoke the timer by hand with POST http://localhost:7071/admin/functions/ExtendSeries and body {"input":""}. The local host answers 202 and needs no key; in Azure the admin endpoint needs the master key. Each run logs Series extension: ....
  • Operations note: a failing Series shows as an Error log line naming its id. The function itself still reports Succeeded, so alerting has to look at the log line, not the function's outcome.

Observations (not fixed)

  1. ReconcileAsync reports Held keys, but the timer passes no candidates, so Held can only come from Occupied(...) over an empty held list, which returns nothing. The timer therefore never records skipped keys, which is correct. If that method ever changes, a timer that ignores Held would need a second look.
  2. SeriesOccurrences.OfSeriesAsync reads the whole Events table once per Series. The daily run is therefore O(Series x Events) table reads. That is fine at this app's size, but worth an issue if Series grow to dozens. One read per run, grouped by SeriesId, would do.
  3. The admin endpoint invocation has no master key locally. In Azure it needs x-functions-key with the master key. No route change is needed; this is a note for anyone testing in Azure.
  4. with-slot.sh E2E: the repo E2E suite needs PLAYWRIGHT_BROWSERS_PATH=$T/browsers. Without it, 12 browser tests fail with "Executable doesn't exist at /opt/hermes/tools/chromium_headless_shell-1243". My first run hit this and the rerun with the variable passed 17/17. The tooling could export the variable itself.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Other ❓ Flaky 🍂 Duration ⏱️
1814 1810 0 0 4 0 1m 3s

🎉 No failed tests in this run.

Github Test Reporter by CTRF 💚

🔄 This comment has been updated

… flight is not undone

A cancel records the Occurrence's key in the Series' Skipped list first and
deletes the Occurrence row second. The daily run read the Series row first
and the Occurrences second, so a cancel that landed between the two reads
was in neither the run's Skipped nor its kept list. The run's atomic add
then made the cancelled Occurrence again, and nothing ever removed it.

SeriesExtender now reads the Occurrences before the Series. A cancel in
flight is then either already in Skipped or still in kept, so it is never
made again.

Tests:
- An_Occurrence_cancelled_while_the_run_reads_the_Series_is_not_made_again
  cancels the third Occurrence through the Event API between the run's two
  reads; it fails on the old read order.
- A_row_in_another_partition_of_the_Series_table_is_not_a_Series answers
  the listing through the filter it receives, so dropping the partition
  filter from the listing fails it.

Refs #204
@mwg-bender

mwg-bender Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Review fix: new head 75c796ffca45f51c58089b7572ec000acc42deb4

One new commit on 31922ca, pushed as a fast-forward (31922ca..75c796f). There is no amend, rebase or force-push, and dev was not merged in (dev is still 02d09f5). The PR body is unchanged. Author and committer, read back from GitHub: MWG Bender[bot] <331880973+mwg-bender[bot]@users.noreply.github.com>.

Commit: fix(api): read a Series' Occurrences before the Series so a cancel in flight is not undone (footer Refs #204). Files: SeriesExtender.cs (+3 −1), SeriesExtenderRaceTests.cs (+23), SeriesExtenderFailureTests.cs (+22).

Correction to my earlier comment

The first comment's line #204 timer.cancelled DONE cancelled keys never made again (test + live F4) was wrong. It held only for a cancel that finished before the run read the Series. A cancel landing between the run's read of the Series row and its read of the Occurrences was in neither Skipped nor kept, so the run's atomic add brought the Occurrence back for good. Live F4 cancels before it invokes the run, so it could not show the race. The disposition below replaces that line.

DISPOSITION

Item Disposition Proof
A (major): a cancel in flight is undone by the run DONE: Occurrences are now read before the Series. A cancel records the key on the Series and then deletes the row, so a cancel in flight is either in Skipped or still in kept. MW-GC.EventManager.API/Services/SeriesExtender.cs:72-73 (comment), :74-75 (OfSeriesAsync first), :76 (store.GetAsync second). Test MW-GC.EventManager.Api.Tests/Series/SeriesExtenderRaceTests.cs:110 An_Occurrence_cancelled_while_the_run_reads_the_Series_is_not_made_again: the cancel goes through the Event API (204) inside the BeforeEventQuery hook; it asserts Skipped holds the key (:127) and the Occurrence dates do not contain it (:128).
B (hard test gap): the listing's partition filter was unpinned DONE: test only; production unchanged. MW-GC.EventManager.Api.Tests/Series/SeriesExtenderFailureTests.cs:128 A_row_in_another_partition_of_the_Series_table_is_not_a_Series: the SeriesTable.QueryAsync mock answers the Series rows plus TableEntity("Marker","not-a-series") through the filter.Compile() it receives, as one page; it asserts Seen 1 (:142), Failed empty, Created 2. The filter it pins: SeriesExtender.cs:86.
#204 timer.cancelled DONE (corrected): cancelled keys are never made again, including a cancel that lands while the run reads the Series. test A above, plus the existing A_cancelled_Occurrence_is_not_made_again, plus live F4 on the new head

The other lines of the earlier DISPOSITION (timer, reuse, untouched, idempotent, failures, logline, shared) stand as written.

Red run on the old head (31922ca, the test added, production unchanged)

  Passed A_row_in_another_partition_of_the_Series_table_is_not_a_Series [703 ms]
  Failed An_Occurrence_cancelled_while_the_run_reads_the_Series_is_not_made_again [70 ms]
  Error Message:
   Assertion failed. Expected collection to not contain the specified element.

unexpected: "2027-03-18T19:00:00-05:00"
collection: ["2027-03-04T19:00:00-05:00", "2027-03-11T19:00:00-05:00", "2027-03-18T19:00:00-05:00", "2027-03-25T19:00:00-05:00", "2027-04-01T19:00:00-05:00", "2027-04-08T19:00:00-05:00", "2027-04-15T19:00:00-05:00", "2027-04-22T19:00:00-05:00", "2027-04-29T19:00:00-05:00"]
  Stack Trace:
     at ...SeriesExtenderRaceTests.An_Occurrence_cancelled_while_the_run_reads_the_Series_is_not_made_again() in .../SeriesExtenderRaceTests.cs:line 128
Total tests: 2  Passed: 1  Failed: 1

B is meant to be green on the old head, because the filter was already there. It is a pin, and the mutation below makes it go red.

Mutations (each applied to production, then the FULL Api.Tests run, then restored; the file was verified identical afterwards)

# Mutation Result Failing test
MA read order swapped back (store.GetAsync before OfSeriesAsync) KILLED: 1 red (869 passed, 1 failed, 4 skipped, 874 total) An_Occurrence_cancelled_while_the_run_reads_the_Series_is_not_made_again
MB listing filter dropped (e => true) KILLED: 1 red (869 passed, 1 failed, 4 skipped, 874 total) A_row_in_another_partition_of_the_Series_table_is_not_a_Series (Assert.AreEqual(1, run.Seen))

Numbers (cold: clean-build.sh, build --no-incremental, test --no-build, on 75c796f)

  • Build: 0 errors, 0 warnings.
  • Api.Tests: 870 passed + 4 skipped (874) = 868 + 2 new. No test removed, renamed or changed, and no existing assertion touched.
  • Web.Tests: 940 passed.
  • E2E: 17 (5 passed + 12 skipped without the stack); 17/17 passed against the live stack below.

Live run on 75c796f (with-slot.sh, slot 0, same timer_flows.py as the first run)

PASS F0 the host lists the timer function and it did not run on startup
PASS F1 create a weekly Series | created=8 rows=8
PASS F2 two runs: the second creates nothing | run1 summary=(1, 0, 0); run2 summary=(1, 0, 0); Occurrence rows unchanged across both=True (count 8); Series row unchanged=True
PASS F3 a deleted Occurrence comes back with the same id, nothing else changes | 7 left; run summary=(1, 1, 0); back with same id=True, same fields=True; others unchanged=True; Series row unchanged=True
PASS F4 a cancelled Occurrence stays gone after a run | key in Skipped=True; run summary=(1, 0, 0); still gone=True; rows unchanged=True (count 7); Series row unchanged=True
PASS F5 a Series with a past end gets nothing | create created=0; run summary=(2, 0, 0); Occurrence rows=0; Series row unchanged=True
=== SUMMARY F0=PASS, F1=PASS, F2=PASS, F3=PASS, F4=PASS, F5=PASS

The 10 baseline API flows all passed. Repo E2E against the slot: 17 of 17 passed.

Observations

These are the four from the first comment, unchanged: ReconcileAsync's Held is always empty for the timer; OfSeriesAsync reads the whole Events table once per Series (O(Series × Events)); invoking the timer from the admin endpoint needs the master key in Azure; running E2E under with-slot.sh needs PLAYWRIGHT_BROWSERS_PATH.

@mwg-bender
mwg-bender Bot merged commit aafbcb5 into dev Nov 25, 2026
7 checks passed
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.

0 participants