Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Make the sidekiq-cron grace period configurable (#4972)

arnaurodondev4affedf0688e
diff --git a/.env.example b/.env.example
index 4906dfced0..d75a91c2ff 100644
--- a/.env.example
+++ b/.env.example
@@ -205,6 +205,12 @@ REDIS_URL=redis://localhost:6379/1
# SIDEKIQ_WEB_USERNAME=
# SIDEKIQ_WEB_PASSWORD=
+# Scheduled jobs (daily sync, market data import, cleanups) that were missed by
+# more than this many seconds are skipped, for example while the host sleeps.
+# Default 600 (10 minutes). Laptops or NAS boxes that sleep overnight can set
+# 86400 so the most recent missed run of each job still happens once on wake.
+# SIDEKIQ_CRON_GRACE_PERIOD_SECONDS=600
+
# App Domain
# This is the domain that your Sure instance will be hosted at. It is used to generate links in emails and other places.
APP_DOMAIN=
diff --git a/Gemfile.lock b/Gemfile.lock
index b4c47801cd..7a87df0648 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -799,7 +799,7 @@ GEM
logger (>= 1.6.2)
rack (>= 3.1.0)
redis-client (>= 0.23.2)
- sidekiq-cron (2.3.0)
+ sidekiq-cron (2.4.0)
cronex (>= 0.13.0)
fugit (~> 1.8, >= 1.11.1)
globalid (>= 1.0.1)
diff --git a/compose.example.ai.yml b/compose.example.ai.yml
index a8dc705c14..4d0ee80cdc 100644
--- a/compose.example.ai.yml
+++ b/compose.example.ai.yml
@@ -92,6 +92,8 @@ x-rails-env: &rails_env
DB_HOST: db
DB_PORT: 5432
REDIS_URL: redis://redis:6379/1
+ # Seconds a missed scheduled job may run late (default 600); see docs/hosting/docker.md
+ SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: ${SIDEKIQ_CRON_GRACE_PERIOD_SECONDS:-}
# MCP server endpoint — enables /mcp for external AI assistants (e.g. Claude, GPT).
# Set both values to activate. MCP_USER_EMAIL must match an existing user's email.
# External AI clients should connect via Pipelock (port 8889) for scanning.
diff --git a/compose.example.yml b/compose.example.yml
index b44f135c65..bf8a2fe6d5 100644
--- a/compose.example.yml
+++ b/compose.example.yml
@@ -71,6 +71,8 @@ x-rails-env: &rails_env
DB_HOST: db
DB_PORT: 5432
REDIS_URL: redis://redis:6379/1
+ # Seconds a missed scheduled job may run late (default 600); see docs/hosting/docker.md
+ SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: ${SIDEKIQ_CRON_GRACE_PERIOD_SECONDS:-}
# NOTE: enabling OpenAI will incur costs when you use AI-related features in the app (chat, rules). Make sure you have set appropriate spend limits on your account before adding this.
OPENAI_ACCESS_TOKEN: ${OPENAI_ACCESS_TOKEN}
OPENAI_MODEL: ${OPENAI_MODEL:-}
diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb
index 1fdf88b702..623e35a32e 100644
--- a/config/initializers/sidekiq.rb
+++ b/config/initializers/sidekiq.rb
@@ -96,6 +96,10 @@ Sidekiq.configure_client do |config|
end
Sidekiq::Cron.configure do |config|
- # 10 min "catch-up" window in case worker process is re-deploying when cron tick occurs
- config.reschedule_grace_period = 600
+ # How late (in seconds) a missed cron tick may still be enqueued. The 10 min
+ # default covers a worker re-deploying when the tick occurs; hosts that sleep
+ # (laptops, NAS) can raise SIDEKIQ_CRON_GRACE_PERIOD_SECONDS so the most recent
+ # missed run still happens once on wake.
+ grace_period = Integer(ENV.fetch("SIDEKIQ_CRON_GRACE_PERIOD_SECONDS", "600"), exception: false)
+ config.reschedule_grace_period = grace_period&.positive? ? grace_period : 600
end
diff --git a/docs/hosting/docker.md b/docs/hosting/docker.md
index 1ccfe88e1e..0140251524 100644
--- a/docs/hosting/docker.md
+++ b/docs/hosting/docker.md
@@ -433,6 +433,18 @@ MARKET_DATA_SYNC_TIMEZONE=UTC # IANA name such as Europe/Berlin; unknown nam
**Upgrading from an earlier version:** the import used to be a fixed entry in `config/schedule.yml`. It is now created by the worker when it starts, from the settings above. Without any change on your side it keeps running at 22:00 UTC, Monday to Friday. If prices stop updating after an upgrade, open `/sidekiq` (see [Inspecting background jobs](#inspecting-background-jobs-sidekiq)), check that the **Cron** tab lists `import_market_data`, and look for `[MarketDataScheduler]` lines in the worker log (`docker compose logs worker`). Saving the import time once on the settings page recreates the job.
+### Hosts that sleep (laptops, NAS)
+
+Scheduled jobs such as the daily account sync and the market data import only run if the worker is awake within 10 minutes of their scheduled time. A run missed by more than that is skipped until the next day. If your host sleeps overnight, either schedule the jobs for a time it is awake, or let missed runs catch up by raising the grace period in your `.env` file:
+
+```txt
+SIDEKIQ_CRON_GRACE_PERIOD_SECONDS=86400 # default 600 (10 minutes)
+```
+
+When the worker wakes, each job then runs once for its most recent missed time, but only if that time is within the grace period; otherwise it waits for its next scheduled time (older missed runs are not replayed). For example, a Friday 22:00 import missed until Monday morning needs `259200` (3 days) to catch up; with `86400` it waits until Monday 22:00.
+
+The value only reaches the worker if your compose file passes it: the current `compose.example.yml` and `compose.example.ai.yml` forward it in the `x-rails-env` block. If your `compose.yml` was copied from an older example, add `SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: ${SIDEKIQ_CRON_GRACE_PERIOD_SECONDS:-}` under `x-rails-env`. After changing it, run `docker compose up -d worker` (`docker compose -f compose.ai.yml up -d worker` for the AI compose file) to recreate the worker; `docker compose restart` keeps the old value.
+
## How to update your app
The mechanism that updates your self-hosted Sure app is the GHCR (Github Container Registry) Docker image that you see in the `compose.yml` file:
diff --git a/test/initializers/sidekiq_test.rb b/test/initializers/sidekiq_test.rb
new file mode 100644
index 0000000000..6db3f88024
--- /dev/null
+++ b/test/initializers/sidekiq_test.rb
@@ -0,0 +1,52 @@
+require "test_helper"
+
+class SidekiqInitializerTest < ActiveSupport::TestCase
+ setup do
+ @previous_grace_period = Sidekiq::Cron.configuration.reschedule_grace_period
+ end
+
+ teardown do
+ Sidekiq::Cron.configuration.reschedule_grace_period = @previous_grace_period
+ end
+
+ test "cron grace period defaults to 10 minutes" do
+ load_initializer
+
+ assert_equal 600, Sidekiq::Cron.configuration.reschedule_grace_period
+ end
+
+ test "cron grace period can be raised for hosts that sleep" do
+ load_initializer(SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: "86400")
+
+ assert_equal 86_400, Sidekiq::Cron.configuration.reschedule_grace_period
+ end
+
+ test "an invalid cron grace period falls back to the default" do
+ [ "abc", "0", "-5" ].each do |value|
+ load_initializer(SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: value)
+
+ assert_equal 600, Sidekiq::Cron.configuration.reschedule_grace_period, value
+ end
+ end
+
+ test "a daily run missed while the host slept is enqueued on wake only within the grace period" do
+ job = Sidekiq::Cron::Job.new(name: "sidekiq_initializer_test_#{SecureRandom.hex(8)}", cron: "22 2 * * * Etc/UTC",
+ class: "SyncAllJob", status: "enabled")
+ woke_at = Time.utc(2026, 11, 9, 7, 0)
+
+ load_initializer
+ assert_not job.should_enqueue?(woke_at)
+
+ load_initializer(SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: "86400")
+ assert job.should_enqueue?(woke_at)
+ ensure
+ job&.destroy
+ end
+
+ private
+ def load_initializer(**overrides)
+ with_env_overrides({ SIDEKIQ_CRON_GRACE_PERIOD_SECONDS: nil, SIDEKIQ_WEB_USERNAME: nil, SIDEKIQ_WEB_PASSWORD: nil }.merge(overrides)) do
+ load Rails.root.join("config/initializers/sidekiq.rb")
+ end
+ end
+end