thecore_backend_commons 3.14.0 → 3.15.0
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.
- checksums.yaml +4 -4
- data/README.md +9 -58
- data/config/initializers/application_config.rb +2 -7
- data/lib/thecore_backend_commons/activity_log.rb +1 -1
- data/lib/thecore_backend_commons/version.rb +1 -1
- data/lib/thecore_backend_commons.rb +0 -2
- metadata +4 -7
- data/config/initializers/sidekiq.rb +0 -11
- data/lib/thecore_backend_commons/discarded_job_logger.rb +0 -31
- data/lib/thecore_backend_commons/job_failure_policy.rb +0 -54
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c25e75a330b000da97adb51aadde4893de944605914b1d307febcba8ba310739
|
|
4
|
+
data.tar.gz: 702b620b89c37ecb791bfc4644438aa0057111ddb236ef7cd8100f2947950475
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c16a2c450be82f46f8113ba82d2939c44197f07f60d0bb9dd2d4ae2d9c414b092c7c58c888acae959ec1a6d9c8c82cd18db7f388f7e075d089a64fa7e56bd4f8
|
|
7
|
+
data.tar.gz: 83c0155ac3b4b2f85a6258a4f3b31ab2acca3f1c76f64127e05468f12d0cdaec2f0826a9dc6ca7e60af9122b9f3a6f646647453b61a9b90814384b710a20f08d
|
data/README.md
CHANGED
|
@@ -175,64 +175,15 @@ overrides it. Full mechanics (idempotency, abstract/STI exclusion, `to_prepare`
|
|
|
175
175
|
|
|
176
176
|
---
|
|
177
177
|
|
|
178
|
-
## Background jobs
|
|
179
|
-
|
|
180
|
-
Every app using this gem runs its background jobs
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
- **every failure is written to the Rails log** by `ThecoreBackendCommons::DiscardedJobLogger`:
|
|
188
|
-
job class (or `Mailer#action`), queue, jid and error, never the arguments.
|
|
189
|
-
|
|
190
|
-
Why zero: in a Thecore app in production, mail jobs carrying a whole PDF in their arguments
|
|
191
|
-
failed with a permanent SMTP error and were retried for weeks, then kept in the Dead Set. Redis
|
|
192
|
-
grew to 23 GB until the host's OOM killer put it in a restart loop and no job ran for three days
|
|
193
|
-
(`docs/adr/0002-no-job-retries-failures-logged.md`).
|
|
194
|
-
|
|
195
|
-
### Changing the defaults on an installation
|
|
196
|
-
|
|
197
|
-
Set these in the environment of both the `backend` and the `worker` containers. On an
|
|
198
|
-
installation deployed by `thecore_devcontainer` that is the target's
|
|
199
|
-
`vendor/deploytargets/<provider>/<customer>.env`, which reaches both containers through their
|
|
200
|
-
`env_file` (`thecore_devcontainer` ADR 0002); on a hand-written compose, its `environment:`.
|
|
201
|
-
|
|
202
|
-
| Variable | Unset or blank | Accepted values | Effect |
|
|
203
|
-
| --- | --- | --- | --- |
|
|
204
|
-
| `THECORE_JOB_RETRIES` | `0` (no retries) | a whole number >= 0 | how many times Sidekiq retries a failed job, app-wide |
|
|
205
|
-
| `THECORE_FAILED_JOBS_KEPT` | `1` | a whole number >= 1 | how many failed jobs stay in the Redis `failed` set |
|
|
206
|
-
|
|
207
|
-
Any other value (`yes`, `-1`, `1.5`, ...) stops the app at boot with an error naming the
|
|
208
|
-
variable: a typo never silently turns retries back on. With retries above 0, a job that uses
|
|
209
|
-
them all ends in Sidekiq's Dead Set, which is capped by count and age but not by size.
|
|
210
|
-
|
|
211
|
-
### Changing it for one job
|
|
212
|
-
|
|
213
|
-
A job class sets its own retry count with Sidekiq's option, which wins over the app-wide value
|
|
214
|
-
in both directions (it works on ActiveJob classes and mailers' jobs alike):
|
|
215
|
-
|
|
216
|
-
```ruby
|
|
217
|
-
class SyncInventoryJob < ApplicationJob
|
|
218
|
-
sidekiq_options retry: 3 # or retry: false when THECORE_JOB_RETRIES is above 0
|
|
219
|
-
end
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
**Don't use ActiveJob's `retry_on` for this.** It retries independently of Sidekiq: on failure
|
|
223
|
-
ActiveJob enqueues a new copy of the job, arguments included, whatever `THECORE_JOB_RETRIES`
|
|
224
|
-
says, and the copies never show up as Sidekiq retries.
|
|
225
|
-
|
|
226
|
-
### Logging failures somewhere else too
|
|
227
|
-
|
|
228
|
-
Add your own Sidekiq death handler next to the default one; both run:
|
|
229
|
-
|
|
230
|
-
```ruby
|
|
231
|
-
# config/initializers/sidekiq.rb in the app
|
|
232
|
-
Sidekiq.configure_server do |config|
|
|
233
|
-
config.death_handlers << ->(job, exception) { MyErrorLog.record(job["jid"], exception) }
|
|
234
|
-
end
|
|
235
|
-
```
|
|
178
|
+
## Background jobs
|
|
179
|
+
|
|
180
|
+
Every app using this gem runs its background jobs as `thecore_background_jobs` (`~> 3.1`, a
|
|
181
|
+
dependency of this gem) sets them up: Solid Queue in production, no retries by default
|
|
182
|
+
(`THECORE_JOB_RETRIES`, `THECORE_FAILED_JOBS_KEPT`), one log line per failed job without its
|
|
183
|
+
arguments, and `ThecoreBackgroundJobs.on_job_failure(handler)` to record failures elsewhere too.
|
|
184
|
+
See that gem's README. Until 3.14 this gem configured Sidekiq for it
|
|
185
|
+
(`docs/adr/0001-active-job-queue-adapter-is-framework-imposed.md`,
|
|
186
|
+
`docs/adr/0002-no-job-retries-failures-logged.md`, both amended by thecore ADR 0017).
|
|
236
187
|
|
|
237
188
|
---
|
|
238
189
|
|
|
@@ -8,10 +8,5 @@ Rails.application.config.action_mailer.delivery_method = :smtp
|
|
|
8
8
|
|
|
9
9
|
Rails.application.config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/, /file:\/\/*/, 'file://', nil]
|
|
10
10
|
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
# and mounts Sidekiq::Web unconditionally (thecore_background_jobs), so ActiveJob should actually
|
|
14
|
-
# route through it rather than defaulting to Rails' in-process :async, which silently drops any
|
|
15
|
-
# job in flight when its process restarts. Production only: :async remains the default in
|
|
16
|
-
# development/test so neither requires a reachable Redis. See docs/adr/0001, GLOSSARY.md.
|
|
17
|
-
Rails.application.config.active_job.queue_adapter = :sidekiq if Rails.env.production?
|
|
11
|
+
# The production queue adapter (Solid Queue), the job failure policy and the failure logger are
|
|
12
|
+
# imposed by thecore_background_jobs since 3.1, not here (thecore ADR 0017, docs/adr/0001 and 0002).
|
|
@@ -12,7 +12,7 @@ module ThecoreBackendCommons
|
|
|
12
12
|
TRUE_VALUES = %w[true 1 yes on].freeze
|
|
13
13
|
# The setting is read at most once per this many seconds per process, so a save never pays a
|
|
14
14
|
# query for it. The process that saves the setting forgets the cached value at once (SettingHook);
|
|
15
|
-
# every other process (web workers,
|
|
15
|
+
# every other process (web workers, job workers) sees the change within this delay.
|
|
16
16
|
CACHE_SECONDS = 60
|
|
17
17
|
|
|
18
18
|
# Included into ThecoreSettings::Setting (config/initializers/activity_log.rb).
|
|
@@ -20,8 +20,6 @@ require "thecore_backend_commons/smtp_tester"
|
|
|
20
20
|
require "thecore_backend_commons/push_notification_service"
|
|
21
21
|
require "thecore_backend_commons/default_module_registry"
|
|
22
22
|
require "thecore_backend_commons/activity_log"
|
|
23
|
-
require "thecore_backend_commons/discarded_job_logger"
|
|
24
|
-
require "thecore_backend_commons/job_failure_policy"
|
|
25
23
|
|
|
26
24
|
module ThecoreBackendCommons
|
|
27
25
|
# Deep-merges +dest+ into +src+ (mutating and returning +src+), extending existing arrays
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: thecore_backend_commons
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.15.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thecore_auth_commons
|
|
@@ -30,14 +30,14 @@ dependencies:
|
|
|
30
30
|
requirements:
|
|
31
31
|
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '3.
|
|
33
|
+
version: '3.1'
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '3.
|
|
40
|
+
version: '3.1'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: ostruct
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -242,7 +242,6 @@ files:
|
|
|
242
242
|
- config/initializers/default_module_registry.rb
|
|
243
243
|
- config/initializers/extension_nil.rb
|
|
244
244
|
- config/initializers/extension_string.rb
|
|
245
|
-
- config/initializers/sidekiq.rb
|
|
246
245
|
- config/locales/en.devise.custom.yml
|
|
247
246
|
- config/locales/en.yml
|
|
248
247
|
- config/locales/it.devise.custom.yml
|
|
@@ -257,9 +256,7 @@ files:
|
|
|
257
256
|
- lib/thecore_backend_commons.rb
|
|
258
257
|
- lib/thecore_backend_commons/activity_log.rb
|
|
259
258
|
- lib/thecore_backend_commons/default_module_registry.rb
|
|
260
|
-
- lib/thecore_backend_commons/discarded_job_logger.rb
|
|
261
259
|
- lib/thecore_backend_commons/engine.rb
|
|
262
|
-
- lib/thecore_backend_commons/job_failure_policy.rb
|
|
263
260
|
- lib/thecore_backend_commons/push_notification_service.rb
|
|
264
261
|
- lib/thecore_backend_commons/smtp_config.rb
|
|
265
262
|
- lib/thecore_backend_commons/smtp_tester.rb
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
# What a failed background job leaves behind, app-wide, from the container's environment:
|
|
2
|
-
# THECORE_JOB_RETRIES (default 0, no retries) and THECORE_FAILED_JOBS_KEPT (default 1, the latest
|
|
3
|
-
# failure in the sidekiq-failures set); an invalid value refuses to boot (JobFailurePolicy).
|
|
4
|
-
# Why: docs/adr/0002-no-job-retries-failures-logged.md.
|
|
5
|
-
ThecoreBackendCommons::JobFailurePolicy.apply!
|
|
6
|
-
|
|
7
|
-
# Every failed job leaves one line in the Rails log (DiscardedJobLogger); apps append their own
|
|
8
|
-
# handlers next to it. Required, not autoloaded: see docs/adr/0002-no-job-retries-failures-logged.md.
|
|
9
|
-
Sidekiq.configure_server do |config|
|
|
10
|
-
config.death_handlers << ThecoreBackendCommons::DiscardedJobLogger unless config.death_handlers.include?(ThecoreBackendCommons::DiscardedJobLogger)
|
|
11
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
module ThecoreBackendCommons
|
|
2
|
-
# Generic Sidekiq death handler, registered by config/initializers/sidekiq.rb: with retries off
|
|
3
|
-
# Sidekiq drops a failed job without keeping it anywhere, so this writes one line to the Rails log
|
|
4
|
-
# per failure. Only the job's identity goes in: class (or Mailer#action for a mail job), queue, jid
|
|
5
|
-
# and the error, never the arguments, which can be huge or sensitive (docs/adr/0002). Apps add their own handlers next to this one (`config.death_handlers << MyHandler`
|
|
6
|
-
# inside `Sidekiq.configure_server`), e.g. to record failures in their own integration log.
|
|
7
|
-
module DiscardedJobLogger
|
|
8
|
-
MAX_MESSAGE_LENGTH = 500
|
|
9
|
-
|
|
10
|
-
def self.call(job, exception)
|
|
11
|
-
error = "#{exception.class}: #{exception.message}".truncate(MAX_MESSAGE_LENGTH)
|
|
12
|
-
Rails.logger.error(
|
|
13
|
-
"Background job #{name_of(job)} failed and was discarded (no retry left): " \
|
|
14
|
-
"queue=#{job["queue"]} jid=#{job["jid"]} error=#{error}"
|
|
15
|
-
)
|
|
16
|
-
rescue StandardError => e
|
|
17
|
-
Rails.logger.error("DiscardedJobLogger could not record a failed job: #{e.class}: #{e.message}")
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def self.name_of(job)
|
|
21
|
-
wrapped = (job["wrapped"] || job["class"]).to_s
|
|
22
|
-
first = Array(job["args"]).first
|
|
23
|
-
arguments = first.is_a?(Hash) ? Array(first["arguments"]) : []
|
|
24
|
-
return wrapped unless wrapped == "ActionMailer::MailDeliveryJob" && arguments.size >= 2
|
|
25
|
-
|
|
26
|
-
"#{arguments[0]}##{arguments[1]}"
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
private_class_method :name_of
|
|
30
|
-
end
|
|
31
|
-
end
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
module ThecoreBackendCommons
|
|
2
|
-
# What happens to a failed background job, app-wide, read from the container's environment by
|
|
3
|
-
# config/initializers/sidekiq.rb (docs/adr/0002-no-job-retries-failures-logged.md):
|
|
4
|
-
#
|
|
5
|
-
# - THECORE_JOB_RETRIES: how many times Sidekiq retries a failed job. Unset or blank = 0, i.e.
|
|
6
|
-
# `retry: false` (not `retry: 0`, which would still send the job to the Dead Set).
|
|
7
|
-
# - THECORE_FAILED_JOBS_KEPT: how many failed jobs sidekiq-failures keeps in the Redis `failed`
|
|
8
|
-
# set, payload and arguments included (Sidekiq.failures_max_count). Unset or blank = 1, the
|
|
9
|
-
# latest, still shown in the Failures tab.
|
|
10
|
-
#
|
|
11
|
-
# Any other value refuses to boot: a typo must never silently re-enable retries or keep every
|
|
12
|
-
# failed payload. A job class still overrides the retry count with `sidekiq_options retry: N`.
|
|
13
|
-
# Required, not autoloaded: see the ADR.
|
|
14
|
-
module JobFailurePolicy
|
|
15
|
-
RETRIES_VARIABLE = "THECORE_JOB_RETRIES".freeze
|
|
16
|
-
KEPT_VARIABLE = "THECORE_FAILED_JOBS_KEPT".freeze
|
|
17
|
-
DEFAULT_RETRIES = 0
|
|
18
|
-
DEFAULT_KEPT = 1
|
|
19
|
-
|
|
20
|
-
class InvalidValue < StandardError; end
|
|
21
|
-
|
|
22
|
-
# Sets Sidekiq's app-wide default retry option and the sidekiq-failures cap.
|
|
23
|
-
def self.apply!
|
|
24
|
-
Sidekiq.default_job_options = Sidekiq.default_job_options.merge("retry" => sidekiq_retry_option)
|
|
25
|
-
Sidekiq.failures_max_count = failed_jobs_kept
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# The value for Sidekiq's `retry` option: false for no retries, else the count.
|
|
29
|
-
def self.sidekiq_retry_option
|
|
30
|
-
retries.zero? ? false : retries
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def self.retries
|
|
34
|
-
whole_number_from_env(RETRIES_VARIABLE, default: DEFAULT_RETRIES, minimum: 0, expected: "a whole number >= 0")
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def self.failed_jobs_kept
|
|
38
|
-
whole_number_from_env(KEPT_VARIABLE, default: DEFAULT_KEPT, minimum: 1, expected: "a whole number >= 1")
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# The whole number in the environment variable, default when unset or blank; raises InvalidValue
|
|
42
|
-
# on anything else or below minimum.
|
|
43
|
-
def self.whole_number_from_env(variable, default:, minimum:, expected:)
|
|
44
|
-
raw = ENV.fetch(variable, nil)
|
|
45
|
-
return default if raw.nil? || raw.strip.empty?
|
|
46
|
-
|
|
47
|
-
value = raw.strip
|
|
48
|
-
return value.to_i if value.match?(/\A\d+\z/) && value.to_i >= minimum
|
|
49
|
-
|
|
50
|
-
raise InvalidValue, "#{variable}=#{raw.inspect} is not valid: use #{expected}, or leave it unset (default #{default})"
|
|
51
|
-
end
|
|
52
|
-
private_class_method :whole_number_from_env
|
|
53
|
-
end
|
|
54
|
-
end
|