跳到正文

第 20 课 · Automating Reports

Scheduling and Delivery

Schedule automated reports and deliver them safely: triggers, dependencies, publish gates, retries, duplicate protection, email messages, alerts and failure handling.

A script that you run by hand is half automated. The other half is deciding when it runs, what must be true before it sends anything, how readers get the result and who learns about failures. Most painful reporting incidents happen here: a report with half the data, sent twice, to the wrong list.

Time or data triggers

A time trigger runs at a set moment, such as Monday at 07:00. Operating systems, servers and reporting platforms all offer schedulers. For example, the classic Unix cron format has five fields.

TEXT
# minute hour day-of-month month day-of-week   command (illustrative)
  0      7    *            *     1             run_weekly_report.sh

This line means 07:00 every Monday in the scheduler's time zone. Check which zone your scheduler uses, and what it does when clocks change for daylight saving time. Platform schedulers use their own syntax, so check their documentation.

A data trigger runs when the needed data has arrived, for example after a load job succeeds. This avoids reporting on incomplete data. Many teams combine the two: start after the data is ready, and raise an alert if it is not ready by a deadline.

The publish gate

Before delivery, run checks. If a required check fails, do not send the report. Tell the owner, and tell readers that it is delayed if they expect it. A late report is usually better than a wrong one.

Python Stand-in: a publish gate

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This stand-in prints a decision and sends nothing. Its 30% and 1% limits are examples; set yours from your data's normal behavior. Here the data stops one day early, so the report is held even though the other checks pass.

Retries and duplicates

Networks and services fail briefly. Retrying helps with temporary errors, but a retry can send a report twice. Make delivery idempotent: record a unique key, such as report name, period and version, and send only if that key has not been sent.

Python Stand-in: send each report only once

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In real systems the log must survive restarts and cope with two runs at once, usually with a database and a unique constraint. If sending succeeds and recording fails, a duplicate is still possible. Decide how you will detect that.

Delivery channels

Channel Good for Watch out for
Link to a secured page Current access control, corrections Readers must open it
Email summary with link Reach and a short message Forwarding, mail size limits
Attached file Offline reading, records Copies you cannot withdraw
Chat message or alert Urgent exceptions Noise, wrong channel members

Prefer a link to a protected location for sensitive information. A short summary in the message helps readers, but remember that even a summary can be sensitive.

Build a message without sending it

Python's email package can construct a message. This block only builds and prints it. Sending needs a mail server, credentials and your organization's approval.

Python Stand-in: compose a report email

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Before enabling real delivery, send to a test list. Review recipient lists regularly, remove people who left or changed roles, and require a human approval step for new external recipients.

Alerts

An alert interrupts someone, so use it for things that need action. A useful alert names the measure, value, limit, time, owner and first step. Avoid repeating the same alert every few minutes; group repeats and say when the condition clears. If people routinely ignore an alert, change its limit or remove it.

When a run fails

Every scheduled report needs monitoring. Silence can mean success or that the job never ran. Have something check that the expected output exists by the deadline. Write a short runbook:

TEXT
1. Where are the logs?
2. How do I rerun a period safely?
3. Who owns each data source?
4. What do we tell readers if it is late?
5. How do we send a correction (new version, clear note)?

Handle dependencies and late data

A report often depends on several loads that finish at different times. List each dependency with the time it normally arrives and the latest time you can accept it. Then decide in advance what happens when one is late:

  • Wait and publish later, with a notice to readers.
  • Publish partly, only if the missing part is clearly marked and the rest remains correct on its own.
  • Skip this issue and explain why.

Be cautious with partial publishing. A total that silently leaves out one region is wrong, even if every other row is right.

Plan backfills too. A backfill reruns past periods, for example after you fix a bug. The script must use the requested period everywhere, and delivery should be switched off or require approval. Otherwise a repair job could send months of old reports to every reader.

Keep a run history with the period, start and end times, result, check outcomes and version of the code. It answers common questions quickly: Did the job run on Monday? Which version produced the figure? When did it start to take longer? A steadily growing run time is a useful early warning that the job may soon miss its deadline.

Test failure paths on purpose before launch. Point a test run at empty data, stop a dependency and use a bad address on a test list. Confirm that the report is held, the right person is told and nothing reaches real readers.

Run scheduled jobs with the least access they need, for example read-only database accounts. Do not run them under a person's own login, which may stop working or give far more access than required.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. The data for the last day of the period is missing at send time. What is usually best?

    A held report avoids decisions based on incomplete data.
  2. 2. What protects readers from receiving the same report twice after a retry?

    An idempotency key lets the system skip a delivery already made.
  3. 3. How can you discover that a scheduled job never started?

    A job that never runs cannot report its own failure.

Summary

  • Choose time triggers, data triggers or both, and confirm the time zone.
  • Gate publication on freshness, completeness and reconciliation checks.
  • Make retries safe with delivery keys and send corrections as new versions.
  • Prefer protected links for sensitive content and review recipients.
  • Monitor for missing runs, keep a runbook and use least-privilege accounts.

Next: Checking Your Numbers

你的学习进度保存在此浏览器中,无需账号。