Skip to content

Commit ca31e4b

Browse files
committed
polish rdbms jobs
1 parent d8230e8 commit ca31e4b

File tree

1 file changed

+55
-39
lines changed

1 file changed

+55
-39
lines changed

MyApp/_hold/2025-10-17_rdbms_jobs.md

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,117 @@
11
---
22
title: RDBMS Background Jobs
3-
summary: Run Background Jobs and Scheduled Tasks in PostgreSQL, SQL Server or MySQL
3+
summary: Run Background Jobs and Scheduled Tasks in PostgreSQL, SQL Server or MySQL
44
tags: [db,ormlite,jobs]
55
author: Demis Bellot
66
image: ./img/posts/background-jobs/bg.webp
7-
draft: true
87
---
98

109
We're excited to announce that we've ported our much loved [Background Jobs](https://docs.servicestack.net/background-jobs)
11-
feature for SQLite to the popular **PostgreSQL**, **SQL Server** and **MySQL** RDBMS's.
10+
feature to the popular **PostgreSQL**, **SQL Server** and **MySQL** RDBMS's!
1211

13-
Whilst we love [SQLite + Litestream](https://docs.servicestack.net/ormlite/litestream) for its low dev ops maintenance
14-
allowing us to break free from
15-
[expensive cloud hosting hosts](https://docs.servicestack.net/ormlite/litestream#the-right-time-for-server-side-sqlite)
16-
for managed RDBMS's, it's clear many of our Customers need the features of an industrial strength RDBMS.
12+
Since launching [Background Jobs](https://servicestack.net/posts/background-jobs) in September 2024, it's become
13+
one of our most popular features - providing a simple, infrastructure-free solution for managing background jobs
14+
and scheduled tasks in .NET 8+ Apps. The original implementation used SQLite for its durability, which worked
15+
beautifully for many use cases thanks to SQLite's low latency, fast disk persistence, and zero infrastructure requirements.
1716

18-
In future we'll also be looking at providing a great self-hosted manged solution for Customers that can be run free of
19-
expensive cloud hosting costs (starting with PostgreSQL). Before we can focus on this we needed to rewrite all our
20-
SQLite-only features to work with OrmLite's other premier supported RDBMS's.
17+
However, we recognize that many of our customers need the features and scalability of industrial-strength RDBMS systems.
18+
Whether it's for leveraging existing database infrastructure, meeting enterprise requirements, or utilizing advanced
19+
database features like native table partitioning - we wanted to ensure Background Jobs could work seamlessly with
20+
your preferred database platform.
2121

22-
The new **DatabaseJobFeature** is a new implementation purpose built for PostgreSQL, SQL Server and MySQL backends that's
23-
a drop-in replacement for SQLite's **BackgroundsJobFeature** which can be applied to an existing .NET 8+ project by
24-
[mixing in](https://docs.servicestack.net/mix-tool) the **db-identity** or **db-jobs** gist files to your host project.
22+
## Introducing DatabaseJobsFeature
2523

26-
## Install
24+
The new **DatabaseJobsFeature** is a purpose-built implementation for PostgreSQL, SQL Server, and MySQL that's
25+
a drop-in replacement for SQLite's **BackgroundsJobFeature**. It maintains the same simple API, data models,
26+
and service contracts - making migration from SQLite straightforward while unlocking the power of enterprise RDBMS platforms.
2727

28-
For [ServiceStack ASP.NET Identity Auth](https://servicestack.net/start) Projects:
28+
Best of all, it can be added to an existing .NET 8+ project with a single command using our
29+
[mix tool](https://docs.servicestack.net/mix-tool):
30+
31+
## Quick Start
32+
33+
### For Identity Auth Projects
34+
35+
If you're using [ServiceStack ASP.NET Identity Auth](https://servicestack.net/start) templates, simply run:
2936

3037
:::sh
3138
x mix db-identity
3239
:::
3340

34-
Which replaces `Configure.BackgroundJobs.cs` and `Configure.RequestLogs.cs` with an equivalent
35-
version that uses the new `DatabaseJobFeature` for sending Application Emails and `DbRequestLogger`
36-
for API Request Logging.
41+
This replaces both `Configure.BackgroundJobs.cs` and `Configure.RequestLogs.cs` with RDBMS-compatible versions
42+
that use `DatabaseJobsFeature` for background jobs and `DbRequestLogger` for API request logging.
43+
44+
### For Other .NET 8+ Apps
3745

38-
All other .NET 8+ ServiceStack Apps should instead use:
46+
For all other ServiceStack applications, use:
3947

4048
:::sh
4149
x mix db-jobs
4250
:::
4351

44-
Which replaces `Configure.BackgroundJobs.cs` to use `DatabaseJobFeature`:
52+
This replaces `Configure.BackgroundJobs.cs` to use the new `DatabaseJobsFeature`:
4553

4654
```csharp
4755
public class ConfigureBackgroundJobs : IHostingStartup
4856
{
4957
public void Configure(IWebHostBuilder builder) => builder
5058
.ConfigureServices(services => {
5159
services.AddPlugin(new CommandsFeature());
52-
services.AddPlugin(new DatabaseJobFeature {
53-
// NamedConnection = "<alternative db>"
60+
services.AddPlugin(new DatabaseJobsFeature {
61+
// Optional: Use a separate named connection
62+
// NamedConnection = "jobs"
5463
});
5564
services.AddHostedService<JobsHostedService>();
5665
}).ConfigureAppHost(afterAppHostInit: appHost => {
5766
var services = appHost.GetApplicationServices();
5867
var jobs = services.GetRequiredService<IBackgroundJobs>();
59-
// Example of registering a Recurring Job to run Every Hour
60-
//jobs.RecurringCommand<MyCommand>(Schedule.Hourly);
68+
// Example: Register recurring jobs to run on a schedule
69+
// jobs.RecurringCommand<MyCommand>(Schedule.Hourly);
6170
});
6271
}
6372

64-
public class JobsHostedService(ILogger<JobsHostedService> log, IBackgroundJobs jobs) : BackgroundService
73+
public class JobsHostedService(ILogger<JobsHostedService> log, IBackgroundJobs jobs)
74+
: BackgroundService
6575
{
6676
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
6777
{
6878
await jobs.StartAsync(stoppingToken);
69-
79+
7080
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3));
71-
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
81+
while (!stoppingToken.IsCancellationRequested &&
82+
await timer.WaitForNextTickAsync(stoppingToken))
7283
{
7384
await jobs.TickAsync();
7485
}
7586
}
7687
}
7788
```
7889

79-
Fortunately we were able reuse the same `IBackgroundJobs` interface, Data Models, and API Service Contracts
80-
which greatly simplifies any migration efforts from SQLite's **ServiceStack.Jobs** implementation.
90+
## Seamless Migration from SQLite
91+
92+
We've maintained the same `IBackgroundJobs` interface, data models, and API service contracts, which means:
93+
94+
- **Zero code changes** to your existing job enqueueing logic
95+
- **Same Admin UI** for monitoring and managing jobs
96+
- **Compatible APIs** - all your existing commands and job configurations work as-is
97+
98+
The only change needed is swapping `BackgroundsJobFeature` for `DatabaseJobsFeature` in your configuration!
8199

82-
By implementing the same API Service Contracts (i.e. Request/Response DTOs) we're also able to reuse the same
83-
[built-in](/auto-ui) Management UI to provide real-time monitoring, inspection and management of background jobs:
100+
Watch our video introduction to Background Jobs to see it in action:
84101

85102
:::youtube 2Cza_a_rrjA
86103
Durable C# Background Jobs and Scheduled Tasks for .NET
87104
:::
88105

89-
## RDBMS Optimizations
106+
## Smart RDBMS Optimizations
90107

91-
A key benefit of using SQLite for Background Jobs was the ability to easily maintain completed and failed job history in
92-
separate **monthly databases**. This approach prevented the main application database from growing unbounded by archiving
93-
historical job data into isolated monthly SQLite database files (e.g., `jobs_2025-01.db`, `jobs_2025-02.db`).
94-
These monthly databases could be easily backed up, archived to cold storage, or deleted after a retention period,
95-
providing a simple yet effective data lifecycle management strategy.
108+
One of the key benefits of SQLite Background Jobs was the ability to maintain completed and failed job history in
109+
separate **monthly databases** (e.g., `jobs_2025-01.db`, `jobs_2025-02.db`). This prevented unbounded database growth
110+
and made it easy to archive or delete old job history.
96111

97-
For the new **DatabaseJobFeature** supporting PostgreSQL, SQL Server, and MySQL, we've replicated this monthly
98-
partitioning strategy using **monthly partitioned SQL tables** for the `CompletedJob` and `FailedJob` archive tables.
112+
For `DatabaseJobsFeature`, we've replicated this monthly partitioning strategy using **monthly partitioned tables**
113+
for the `CompletedJob` and `FailedJob` archive tables - but the implementation varies by database platform to leverage
114+
each RDBMS's strengths.
99115

100116
### PostgreSQL - Native Table Partitioning
101117

0 commit comments

Comments
 (0)