silencewatch 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ # dependencies
2
+ node_modules/
3
+ .pnp/
4
+ .pnp.js
5
+
6
+ # build output
7
+ dist/
8
+ build/
9
+ out/
10
+ *.tsbuildinfo
11
+ packages/web/.angular/
12
+ packages/server/public/
13
+
14
+ # java
15
+ target/
16
+ clients/spring-boot-starter/target/
17
+
18
+ # environment / secrets — never commit these
19
+ .env
20
+ .env.*
21
+ !.env.example
22
+ *.pem
23
+ *.key
24
+ !**/*.key.example
25
+
26
+ # logs & runtime
27
+ *.log
28
+ npm-debug.log*
29
+ coverage/
30
+ .nyc_output/
31
+ pgdata/
32
+
33
+ # editors / os
34
+ .idea/
35
+ .vscode/*
36
+ !.vscode/extensions.json
37
+ .DS_Store
38
+ Thumbs.db
39
+
40
+ # python
41
+ __pycache__/
42
+ *.py[cod]
43
+ *.egg-info/
44
+ .pytest_cache/
45
+ .mypy_cache/
46
+ .ruff_cache/
47
+ .venv/
48
+ clients/python/dist/
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ The Python library is versioned apart from the application, and tagged
4
+ `python-N.N.N`. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
+
6
+ ## [Unreleased]
7
+
8
+ ## [0.1.0]
9
+
10
+ First release.
11
+
12
+ - **Celery beat** and **APScheduler 3** integrations: the jobs are declared to SilenceWatch
13
+ with their real schedule, and a heartbeat is sent around each run.
14
+ - No runtime dependency; never fails or blocks a job; bounded queue; survives a fork.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,212 @@
1
+ Metadata-Version: 2.5
2
+ Name: silencewatch
3
+ Version: 0.1.0
4
+ Summary: Declare and monitor the scheduled jobs of a Python application with SilenceWatch: Celery beat, APScheduler.
5
+ Project-URL: Homepage, https://silencewatch.com/python/
6
+ Project-URL: Documentation, https://silencewatch.com/docs/python/
7
+ Project-URL: Source, https://github.com/liliang-dev/SilenceWatch/tree/main/clients/python
8
+ Project-URL: Changelog, https://github.com/liliang-dev/SilenceWatch/blob/main/clients/python/CHANGELOG.md
9
+ Author: The SilenceWatch authors
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: apscheduler,celery,cron,dead-mans-switch,heartbeat,monitoring,silencewatch
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: System :: Monitoring
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.9
26
+ Provides-Extra: apscheduler
27
+ Requires-Dist: apscheduler<4,>=3.9; extra == 'apscheduler'
28
+ Provides-Extra: celery
29
+ Requires-Dist: celery>=5.2; extra == 'celery'
30
+ Description-Content-Type: text/markdown
31
+
32
+ # SilenceWatch for Python
33
+
34
+ Add the library and an API key. The scheduled jobs of your application show up in
35
+ SilenceWatch with their real schedule, and send a heartbeat around each run.
36
+
37
+ Licensed under **Apache-2.0** — deliberately, so it can be embedded in any
38
+ application without licence consequences.
39
+
40
+ | Scheduler | Install | What it reads |
41
+ | --- | --- | --- |
42
+ | **Celery beat** | `pip install "silencewatch[celery]"` | the `beat_schedule` (crontab, `timedelta`, seconds) |
43
+ | **APScheduler 3** | `pip install "silencewatch[apscheduler]"` | the jobs of the scheduler (cron and interval triggers) |
44
+
45
+ More are planned; see [Adding an integration](#adding-an-integration).
46
+
47
+ ## Set up
48
+
49
+ Create an API key in your project's Settings page and give it to the process:
50
+
51
+ ```bash
52
+ export SILENCEWATCH_API_KEY=sw_...
53
+ # export SILENCEWATCH_URL=https://watch.example.com # only when you host SilenceWatch yourself
54
+ ```
55
+
56
+ ### Celery
57
+
58
+ ```python
59
+ from celery import Celery
60
+ from celery.schedules import crontab
61
+ from silencewatch.integrations.celery import instrument
62
+
63
+ app = Celery("shop")
64
+ app.conf.beat_schedule = {
65
+ "nightly-backup": {"task": "shop.tasks.backup", "schedule": crontab(hour=2, minute=0)},
66
+ "sync-prices": {"task": "shop.tasks.sync_prices", "schedule": 900.0},
67
+ }
68
+ instrument(app)
69
+ ```
70
+
71
+ Workers declare the schedule when they start, and send a heartbeat from the worker
72
+ that runs each scheduled task. Nothing else to write.
73
+
74
+ ### APScheduler
75
+
76
+ ```python
77
+ from apscheduler.schedulers.background import BackgroundScheduler
78
+ from silencewatch.integrations.apscheduler import instrument
79
+
80
+ scheduler = BackgroundScheduler()
81
+ scheduler.add_job(backup, "cron", hour=2, id="nightly-backup")
82
+ scheduler.add_job(sync_prices, "interval", minutes=15, id="sync-prices")
83
+ instrument(scheduler)
84
+ scheduler.start()
85
+ ```
86
+
87
+ **Give each job an `id`.** APScheduler makes up a random one otherwise, which changes
88
+ on every start and cannot tie a job to its history. Without an id the job is
89
+ identified by the function it runs, which is stable.
90
+
91
+ ## What it does
92
+
93
+ At startup, in **one HTTP call**, every job found is declared to
94
+ `POST /api/v1/checks/sync` and the returned ping keys are kept in memory. Around
95
+ each run:
96
+
97
+ ```
98
+ POST /p/<key>/start when the run begins
99
+ POST /p/<key>/0?duration_ms=1234 when it returns
100
+ POST /p/<key>/fail?duration_ms=1234 when it raises
101
+ ```
102
+
103
+ Each job gets a **stable identity**, which is what ties it to its history on the
104
+ server across restarts, redeployments and renames:
105
+
106
+ | Integration | Key |
107
+ | --- | --- |
108
+ | Celery | `celery:shop.tasks.backup` (the task name) |
109
+ | APScheduler | `apscheduler:nightly-backup` (the job id, else `apscheduler:app.jobs:backup`, the function) |
110
+
111
+ Jobs that disappear from the code are **flagged orphaned on the server, never
112
+ deleted**.
113
+
114
+ ## Configuration
115
+
116
+ Every setting is a keyword argument (`instrument(app, api_key="…", environment="staging")`)
117
+ and an environment variable. The keyword wins. Durations in the environment are a
118
+ number of seconds.
119
+
120
+ | Keyword | Environment variable | Default | |
121
+ | --- | --- | --- | --- |
122
+ | `api_key` | `SILENCEWATCH_API_KEY` | — | Without one nothing is sent |
123
+ | `base_url` | `SILENCEWATCH_URL` | `https://app.silencewatch.com` | Your own instance when self-hosting |
124
+ | `environment` | `SILENCEWATCH_ENVIRONMENT` | `production` | Part of a check's identity |
125
+ | `enabled` | `SILENCEWATCH_ENABLED` | `true` | `false` removes everything, see below |
126
+ | `default_grace` | `SILENCEWATCH_DEFAULT_GRACE` | `5 min` | For jobs that declare none |
127
+ | `auto_register` | `SILENCEWATCH_AUTO_REGISTER` | `true` | `false` to manage checks by hand |
128
+ | `report_start` | `SILENCEWATCH_REPORT_START` | `true` | `false` to send only the outcome |
129
+ | `timeout` | `SILENCEWATCH_TIMEOUT` | `2` (s) | Per call, deliberately short |
130
+ | `queue_capacity` | `SILENCEWATCH_QUEUE_CAPACITY` | `1000` | Bounded, oldest dropped when full |
131
+ | `timezone` | `SILENCEWATCH_TIMEZONE` | `UTC` | For cron jobs whose own schedule carries none |
132
+ | `prune` | `SILENCEWATCH_PRUNE` | `true` | See below |
133
+
134
+ `prune` flags as orphaned every check of the project and environment that the
135
+ declaration leaves out. Use **one environment per application**; if several services
136
+ share one project and environment, set `prune=False`, or each would orphan the
137
+ others' checks.
138
+
139
+ ## Rules it will not break
140
+
141
+ These are the reason the library is safe to put in a production application:
142
+
143
+ - **It never fails your job.** Every network or HTTP error is swallowed and logged at
144
+ `WARNING`. Your task runs exactly once, and its result and its exception are untouched.
145
+ - **It never blocks your job.** Sending a heartbeat is a queue append handled by a
146
+ daemon thread. An unreachable SilenceWatch costs your job nothing.
147
+ - **It is bounded.** The queue holds at most `queue_capacity` heartbeats; past that the
148
+ oldest are dropped, because the freshest state is the one worth reporting. Warnings
149
+ are limited to one a minute so an outage cannot flood your logs.
150
+ - **It degrades silently.** No API key, no network, no server: the application starts
151
+ and runs exactly as it would without the library. A process that started while
152
+ SilenceWatch was unreachable declares itself at its first heartbeat, at most once a
153
+ minute.
154
+ - **It survives a fork.** Celery's prefork pool and gunicorn fork after the library
155
+ started; each child starts its own queue and thread.
156
+ - **`enabled=False` removes the mechanism entirely**: no discovery, no hooks, no threads.
157
+ - **No dependency.** The standard library only. `celery` and `apscheduler` are extras you
158
+ already have.
159
+
160
+ ## Notes and limits
161
+
162
+ - **Celery** — a task scheduled by several beat entries is declared from the one that
163
+ fires most often. A **manual** `.delay()` of a scheduled task also sends a heartbeat,
164
+ since a worker cannot tell it from a scheduled one; do not rely on a task being called
165
+ by hand for the check to stay green. Solar schedules and schedules kept in a database
166
+ (`django-celery-beat`) are not read. A crontab that restricts both the day of the month
167
+ and the day of the week fires when both match in Celery and when either does in cron;
168
+ the log says so.
169
+ - **APScheduler** — version 3 only (4 has another API). A job's duration includes any time
170
+ it waited for an executor. One-off `date` triggers, combined `and`/`or` triggers and the
171
+ `week`, `year` and "third friday" cron fields cannot be expressed and are skipped, with a
172
+ warning. Jobs added or removed after the scheduler started are declared again two
173
+ seconds later.
174
+ - **Sub-30-second jobs** are declared with a 30-second period, the shortest the server
175
+ accepts, and a warning names them.
176
+ - **Cron expressions the server cannot evaluate** (Quartz's `W`) are skipped with a
177
+ warning rather than failing the whole declaration.
178
+
179
+ ## Adding an integration
180
+
181
+ The package is organised so the next scheduler (Django, RQ, Dramatiq, a cron wrapper…)
182
+ is one module:
183
+
184
+ ```
185
+ src/silencewatch/
186
+ config.py settings, from keywords and SILENCEWATCH_* variables
187
+ model.py DiscoveredJob: what an integration reports
188
+ client.py the HTTP calls (declaration, heartbeat), standard library only
189
+ heartbeat.py the bounded queue and the thread, fork-safe
190
+ monitor.py one per process: declares everything in one request, sends the runs
191
+ cron.py the cron dialect the server reads, and helpers to write one
192
+ integrations/
193
+ base.py the contract an integration follows
194
+ celery.py
195
+ apscheduler.py ← a new scheduler goes here, next to these
196
+ ```
197
+
198
+ An integration is a class with a `name`, a `discover()` that returns the jobs as
199
+ `DiscoveredJob`s, and an `install(monitor)` that hooks the scheduler: it calls
200
+ `monitor.declare(self, jobs)` when the scheduler is ready and `monitor.started`,
201
+ `succeeded` or `failed` around each run. Add an `instrument(...)` function that creates
202
+ it and calls `monitor.attach(...)`, tests next to the others, and the README row above.
203
+ Nothing else changes.
204
+
205
+ ## Development
206
+
207
+ ```bash
208
+ pip install -e ".[celery,apscheduler]" -r requirements.txt # Python 3.10+ for the lint tools
209
+ ruff check . && mypy && pytest
210
+ ```
211
+
212
+ The tests start a small stand-in for SilenceWatch, so the real HTTP code runs.
@@ -0,0 +1,181 @@
1
+ # SilenceWatch for Python
2
+
3
+ Add the library and an API key. The scheduled jobs of your application show up in
4
+ SilenceWatch with their real schedule, and send a heartbeat around each run.
5
+
6
+ Licensed under **Apache-2.0** — deliberately, so it can be embedded in any
7
+ application without licence consequences.
8
+
9
+ | Scheduler | Install | What it reads |
10
+ | --- | --- | --- |
11
+ | **Celery beat** | `pip install "silencewatch[celery]"` | the `beat_schedule` (crontab, `timedelta`, seconds) |
12
+ | **APScheduler 3** | `pip install "silencewatch[apscheduler]"` | the jobs of the scheduler (cron and interval triggers) |
13
+
14
+ More are planned; see [Adding an integration](#adding-an-integration).
15
+
16
+ ## Set up
17
+
18
+ Create an API key in your project's Settings page and give it to the process:
19
+
20
+ ```bash
21
+ export SILENCEWATCH_API_KEY=sw_...
22
+ # export SILENCEWATCH_URL=https://watch.example.com # only when you host SilenceWatch yourself
23
+ ```
24
+
25
+ ### Celery
26
+
27
+ ```python
28
+ from celery import Celery
29
+ from celery.schedules import crontab
30
+ from silencewatch.integrations.celery import instrument
31
+
32
+ app = Celery("shop")
33
+ app.conf.beat_schedule = {
34
+ "nightly-backup": {"task": "shop.tasks.backup", "schedule": crontab(hour=2, minute=0)},
35
+ "sync-prices": {"task": "shop.tasks.sync_prices", "schedule": 900.0},
36
+ }
37
+ instrument(app)
38
+ ```
39
+
40
+ Workers declare the schedule when they start, and send a heartbeat from the worker
41
+ that runs each scheduled task. Nothing else to write.
42
+
43
+ ### APScheduler
44
+
45
+ ```python
46
+ from apscheduler.schedulers.background import BackgroundScheduler
47
+ from silencewatch.integrations.apscheduler import instrument
48
+
49
+ scheduler = BackgroundScheduler()
50
+ scheduler.add_job(backup, "cron", hour=2, id="nightly-backup")
51
+ scheduler.add_job(sync_prices, "interval", minutes=15, id="sync-prices")
52
+ instrument(scheduler)
53
+ scheduler.start()
54
+ ```
55
+
56
+ **Give each job an `id`.** APScheduler makes up a random one otherwise, which changes
57
+ on every start and cannot tie a job to its history. Without an id the job is
58
+ identified by the function it runs, which is stable.
59
+
60
+ ## What it does
61
+
62
+ At startup, in **one HTTP call**, every job found is declared to
63
+ `POST /api/v1/checks/sync` and the returned ping keys are kept in memory. Around
64
+ each run:
65
+
66
+ ```
67
+ POST /p/<key>/start when the run begins
68
+ POST /p/<key>/0?duration_ms=1234 when it returns
69
+ POST /p/<key>/fail?duration_ms=1234 when it raises
70
+ ```
71
+
72
+ Each job gets a **stable identity**, which is what ties it to its history on the
73
+ server across restarts, redeployments and renames:
74
+
75
+ | Integration | Key |
76
+ | --- | --- |
77
+ | Celery | `celery:shop.tasks.backup` (the task name) |
78
+ | APScheduler | `apscheduler:nightly-backup` (the job id, else `apscheduler:app.jobs:backup`, the function) |
79
+
80
+ Jobs that disappear from the code are **flagged orphaned on the server, never
81
+ deleted**.
82
+
83
+ ## Configuration
84
+
85
+ Every setting is a keyword argument (`instrument(app, api_key="…", environment="staging")`)
86
+ and an environment variable. The keyword wins. Durations in the environment are a
87
+ number of seconds.
88
+
89
+ | Keyword | Environment variable | Default | |
90
+ | --- | --- | --- | --- |
91
+ | `api_key` | `SILENCEWATCH_API_KEY` | — | Without one nothing is sent |
92
+ | `base_url` | `SILENCEWATCH_URL` | `https://app.silencewatch.com` | Your own instance when self-hosting |
93
+ | `environment` | `SILENCEWATCH_ENVIRONMENT` | `production` | Part of a check's identity |
94
+ | `enabled` | `SILENCEWATCH_ENABLED` | `true` | `false` removes everything, see below |
95
+ | `default_grace` | `SILENCEWATCH_DEFAULT_GRACE` | `5 min` | For jobs that declare none |
96
+ | `auto_register` | `SILENCEWATCH_AUTO_REGISTER` | `true` | `false` to manage checks by hand |
97
+ | `report_start` | `SILENCEWATCH_REPORT_START` | `true` | `false` to send only the outcome |
98
+ | `timeout` | `SILENCEWATCH_TIMEOUT` | `2` (s) | Per call, deliberately short |
99
+ | `queue_capacity` | `SILENCEWATCH_QUEUE_CAPACITY` | `1000` | Bounded, oldest dropped when full |
100
+ | `timezone` | `SILENCEWATCH_TIMEZONE` | `UTC` | For cron jobs whose own schedule carries none |
101
+ | `prune` | `SILENCEWATCH_PRUNE` | `true` | See below |
102
+
103
+ `prune` flags as orphaned every check of the project and environment that the
104
+ declaration leaves out. Use **one environment per application**; if several services
105
+ share one project and environment, set `prune=False`, or each would orphan the
106
+ others' checks.
107
+
108
+ ## Rules it will not break
109
+
110
+ These are the reason the library is safe to put in a production application:
111
+
112
+ - **It never fails your job.** Every network or HTTP error is swallowed and logged at
113
+ `WARNING`. Your task runs exactly once, and its result and its exception are untouched.
114
+ - **It never blocks your job.** Sending a heartbeat is a queue append handled by a
115
+ daemon thread. An unreachable SilenceWatch costs your job nothing.
116
+ - **It is bounded.** The queue holds at most `queue_capacity` heartbeats; past that the
117
+ oldest are dropped, because the freshest state is the one worth reporting. Warnings
118
+ are limited to one a minute so an outage cannot flood your logs.
119
+ - **It degrades silently.** No API key, no network, no server: the application starts
120
+ and runs exactly as it would without the library. A process that started while
121
+ SilenceWatch was unreachable declares itself at its first heartbeat, at most once a
122
+ minute.
123
+ - **It survives a fork.** Celery's prefork pool and gunicorn fork after the library
124
+ started; each child starts its own queue and thread.
125
+ - **`enabled=False` removes the mechanism entirely**: no discovery, no hooks, no threads.
126
+ - **No dependency.** The standard library only. `celery` and `apscheduler` are extras you
127
+ already have.
128
+
129
+ ## Notes and limits
130
+
131
+ - **Celery** — a task scheduled by several beat entries is declared from the one that
132
+ fires most often. A **manual** `.delay()` of a scheduled task also sends a heartbeat,
133
+ since a worker cannot tell it from a scheduled one; do not rely on a task being called
134
+ by hand for the check to stay green. Solar schedules and schedules kept in a database
135
+ (`django-celery-beat`) are not read. A crontab that restricts both the day of the month
136
+ and the day of the week fires when both match in Celery and when either does in cron;
137
+ the log says so.
138
+ - **APScheduler** — version 3 only (4 has another API). A job's duration includes any time
139
+ it waited for an executor. One-off `date` triggers, combined `and`/`or` triggers and the
140
+ `week`, `year` and "third friday" cron fields cannot be expressed and are skipped, with a
141
+ warning. Jobs added or removed after the scheduler started are declared again two
142
+ seconds later.
143
+ - **Sub-30-second jobs** are declared with a 30-second period, the shortest the server
144
+ accepts, and a warning names them.
145
+ - **Cron expressions the server cannot evaluate** (Quartz's `W`) are skipped with a
146
+ warning rather than failing the whole declaration.
147
+
148
+ ## Adding an integration
149
+
150
+ The package is organised so the next scheduler (Django, RQ, Dramatiq, a cron wrapper…)
151
+ is one module:
152
+
153
+ ```
154
+ src/silencewatch/
155
+ config.py settings, from keywords and SILENCEWATCH_* variables
156
+ model.py DiscoveredJob: what an integration reports
157
+ client.py the HTTP calls (declaration, heartbeat), standard library only
158
+ heartbeat.py the bounded queue and the thread, fork-safe
159
+ monitor.py one per process: declares everything in one request, sends the runs
160
+ cron.py the cron dialect the server reads, and helpers to write one
161
+ integrations/
162
+ base.py the contract an integration follows
163
+ celery.py
164
+ apscheduler.py ← a new scheduler goes here, next to these
165
+ ```
166
+
167
+ An integration is a class with a `name`, a `discover()` that returns the jobs as
168
+ `DiscoveredJob`s, and an `install(monitor)` that hooks the scheduler: it calls
169
+ `monitor.declare(self, jobs)` when the scheduler is ready and `monitor.started`,
170
+ `succeeded` or `failed` around each run. Add an `instrument(...)` function that creates
171
+ it and calls `monitor.attach(...)`, tests next to the others, and the README row above.
172
+ Nothing else changes.
173
+
174
+ ## Development
175
+
176
+ ```bash
177
+ pip install -e ".[celery,apscheduler]" -r requirements.txt # Python 3.10+ for the lint tools
178
+ ruff check . && mypy && pytest
179
+ ```
180
+
181
+ The tests start a small stand-in for SilenceWatch, so the real HTTP code runs.