You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this provider would first process jobs in `a-long-running-queue`, then `general-queue` and lastly `very-fast-queue`.
84
84
85
+
### Startup resilience and transient database outages
86
+
87
+
Starting from version 1.20.13 (where `PostgreSqlStorageOptions` gained startup resilience options), the storage tries to be more tolerant to *transient* PostgreSQL outages during application startup.
88
+
89
+
#### Default behavior
90
+
91
+
By default, when you use the new-style configuration:
`PostgreSqlStorageOptions` uses the following defaults for startup resilience:
105
+
106
+
-`PrepareSchemaIfNecessary = true`
107
+
-`StartupConnectionMaxRetries = 5`
108
+
-`StartupConnectionBaseDelay = 1 second`
109
+
-`StartupConnectionMaxDelay = 1 minute`
110
+
-`AllowDegradedModeWithoutStorage = true`
111
+
112
+
With these defaults:
113
+
114
+
1. On application startup, when schema preparation is required, the storage will try to open a connection and install/upgrade the schema.
115
+
2. If the database is temporarily unavailable, it will retry the operation up to 6 times (1 initial attempt + 5 retries) with exponential backoff between attempts, capped at 1 minute.
116
+
3. If all attempts fail **during startup**, the storage enters a *degraded* state instead of crashing the whole process. Your ASP.NET Core application can still start and serve other endpoints that do not depend on Hangfire.
117
+
4. On the *first actual use* of the storage (e.g. dashboard, background job server), Hangfire will try to initialize again. If the database is available by then, initialization succeeds and everything works as usual. If it is still unavailable, an `InvalidOperationException` with the original database exception as `InnerException` is thrown at that call site.
118
+
119
+
This behavior is designed to make applications more robust in scenarios where the database may briefly lag behind the application during deployments or orchestrated restarts.
120
+
121
+
#### Opting out of resilient startup (fail fast)
122
+
123
+
If you prefer to fail the whole process immediately if the database is not reachable during startup – you can disable retries by setting `StartupConnectionMaxRetries` to `0`:
- A single attempt is made to open a connection and prepare the schema.
144
+
- If that attempt fails, the storage constructor throws and application startup fails.
145
+
146
+
#### Controlling degraded mode
147
+
148
+
Degraded mode is controlled via `AllowDegradedModeWithoutStorage`:
149
+
150
+
-`true` (default): if all startup attempts fail, both startup and lazy initialization will keep the storage in an uninitialized state on failure and retry on subsequent uses, until initialization eventually succeeds.
151
+
-`false`: if all startup attempts fail, the storage constructor will throw an `InvalidOperationException("Failed to initialize Hangfire PostgreSQL storage.", innerException)`.
152
+
153
+
For example, to keep retries but still fail startup if the DB never becomes available:
154
+
155
+
```csharp
156
+
varstorageOptions=newPostgreSqlStorageOptions
157
+
{
158
+
PrepareSchemaIfNecessary=true,
159
+
StartupConnectionMaxRetries=10, // more aggressive retry policy
AllowDegradedModeWithoutStorage=false, // do not start the app without storage
163
+
};
164
+
```
165
+
166
+
#### Turning off schema preparation entirely
167
+
168
+
If you manage the Hangfire schema yourself (for example via migrations or a dedicated deployment step) and do not want the storage to touch the database during startup or on first use, set `PrepareSchemaIfNecessary = false`:
169
+
170
+
```csharp
171
+
varstorageOptions=newPostgreSqlStorageOptions
172
+
{
173
+
PrepareSchemaIfNecessary=false, // no schema installation/upgrade
174
+
};
175
+
```
176
+
177
+
In this case:
178
+
179
+
- No schema initialization is performed by `PostgreSqlStorage`.
180
+
- The first query that actually needs the database will fail if the schema is missing or mismatched, so you must ensure it is created/updated out of band.
181
+
182
+
> Note: startup resilience settings (`StartupConnectionMaxRetries`, `AllowDegradedModeWithoutStorage`, etc.) only apply when `PrepareSchemaIfNecessary` is `true`.
0 commit comments