resource-engine 0.1.0__py3-none-any.whl
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.
- resource_engine/__init__.py +96 -0
- resource_engine/application/__init__.py +1 -0
- resource_engine/application/actions.py +48 -0
- resource_engine/application/context.py +111 -0
- resource_engine/application/events.py +54 -0
- resource_engine/application/hooks.py +272 -0
- resource_engine/application/idempotency.py +277 -0
- resource_engine/application/manager.py +1987 -0
- resource_engine/application/query.py +47 -0
- resource_engine/application/registry.py +137 -0
- resource_engine/application/relations.py +470 -0
- resource_engine/application/resource.py +984 -0
- resource_engine/application/searching.py +162 -0
- resource_engine/application/settings.py +169 -0
- resource_engine/application/slots.py +67 -0
- resource_engine/application/state_machines.py +177 -0
- resource_engine/application/timers.py +347 -0
- resource_engine/application/transaction.py +100 -0
- resource_engine/application/views.py +239 -0
- resource_engine/audit/__init__.py +40 -0
- resource_engine/audit/export.py +280 -0
- resource_engine/audit/history.py +165 -0
- resource_engine/audit/janitor.py +257 -0
- resource_engine/audit/model.py +98 -0
- resource_engine/audit/recorder.py +145 -0
- resource_engine/audit/resource.py +68 -0
- resource_engine/audit/seal.py +327 -0
- resource_engine/audit/sink.py +69 -0
- resource_engine/authz/__init__.py +72 -0
- resource_engine/authz/apikeys.py +300 -0
- resource_engine/authz/base.py +143 -0
- resource_engine/authz/catalog.py +291 -0
- resource_engine/authz/conditions.py +416 -0
- resource_engine/authz/config.py +77 -0
- resource_engine/authz/evaluator.py +382 -0
- resource_engine/authz/external.py +249 -0
- resource_engine/authz/fields.py +44 -0
- resource_engine/authz/model.py +145 -0
- resource_engine/authz/resources.py +289 -0
- resource_engine/authz/roles.py +101 -0
- resource_engine/cache.py +144 -0
- resource_engine/calls.py +100 -0
- resource_engine/capabilities.py +145 -0
- resource_engine/composite.py +295 -0
- resource_engine/documents/__init__.py +69 -0
- resource_engine/documents/content.py +57 -0
- resource_engine/documents/events.py +49 -0
- resource_engine/documents/janitor.py +93 -0
- resource_engine/documents/machine.py +38 -0
- resource_engine/documents/manager.py +704 -0
- resource_engine/documents/model.py +91 -0
- resource_engine/documents/pipeline.py +98 -0
- resource_engine/documents/processors.py +320 -0
- resource_engine/documents/resource.py +39 -0
- resource_engine/domain/__init__.py +10 -0
- resource_engine/domain/events.py +50 -0
- resource_engine/domain/exceptions.py +23 -0
- resource_engine/email.py +417 -0
- resource_engine/errors.py +562 -0
- resource_engine/explorer.py +133 -0
- resource_engine/exports.py +687 -0
- resource_engine/generation.py +233 -0
- resource_engine/i18n.py +251 -0
- resource_engine/identity.py +46 -0
- resource_engine/integration.py +80 -0
- resource_engine/jobs.py +390 -0
- resource_engine/locale/ca/LC_MESSAGES/resource_engine.mo +0 -0
- resource_engine/locale/ca/LC_MESSAGES/resource_engine.po +206 -0
- resource_engine/locale/es/LC_MESSAGES/resource_engine.mo +0 -0
- resource_engine/locale/es/LC_MESSAGES/resource_engine.po +206 -0
- resource_engine/logs.py +201 -0
- resource_engine/messaging/__init__.py +73 -0
- resource_engine/messaging/asyncapi.py +529 -0
- resource_engine/messaging/cloudevents.py +98 -0
- resource_engine/messaging/consumer.py +165 -0
- resource_engine/messaging/delivery.py +115 -0
- resource_engine/messaging/envelope.py +193 -0
- resource_engine/messaging/hub.py +148 -0
- resource_engine/messaging/inbound.py +76 -0
- resource_engine/messaging/inspection.py +113 -0
- resource_engine/messaging/janitor.py +136 -0
- resource_engine/messaging/live.py +179 -0
- resource_engine/messaging/processing.py +357 -0
- resource_engine/messaging/publishing.py +318 -0
- resource_engine/messaging/relay.py +84 -0
- resource_engine/messaging/worker.py +166 -0
- resource_engine/nosql.py +394 -0
- resource_engine/ports/__init__.py +13 -0
- resource_engine/ports/activity.py +171 -0
- resource_engine/ports/audit.py +42 -0
- resource_engine/ports/authz.py +132 -0
- resource_engine/ports/cache.py +41 -0
- resource_engine/ports/email.py +118 -0
- resource_engine/ports/event_publisher.py +34 -0
- resource_engine/ports/idempotency.py +69 -0
- resource_engine/ports/identity.py +112 -0
- resource_engine/ports/locks.py +28 -0
- resource_engine/ports/messaging.py +197 -0
- resource_engine/ports/observability.py +60 -0
- resource_engine/ports/ratelimit.py +59 -0
- resource_engine/ports/repository.py +56 -0
- resource_engine/ports/storage.py +93 -0
- resource_engine/ports/templates.py +54 -0
- resource_engine/ports/tracing.py +61 -0
- resource_engine/ports/unit_of_work.py +46 -0
- resource_engine/processes.py +408 -0
- resource_engine/py.typed +0 -0
- resource_engine/querying.py +319 -0
- resource_engine/ratelimit.py +217 -0
- resource_engine/reporting.py +230 -0
- resource_engine/schema_examples.py +51 -0
- resource_engine/templates.py +143 -0
- resource_engine/tenancy.py +68 -0
- resource_engine/testing/__init__.py +34 -0
- resource_engine/testing/activity.py +123 -0
- resource_engine/testing/contracts.py +1416 -0
- resource_engine/testing/email.py +36 -0
- resource_engine/testing/engine.py +40 -0
- resource_engine/testing/memory.py +603 -0
- resource_engine/testing/storage.py +110 -0
- resource_engine/testing/tracing.py +100 -0
- resource_engine/tracing.py +156 -0
- resource_engine/translatable.py +80 -0
- resource_engine/versions.py +189 -0
- resource_engine-0.1.0.dist-info/METADATA +36 -0
- resource_engine-0.1.0.dist-info/RECORD +128 -0
- resource_engine-0.1.0.dist-info/WHEEL +4 -0
- resource_engine-0.1.0.dist-info/licenses/LICENSE +202 -0
resource_engine/jobs.py
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""Background work in one process: periodic jobs and long-running services.
|
|
2
|
+
|
|
3
|
+
```python
|
|
4
|
+
runner = JobRunner(locks=SqlAlchemyLocks(sessions, locks_table(metadata)))
|
|
5
|
+
runner.add("outbox", OutboxRelay(uow, publisher), every=1)
|
|
6
|
+
runner.add("audit", AuditJanitor(uow, retention=..., sealer=sealer), every=3600, single=True)
|
|
7
|
+
runner.add_service("billing", MessageConsumer(source, processor))
|
|
8
|
+
await runner.run_forever()
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
A job is anything with ``run_once()`` or an async callable. ``single=True``
|
|
12
|
+
jobs take a lease from the ``LockProvider`` before each run, so they run on one
|
|
13
|
+
worker at a time however many workers are started. Services are objects with
|
|
14
|
+
``run_forever(stop=...)`` (``MessageConsumer``) and run until the runner stops.
|
|
15
|
+
|
|
16
|
+
With ``activity=`` an ``ActivityLog`` the runner reports to the explorer
|
|
17
|
+
(docs/explorer.md#jobs): it registers its jobs and their schedules, records
|
|
18
|
+
every run, and runs the jobs someone asked to "run now", through
|
|
19
|
+
``run_job`` so leases hold. See docs/jobs.md.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import asyncio
|
|
25
|
+
import contextlib
|
|
26
|
+
import inspect
|
|
27
|
+
import logging
|
|
28
|
+
import os
|
|
29
|
+
import socket
|
|
30
|
+
import time
|
|
31
|
+
import uuid
|
|
32
|
+
from collections.abc import Awaitable, Callable, Sequence
|
|
33
|
+
from dataclasses import dataclass, field
|
|
34
|
+
from datetime import timedelta
|
|
35
|
+
from typing import Any, Literal, Protocol, override
|
|
36
|
+
|
|
37
|
+
from resource_engine.errors import ConfigurationError
|
|
38
|
+
from resource_engine.logs import bind_log_context
|
|
39
|
+
from resource_engine.ports.locks import LockProvider
|
|
40
|
+
from resource_engine.tracing import get_tracer
|
|
41
|
+
|
|
42
|
+
logger = logging.getLogger("resource_engine.jobs")
|
|
43
|
+
|
|
44
|
+
type Outcome = Literal["ok", "failed", "skipped"]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class JobObserver(Protocol):
|
|
48
|
+
"""Called after every job run: ``(job, outcome, seconds, error)``.
|
|
49
|
+
``skipped`` means a ``single`` job's lease was held by another worker."""
|
|
50
|
+
|
|
51
|
+
def __call__(
|
|
52
|
+
self, job: str, outcome: Outcome, seconds: float, error: BaseException | None
|
|
53
|
+
) -> Awaitable[None] | None:
|
|
54
|
+
"""Observe one job run."""
|
|
55
|
+
...
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class Job:
|
|
60
|
+
"""A job the worker runs every ``every`` seconds; ``single`` ones hold a lease so only one
|
|
61
|
+
worker runs them."""
|
|
62
|
+
|
|
63
|
+
name: str
|
|
64
|
+
run: Callable[[], Awaitable[Any]]
|
|
65
|
+
every: float
|
|
66
|
+
single: bool = False
|
|
67
|
+
ttl: timedelta = field(default_factory=lambda: timedelta(minutes=10))
|
|
68
|
+
run_at_start: bool = True
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class Service:
|
|
73
|
+
"""A long-running service the worker starts and stops with the jobs."""
|
|
74
|
+
|
|
75
|
+
name: str
|
|
76
|
+
target: Any
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass(frozen=True)
|
|
80
|
+
class JobRun:
|
|
81
|
+
"""The result of one run of a job."""
|
|
82
|
+
|
|
83
|
+
job: str
|
|
84
|
+
outcome: Outcome
|
|
85
|
+
seconds: float
|
|
86
|
+
result: Any = None
|
|
87
|
+
error: BaseException | None = None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def default_owner() -> str:
|
|
91
|
+
"""An owner name unique to this process: host, process id and a random part."""
|
|
92
|
+
return f"{socket.gethostname()}:{os.getpid()}:{uuid.uuid4().hex[:8]}"
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _seconds(value: float | timedelta) -> float:
|
|
96
|
+
return value.total_seconds() if isinstance(value, timedelta) else float(value)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class JobRunner:
|
|
100
|
+
"""Runs periodic jobs and services until stopped.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
locks: Needed for ``single`` jobs.
|
|
104
|
+
owner: This worker's name in leases (host, pid and a random suffix by
|
|
105
|
+
default).
|
|
106
|
+
observers: ``JobObserver`` callbacks, for metrics. Failures are logged
|
|
107
|
+
and ignored.
|
|
108
|
+
activity: An ``ActivityLog``: the jobs, their runs and "run now"
|
|
109
|
+
requests, shared with the explorer.
|
|
110
|
+
requests_every: Seconds between checks for "run now" requests.
|
|
111
|
+
heartbeat: Seconds between re-registrations of the jobs (the
|
|
112
|
+
explorer lists the workers seen recently).
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
def __init__(
|
|
116
|
+
self,
|
|
117
|
+
*,
|
|
118
|
+
locks: LockProvider | None = None,
|
|
119
|
+
owner: str | None = None,
|
|
120
|
+
observers: Sequence[JobObserver] = (),
|
|
121
|
+
activity: Any = None,
|
|
122
|
+
requests_every: float = 2.0,
|
|
123
|
+
heartbeat: float = 60.0,
|
|
124
|
+
) -> None:
|
|
125
|
+
self.locks = locks
|
|
126
|
+
self.owner = owner or default_owner()
|
|
127
|
+
self.observers = list(observers)
|
|
128
|
+
self.activity = activity
|
|
129
|
+
self.requests_every = requests_every
|
|
130
|
+
self.heartbeat = heartbeat
|
|
131
|
+
if activity is not None:
|
|
132
|
+
self.observers.append(ActivityJobObserver(activity, self.owner))
|
|
133
|
+
self.jobs: dict[str, Job] = {}
|
|
134
|
+
self.services: dict[str, Service] = {}
|
|
135
|
+
|
|
136
|
+
# ----------------------------------------------------------- registration
|
|
137
|
+
|
|
138
|
+
def add(
|
|
139
|
+
self,
|
|
140
|
+
name: str,
|
|
141
|
+
target: Any,
|
|
142
|
+
*,
|
|
143
|
+
every: float | timedelta,
|
|
144
|
+
single: bool = False,
|
|
145
|
+
ttl: timedelta | None = None,
|
|
146
|
+
run_at_start: bool = True,
|
|
147
|
+
) -> Job:
|
|
148
|
+
"""Add a periodic job: ``target.run_once()`` or ``await target()``.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
every: Pause between the end of a run and the start of the next.
|
|
152
|
+
single: Run on one worker at a time (needs ``locks``).
|
|
153
|
+
ttl: How long the lease lasts (10 minutes by default). The worker
|
|
154
|
+
renews it every third of the ttl while the job runs; it's how
|
|
155
|
+
long the others wait when a worker dies holding it.
|
|
156
|
+
run_at_start: Run as soon as the runner starts, then every
|
|
157
|
+
``every``; otherwise wait first.
|
|
158
|
+
"""
|
|
159
|
+
self._check_name(name)
|
|
160
|
+
interval = _seconds(every)
|
|
161
|
+
if interval <= 0:
|
|
162
|
+
raise ConfigurationError(f"Job {name!r}: every must be positive")
|
|
163
|
+
if single and self.locks is None:
|
|
164
|
+
raise ConfigurationError(
|
|
165
|
+
f"Job {name!r} is single but the runner has no locks",
|
|
166
|
+
hint="Pass JobRunner(locks=SqlAlchemyLocks(...)) or InMemoryLocks() "
|
|
167
|
+
"for a single worker.",
|
|
168
|
+
doc="docs/jobs.md#one-worker-at-a-time",
|
|
169
|
+
)
|
|
170
|
+
run = getattr(target, "run_once", None) or target
|
|
171
|
+
if not callable(run):
|
|
172
|
+
raise ConfigurationError(f"Job {name!r}: needs run_once() or a callable")
|
|
173
|
+
job = Job(name, run, interval, single, ttl or timedelta(minutes=10), run_at_start)
|
|
174
|
+
if job.ttl <= timedelta(0):
|
|
175
|
+
raise ConfigurationError(f"Job {name!r}: ttl must be positive")
|
|
176
|
+
self.jobs[name] = job
|
|
177
|
+
return job
|
|
178
|
+
|
|
179
|
+
def add_service(self, name: str, target: Any) -> Service:
|
|
180
|
+
"""Add a long-running service: ``await target.run_forever(stop=event)``."""
|
|
181
|
+
self._check_name(name)
|
|
182
|
+
if not callable(getattr(target, "run_forever", None)):
|
|
183
|
+
raise ConfigurationError(f"Service {name!r}: needs run_forever(stop=...)")
|
|
184
|
+
service = Service(name, target)
|
|
185
|
+
self.services[name] = service
|
|
186
|
+
return service
|
|
187
|
+
|
|
188
|
+
def _check_name(self, name: str) -> None:
|
|
189
|
+
if name in self.jobs or name in self.services:
|
|
190
|
+
raise ConfigurationError(f"A job or service named {name!r} already exists")
|
|
191
|
+
|
|
192
|
+
# ---------------------------------------------------------------- running
|
|
193
|
+
|
|
194
|
+
async def _keep_lease(self, job: Job) -> None:
|
|
195
|
+
"""Renew a single job's lease every third of its ttl while it runs, so
|
|
196
|
+
a run longer than the ttl doesn't let another worker start it too.
|
|
197
|
+
Leases of workers that die still expire after the ttl."""
|
|
198
|
+
assert self.locks is not None
|
|
199
|
+
every = job.ttl.total_seconds() / 3
|
|
200
|
+
while True:
|
|
201
|
+
await asyncio.sleep(every)
|
|
202
|
+
try:
|
|
203
|
+
if not await self.locks.acquire(job.name, self.owner, job.ttl):
|
|
204
|
+
logger.warning("Job %s: another worker took the lease mid-run", job.name)
|
|
205
|
+
return
|
|
206
|
+
except Exception:
|
|
207
|
+
logger.exception("Job %s: renewing the lease failed", job.name)
|
|
208
|
+
|
|
209
|
+
async def run_job(self, name: str) -> JobRun:
|
|
210
|
+
"""Run one job now (honouring ``single``). Errors are caught and reported."""
|
|
211
|
+
job = self.jobs[name]
|
|
212
|
+
if job.single:
|
|
213
|
+
assert self.locks is not None
|
|
214
|
+
if not await self.locks.acquire(job.name, self.owner, job.ttl):
|
|
215
|
+
run = JobRun(job.name, "skipped", 0.0)
|
|
216
|
+
await self._observe(run)
|
|
217
|
+
return run
|
|
218
|
+
started = time.perf_counter()
|
|
219
|
+
keeper = asyncio.create_task(self._keep_lease(job)) if job.single else None
|
|
220
|
+
try:
|
|
221
|
+
with bind_log_context(job=job.name):
|
|
222
|
+
async with get_tracer().span(
|
|
223
|
+
f"job {job.name}", attributes={"resource_engine.job": job.name}
|
|
224
|
+
):
|
|
225
|
+
result = job.run()
|
|
226
|
+
if inspect.isawaitable(result):
|
|
227
|
+
result = await result
|
|
228
|
+
run = JobRun(job.name, "ok", time.perf_counter() - started, result)
|
|
229
|
+
except Exception as exc:
|
|
230
|
+
from resource_engine.reporting import report_error
|
|
231
|
+
|
|
232
|
+
with bind_log_context(job=job.name):
|
|
233
|
+
await report_error(exc, where="job", force=True)
|
|
234
|
+
run = JobRun(job.name, "failed", time.perf_counter() - started, error=exc)
|
|
235
|
+
finally:
|
|
236
|
+
if keeper is not None:
|
|
237
|
+
keeper.cancel()
|
|
238
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
239
|
+
await keeper
|
|
240
|
+
if job.single:
|
|
241
|
+
assert self.locks is not None
|
|
242
|
+
try:
|
|
243
|
+
await self.locks.release(job.name, self.owner)
|
|
244
|
+
except Exception:
|
|
245
|
+
logger.exception("Job %s: releasing the lease failed", job.name)
|
|
246
|
+
await self._observe(run)
|
|
247
|
+
return run
|
|
248
|
+
|
|
249
|
+
def describe(self) -> list[Any]:
|
|
250
|
+
"""The jobs and their schedules (``JobInfo``), for the explorer."""
|
|
251
|
+
from resource_engine.ports.activity import JobInfo
|
|
252
|
+
|
|
253
|
+
return [
|
|
254
|
+
JobInfo(job.name, job.every, job.single, job.ttl.total_seconds(), job.run_at_start)
|
|
255
|
+
for job in self.jobs.values()
|
|
256
|
+
]
|
|
257
|
+
|
|
258
|
+
async def register(self) -> None:
|
|
259
|
+
"""Tell the activity log which jobs this worker runs."""
|
|
260
|
+
if self.activity is not None:
|
|
261
|
+
await self.activity.register_jobs(self.owner, self.describe())
|
|
262
|
+
|
|
263
|
+
async def serve_requests(self) -> int:
|
|
264
|
+
"""Run the pending "run now" requests for this worker's jobs; returns
|
|
265
|
+
how many. Each goes through ``run_job``, so a ``single`` job whose lease
|
|
266
|
+
another worker holds is ``skipped``."""
|
|
267
|
+
if self.activity is None or not self.jobs:
|
|
268
|
+
return 0
|
|
269
|
+
requests = await self.activity.claim_requests(self.owner, list(self.jobs))
|
|
270
|
+
for request in requests:
|
|
271
|
+
with bind_log_context(job=request.job):
|
|
272
|
+
logger.info("Job %s: run now, asked by %s", request.job, request.requested_by)
|
|
273
|
+
run = await self.run_job(request.job)
|
|
274
|
+
await self.activity.finish_request(request.id, run.outcome, _error_text(run.error))
|
|
275
|
+
return len(requests)
|
|
276
|
+
|
|
277
|
+
async def _serve_activity(self, stop: asyncio.Event) -> None:
|
|
278
|
+
last_beat: float | None = None # register at once (monotonic time may start near 0)
|
|
279
|
+
while not stop.is_set():
|
|
280
|
+
try:
|
|
281
|
+
if last_beat is None or time.monotonic() - last_beat >= self.heartbeat:
|
|
282
|
+
await self.register()
|
|
283
|
+
last_beat = time.monotonic()
|
|
284
|
+
await self.serve_requests()
|
|
285
|
+
except Exception:
|
|
286
|
+
logger.exception("Worker %s: the activity log failed", self.owner)
|
|
287
|
+
if await _wait(stop, self.requests_every):
|
|
288
|
+
return
|
|
289
|
+
|
|
290
|
+
async def run_once(self) -> list[JobRun]:
|
|
291
|
+
"""Run every job once, in the order they were added (for cron and tests)."""
|
|
292
|
+
return [await self.run_job(name) for name in self.jobs]
|
|
293
|
+
|
|
294
|
+
async def run_forever(self, stop: asyncio.Event | None = None) -> None:
|
|
295
|
+
"""Run jobs on their intervals and every service until ``stop`` is set.
|
|
296
|
+
A service that crashes is logged and restarted after a pause."""
|
|
297
|
+
stop = stop or asyncio.Event()
|
|
298
|
+
if not self.jobs and not self.services:
|
|
299
|
+
raise ConfigurationError("JobRunner has no jobs or services")
|
|
300
|
+
tasks = [
|
|
301
|
+
asyncio.create_task(self._loop(job, stop), name=job.name) for job in self.jobs.values()
|
|
302
|
+
]
|
|
303
|
+
tasks += [
|
|
304
|
+
asyncio.create_task(self._serve(service, stop), name=service.name)
|
|
305
|
+
for service in self.services.values()
|
|
306
|
+
]
|
|
307
|
+
if self.activity is not None:
|
|
308
|
+
tasks.append(asyncio.create_task(self._serve_activity(stop), name="activity"))
|
|
309
|
+
logger.info(
|
|
310
|
+
"Worker %s: %d jobs, %d services", self.owner, len(self.jobs), len(self.services)
|
|
311
|
+
)
|
|
312
|
+
try:
|
|
313
|
+
await stop.wait()
|
|
314
|
+
finally:
|
|
315
|
+
stop.set()
|
|
316
|
+
await asyncio.gather(*tasks, return_exceptions=True)
|
|
317
|
+
|
|
318
|
+
async def _loop(self, job: Job, stop: asyncio.Event) -> None:
|
|
319
|
+
if not job.run_at_start and await _wait(stop, job.every):
|
|
320
|
+
return
|
|
321
|
+
while not stop.is_set():
|
|
322
|
+
await self.run_job(job.name)
|
|
323
|
+
if await _wait(stop, job.every):
|
|
324
|
+
return
|
|
325
|
+
|
|
326
|
+
async def _serve(self, service: Service, stop: asyncio.Event) -> None:
|
|
327
|
+
while not stop.is_set():
|
|
328
|
+
try:
|
|
329
|
+
await service.target.run_forever(stop=stop)
|
|
330
|
+
except Exception:
|
|
331
|
+
logger.exception("Service %s crashed; restarting in 5 s", service.name)
|
|
332
|
+
else:
|
|
333
|
+
if not stop.is_set():
|
|
334
|
+
logger.warning("Service %s returned; restarting in 5 s", service.name)
|
|
335
|
+
if await _wait(stop, 5.0):
|
|
336
|
+
return
|
|
337
|
+
|
|
338
|
+
async def _observe(self, run: JobRun) -> None:
|
|
339
|
+
for observer in self.observers:
|
|
340
|
+
try:
|
|
341
|
+
result = observer(run.job, run.outcome, run.seconds, run.error)
|
|
342
|
+
if inspect.isawaitable(result):
|
|
343
|
+
await result
|
|
344
|
+
except Exception:
|
|
345
|
+
logger.exception("Job observer failed")
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _error_text(error: BaseException | None) -> str | None:
|
|
349
|
+
if error is None:
|
|
350
|
+
return None
|
|
351
|
+
code = getattr(error, "code", None)
|
|
352
|
+
message = getattr(error, "message", None) or str(error)
|
|
353
|
+
return f"{code or type(error).__name__}: {message}"[:1000]
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
class ActivityJobObserver(JobObserver):
|
|
357
|
+
"""``JobObserver`` recording every run in an ``ActivityLog`` (added by
|
|
358
|
+
``JobRunner(activity=...)``)."""
|
|
359
|
+
|
|
360
|
+
def __init__(self, log: Any, owner: str) -> None:
|
|
361
|
+
self.log = log
|
|
362
|
+
self.owner = owner
|
|
363
|
+
|
|
364
|
+
@override
|
|
365
|
+
async def __call__(
|
|
366
|
+
self, job: str, outcome: Outcome, seconds: float, error: BaseException | None
|
|
367
|
+
) -> None:
|
|
368
|
+
from resource_engine.ports.activity import JobRunRecord
|
|
369
|
+
|
|
370
|
+
await self.log.record_job_run(
|
|
371
|
+
JobRunRecord(job, outcome, seconds, self.owner, _error_text(error))
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
async def _wait(stop: asyncio.Event, seconds: float) -> bool:
|
|
376
|
+
"""Sleep ``seconds`` or until ``stop``; True when stopped."""
|
|
377
|
+
with contextlib.suppress(TimeoutError):
|
|
378
|
+
await asyncio.wait_for(stop.wait(), timeout=seconds)
|
|
379
|
+
return stop.is_set()
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
__all__ = [
|
|
383
|
+
"ActivityJobObserver",
|
|
384
|
+
"Job",
|
|
385
|
+
"JobObserver",
|
|
386
|
+
"JobRun",
|
|
387
|
+
"JobRunner",
|
|
388
|
+
"Service",
|
|
389
|
+
"default_owner",
|
|
390
|
+
]
|
|
Binary file
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# Catalan translations of Resource Engine's own messages.
|
|
2
|
+
msgid ""
|
|
3
|
+
msgstr ""
|
|
4
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
|
5
|
+
"Language: ca\n"
|
|
6
|
+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
|
|
7
|
+
|
|
8
|
+
msgctxt "RE-E000"
|
|
9
|
+
msgid "Something went wrong."
|
|
10
|
+
msgstr "Alguna cosa ha fallat."
|
|
11
|
+
|
|
12
|
+
msgctxt "RE-E001"
|
|
13
|
+
msgid "The application is misconfigured."
|
|
14
|
+
msgstr "L'aplicació no està ben configurada."
|
|
15
|
+
|
|
16
|
+
msgctxt "RE-E002"
|
|
17
|
+
msgid "This kind of record does not exist."
|
|
18
|
+
msgstr "Aquest tipus de registre no existeix."
|
|
19
|
+
|
|
20
|
+
msgctxt "RE-E003"
|
|
21
|
+
msgid "This operation is not available."
|
|
22
|
+
msgstr "Aquesta operació no està disponible."
|
|
23
|
+
|
|
24
|
+
msgctxt "RE-E004"
|
|
25
|
+
msgid "The application is misconfigured."
|
|
26
|
+
msgstr "L'aplicació no està ben configurada."
|
|
27
|
+
|
|
28
|
+
msgctxt "RE-E005"
|
|
29
|
+
msgid "The application is misconfigured."
|
|
30
|
+
msgstr "L'aplicació no està ben configurada."
|
|
31
|
+
|
|
32
|
+
msgctxt "RE-E007"
|
|
33
|
+
msgid "The changes could not be saved together."
|
|
34
|
+
msgstr "Els canvis no s'han pogut desar junts."
|
|
35
|
+
|
|
36
|
+
msgctxt "RE-E008"
|
|
37
|
+
msgid "The application is misconfigured."
|
|
38
|
+
msgstr "L'aplicació no està ben configurada."
|
|
39
|
+
|
|
40
|
+
msgctxt "RE-E009"
|
|
41
|
+
msgid "The history is not available."
|
|
42
|
+
msgstr "L'historial no està disponible."
|
|
43
|
+
|
|
44
|
+
msgctxt "RE-E010"
|
|
45
|
+
msgid "A message template is missing."
|
|
46
|
+
msgstr "Falta una plantilla de missatge."
|
|
47
|
+
|
|
48
|
+
msgctxt "RE-E011"
|
|
49
|
+
msgid "A message template could not be filled in."
|
|
50
|
+
msgstr "No s'ha pogut omplir una plantilla de missatge."
|
|
51
|
+
|
|
52
|
+
msgctxt "RE-E012"
|
|
53
|
+
msgid "There are too many changes to save together."
|
|
54
|
+
msgstr "Hi ha massa canvis per desar-los junts."
|
|
55
|
+
|
|
56
|
+
msgctxt "RE-E101"
|
|
57
|
+
msgid "Some fields are not valid."
|
|
58
|
+
msgstr "Alguns camps no són vàlids."
|
|
59
|
+
|
|
60
|
+
msgctxt "RE-E102"
|
|
61
|
+
msgid "The record does not exist."
|
|
62
|
+
msgstr "El registre no existeix."
|
|
63
|
+
|
|
64
|
+
msgctxt "RE-E103"
|
|
65
|
+
msgid "The search cannot be done this way."
|
|
66
|
+
msgstr "La cerca no es pot fer d'aquesta manera."
|
|
67
|
+
|
|
68
|
+
msgctxt "RE-E104"
|
|
69
|
+
msgid "The application is misconfigured."
|
|
70
|
+
msgstr "L'aplicació no està ben configurada."
|
|
71
|
+
|
|
72
|
+
msgctxt "RE-E105"
|
|
73
|
+
msgid "Someone else changed this record. Reload it and try again."
|
|
74
|
+
msgstr "Algú altre ha modificat aquest registre. Torna'l a carregar i prova-ho de nou."
|
|
75
|
+
|
|
76
|
+
msgctxt "RE-E106"
|
|
77
|
+
msgid "Reload the record before changing it."
|
|
78
|
+
msgstr "Torna a carregar el registre abans de modificar-lo."
|
|
79
|
+
|
|
80
|
+
msgctxt "RE-E107"
|
|
81
|
+
msgid "This request was already made with other data."
|
|
82
|
+
msgstr "Aquesta petició ja s'havia fet amb altres dades."
|
|
83
|
+
|
|
84
|
+
msgctxt "RE-E108"
|
|
85
|
+
msgid "A related record does not exist."
|
|
86
|
+
msgstr "Un registre relacionat no existeix."
|
|
87
|
+
|
|
88
|
+
msgctxt "RE-E109"
|
|
89
|
+
msgid "Other records still depend on this one."
|
|
90
|
+
msgstr "Altres registres encara depenen d'aquest."
|
|
91
|
+
|
|
92
|
+
msgctxt "RE-E201"
|
|
93
|
+
msgid "This step is not possible now."
|
|
94
|
+
msgstr "Aquest pas no és possible ara."
|
|
95
|
+
|
|
96
|
+
msgctxt "RE-E202"
|
|
97
|
+
msgid "This step does not exist."
|
|
98
|
+
msgstr "Aquest pas no existeix."
|
|
99
|
+
|
|
100
|
+
msgctxt "RE-E301"
|
|
101
|
+
msgid "This action does not exist."
|
|
102
|
+
msgstr "Aquesta acció no existeix."
|
|
103
|
+
|
|
104
|
+
msgctxt "RE-E401"
|
|
105
|
+
msgid "Some messages could not be sent."
|
|
106
|
+
msgstr "Alguns missatges no s'han pogut enviar."
|
|
107
|
+
|
|
108
|
+
msgctxt "RE-E402"
|
|
109
|
+
msgid "A message could not be handled."
|
|
110
|
+
msgstr "Un missatge no s'ha pogut tractar."
|
|
111
|
+
|
|
112
|
+
msgctxt "RE-E403"
|
|
113
|
+
msgid "The application is misconfigured."
|
|
114
|
+
msgstr "L'aplicació no està ben configurada."
|
|
115
|
+
|
|
116
|
+
msgctxt "RE-E404"
|
|
117
|
+
msgid "The application is misconfigured."
|
|
118
|
+
msgstr "L'aplicació no està ben configurada."
|
|
119
|
+
|
|
120
|
+
msgctxt "RE-E405"
|
|
121
|
+
msgid "A message could not be read."
|
|
122
|
+
msgstr "Un missatge no s'ha pogut llegir."
|
|
123
|
+
|
|
124
|
+
msgctxt "RE-E406"
|
|
125
|
+
msgid "A message was refused."
|
|
126
|
+
msgstr "Un missatge s'ha rebutjat."
|
|
127
|
+
|
|
128
|
+
msgctxt "RE-E501"
|
|
129
|
+
msgid "Another service did not answer. Try again later."
|
|
130
|
+
msgstr "Un altre servei no ha respost. Torna-ho a provar més tard."
|
|
131
|
+
|
|
132
|
+
msgctxt "RE-E502"
|
|
133
|
+
msgid "The email could not be delivered."
|
|
134
|
+
msgstr "El correu no s'ha pogut lliurar."
|
|
135
|
+
|
|
136
|
+
msgctxt "RE-E601"
|
|
137
|
+
msgid "This kind of document does not exist."
|
|
138
|
+
msgstr "Aquest tipus de document no existeix."
|
|
139
|
+
|
|
140
|
+
msgctxt "RE-E602"
|
|
141
|
+
msgid "This type of file is not accepted."
|
|
142
|
+
msgstr "Aquest tipus de fitxer no s'accepta."
|
|
143
|
+
|
|
144
|
+
msgctxt "RE-E603"
|
|
145
|
+
msgid "The file is too large."
|
|
146
|
+
msgstr "El fitxer és massa gran."
|
|
147
|
+
|
|
148
|
+
msgctxt "RE-E604"
|
|
149
|
+
msgid "The upload did not complete."
|
|
150
|
+
msgstr "La pujada no s'ha completat."
|
|
151
|
+
|
|
152
|
+
msgctxt "RE-E605"
|
|
153
|
+
msgid "The document is not available yet."
|
|
154
|
+
msgstr "El document encara no està disponible."
|
|
155
|
+
|
|
156
|
+
msgctxt "RE-E606"
|
|
157
|
+
msgid "A required document is missing."
|
|
158
|
+
msgstr "Falta un document necessari."
|
|
159
|
+
|
|
160
|
+
msgctxt "RE-E607"
|
|
161
|
+
msgid "Files cannot be stored right now. Try again later."
|
|
162
|
+
msgstr "Ara no es poden desar fitxers. Torna-ho a provar més tard."
|
|
163
|
+
|
|
164
|
+
msgctxt "RE-E608"
|
|
165
|
+
msgid "The file does not exist."
|
|
166
|
+
msgstr "El fitxer no existeix."
|
|
167
|
+
|
|
168
|
+
msgctxt "RE-E701"
|
|
169
|
+
msgid "Sign in to continue."
|
|
170
|
+
msgstr "Inicia la sessió per continuar."
|
|
171
|
+
|
|
172
|
+
msgctxt "RE-E702"
|
|
173
|
+
msgid "You are not allowed to do this."
|
|
174
|
+
msgstr "No tens permís per fer això."
|
|
175
|
+
|
|
176
|
+
msgctxt "RE-E703"
|
|
177
|
+
msgid "The policy is not valid."
|
|
178
|
+
msgstr "La política no és vàlida."
|
|
179
|
+
|
|
180
|
+
msgctxt "RE-E704"
|
|
181
|
+
msgid "You cannot grant more than you have."
|
|
182
|
+
msgstr "No pots concedir més del que tens."
|
|
183
|
+
|
|
184
|
+
msgctxt "RE-E705"
|
|
185
|
+
msgid "The policy uses a condition this system cannot apply."
|
|
186
|
+
msgstr "La política fa servir una condició que aquest sistema no pot aplicar."
|
|
187
|
+
|
|
188
|
+
msgctxt "RE-E706"
|
|
189
|
+
msgid "You are not allowed to change some of these fields."
|
|
190
|
+
msgstr "No tens permís per modificar alguns d'aquests camps."
|
|
191
|
+
|
|
192
|
+
msgctxt "RE-E707"
|
|
193
|
+
msgid "You cannot search or sort by this field."
|
|
194
|
+
msgstr "No pots cercar ni ordenar per aquest camp."
|
|
195
|
+
|
|
196
|
+
msgctxt "RE-E708"
|
|
197
|
+
msgid "Choose an organisation first."
|
|
198
|
+
msgstr "Tria primer una organització."
|
|
199
|
+
|
|
200
|
+
msgctxt "RE-E709"
|
|
201
|
+
msgid "This belongs to another organisation."
|
|
202
|
+
msgstr "Això pertany a una altra organització."
|
|
203
|
+
|
|
204
|
+
msgctxt "RE-E710"
|
|
205
|
+
msgid "Too many requests. Wait a moment and try again."
|
|
206
|
+
msgstr "Massa peticions. Espera un moment i torna-ho a provar."
|
|
Binary file
|