Background jobs and notifications
Invidious uses long-running background jobs to keep channel data, user feeds, shared caches, instance data, statistics, and expired records current. New channel videos move from a feed event or channel refresh into a shared notification path, where they become stored user updates and live events for connected browsers.
Job system
The job system starts independent maintenance loops with the main Invidious process. Each job has a small configuration block and can be enabled or disabled without changing the job’s purpose. The process registers only the jobs allowed by the main feature settings, then starts the registered jobs together.
References: src/…/base_job.cr• L1-33 src/…/jobs.cr• L1-40 src/invidious.cr• L163-193
Maintenance work
Maintenance jobs update the data that users and public pages rely on. They use bounded concurrent work for channel and feed refreshes, while simpler jobs run at fixed intervals.
| Job area | System purpose | User-facing result |
|---|---|---|
| Channel refresh | Find new channel videos and update channel data | Subscription feeds and notifications gain new videos |
| Feed refresh | Rebuild a user’s subscription view after a change | The subscription feed reflects current subscriptions |
| PubSub feed subscription | Keep external feed subscriptions active | New channel events can arrive without waiting for a full scan |
| Popular videos | Refresh the shared popular-video set | Popular pages use recent data |
| Statistics | Refresh service and usage figures | Statistics endpoints report current values |
| Expired-item cleanup | Remove old cached videos and nonces | Stale records do not remain in the data store |
| Instance list refresh | Keep the redirect target list current | Redirects can use healthy and recent instances |
Channel refresh is the main source of discovered videos. It scans channels on a recurring schedule and slows its work after fetch errors. Feed refresh then handles users marked as needing an update, so channel changes and subscription changes do not require a request to rebuild the feed immediately.
References: src/…/refresh_channels_job.cr• L1-71 src/…/refresh_feeds_job.cr• L1-75 src/…/subscribe_to_feeds_job.cr• L1-50
The remaining jobs maintain shared service state rather than a single user feed. Cleanup retries sooner after a database error. Popular videos refresh often, statistics refresh on a longer cycle, and the instance list accepts only suitable remote instances before replacing the current list.
References: src/…/clear_expired_items_job.cr• L1-27 src/…/pull_popular_videos_job.cr• L1-20 src/…/statistics_refresh_job.cr• L1-70 src/…/instance_refresh_job.cr• L1-97
Notification flow
The notification flow turns a newly stored channel video into two user paths: a saved update in the account data and a live event for browsers that are watching subscribed channels. A VideoNotification is the small event record that carries the video ID, channel ID, and publish time between the discovery source and the notification job.
References: src/…/notification_job.cr• L1-29 src/…/feeds.cr• L404-456 src/…/channels.cr• L244-255
This diagram shows how a new video moves from discovery to stored and live notification results.
flowchart LR
A[Channel refresh or PubSub webhook] --> B[Store new channel video]
B --> C[Video notification event]
C --> D[Notification job batches by channel]
D --> E[Store IDs for subscribed users]
D --> F[PostgreSQL notification event]
F --> G[Authenticated event stream]
G --> H[Browser ticker or system alert]
E --> I[Subscription feed]
Both channel refresh and the PubSub webhook send an event only after a new video enters the channel-video data set. The notification job groups events by channel and removes duplicates during its short batch window. User notifications also mark affected feeds for refresh. If live user notifications are off, the system keeps the feed-update signal without sending the user-notification events.
References: src/…/channels.cr• L244-255 src/…/feeds.cr• L404-456 src/…/notification_job.cr• L32-99 src/…/users.cr• L122-165 src/…/config.cr• L154-155
User notification routes
The authenticated notification route exposes a Server-Sent Events stream. Server-Sent Events, or SSE, is a one-way HTTP stream that lets a browser receive updates without polling. The client supplies channel IDs as topics, and the server sends matching video data as events.
References: src/…/routing.cr• L318-321 src/…/authenticated.cr• L481-489 src/…/helpers.cr• L64-170
The browser starts one stream across its open tabs. It loads the account’s subscriptions, claims a shared browser-storage flag, and reconnects after a stream error. Each event increases the local notification count. When browser permission is granted, the client also shows a system alert that links to the new video.
References: assets/…/notifications.js• L1-75 assets/…/notifications.js• L90-131
The stream supports two data sources. A client can request stored notifications newer than a supplied time, and the active PostgreSQL event path can deliver new events while the connection stays open. Heartbeats keep the connection active. The stream is limited to the requested topics so one user does not receive unrelated channel updates.
References: src/…/helpers.cr• L64-170 src/…/notification_job.cr• L73-85
Notification state
Stored notification IDs provide the durable part of the feature. They are attached to users who subscribe to the channel and are also used to mark the affected subscription feed for a later refresh. The subscription feed can present those videos as a notification-only view or separate them from normal feed results according to user preferences. See accounts, sessions, and preferences for the wider account state model and persistence and schema migrations for the data store life cycle.
References: src/…/users.cr• L122-165 src/…/users.cr• L25-105 src/…/0004_create_users_table.cr• L5-19
Reading the subscription feed clears its stored notification markers because the feed view counts as the user seeing the update. Opening a notified video also removes that video’s marker. This creates a clear boundary between durable account state and the browser’s temporary live-event count.
References: src/…/feeds.cr• L94-103 src/…/watch.cr• L70-78 src/…/users.cr• L133-150 assets/…/notifications.js• L45-65
Configuration boundaries
Job registration and notification delivery are controlled at different levels. Main settings decide whether a feature-specific job is registered, while the job configuration can disable a registered job. User notifications have a separate global switch that controls their routes and delivery behavior.
References: src/invidious.cr• L163-193 src/…/jobs.cr• L12-40 src/…/base_job.cr• L4-30 src/…/config.cr• L121-155 src/…/routing.cr• L40-42
The main settings that shape this area are:
- Channel and feed worker limits control how much refresh work can run at once.
- PubSub feed settings control whether Invidious maintains external feed subscriptions.
- Popular, statistics, and user-notification settings control optional system features.
- The database URL and HMAC key support the event store and signed PubSub requests.
Change checklist
When these files or sections are changed, remember to consider these:
- A change to the new-video event shape must stay aligned across channel discovery, the notification job, PostgreSQL events, and the SSE response builder.
- A change to stored notification IDs must preserve the link between the users table, subscription-feed selection, feed-read clearing, and video-read removal.
- A change to notification route availability must stay aligned between the feature switch, authenticated routing, and the browser stream startup.
- A change to PubSub verification or delivery must preserve both signed hub requests and the handoff from webhook work to the shared notification channel.
- A change to job registration or job names must preserve the YAML job configuration mapping generated from job types.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A new channel video is not inserted | No notification event is created | Discovery sends the event only after a successful insert |
| A PubSub signature is invalid | The payload is ignored and no video update is processed | The webhook checks the HMAC signature before parsing the payload |
| A notification event names a channel outside the client topics | The connected browser does not receive the event | The stream filters PostgreSQL events by requested topics |
| A channel or feed refresh keeps failing | Refresh work slows and data can remain old | Refresh jobs cap concurrent work and apply backoff or retry cycles |
| The notification database update fails | Stored markers and live delivery can lag behind discovery | The notification loop logs the error and wakes again after its batch delay |
Key files
| File | Why to read it |
|---|---|
src/invidious.cr | Shows which jobs start and which feature settings gate registration. |
src/invidious/jobs.cr and src/invidious/jobs/base_job.cr | Defines the shared job lifecycle and job configuration model. |
src/invidious/jobs/notification_job.cr | Connects discovered videos to stored user markers and live database events. |
src/invidious/jobs/refresh_channels_job.cr | Shows the main recurring source of channel-video discovery. |
src/invidious/routes/feeds.cr | Shows signed PubSub intake and the event created for each inserted video. |
src/invidious/helpers/helpers.cr | Shows how authenticated clients receive stored and live SSE events. |
| assets/…/notifications.js | Shows how the browser claims a stream and surfaces updates to the user. |
src/invidious/users.cr | Shows how stored notification state affects the subscription feed. |
src/invidious/database/users.cr | Shows how user notification markers and feed-update flags change. |