Skip to content

Commit fe3afa6

Browse files
committed
Add ability to configure SqliteRequestLogger + BackgroundJobsFeature
1 parent ac121c2 commit fe3afa6

4 files changed

Lines changed: 53 additions & 30 deletions

File tree

ServiceStack.OrmLite/src/ServiceStack.OrmLite/OrmLiteConnectionFactory.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public virtual IDbConnection OpenDbConnection()
116116
public virtual IDbConnection OpenDbConnection(Action<IDbConnection> configure)
117117
{
118118
var connection = CreateDbConnection();
119-
configure(connection);
119+
configure?.Invoke(connection);
120120
connection.Open();
121121
return connection;
122122
}
@@ -136,7 +136,7 @@ public virtual async Task<IDbConnection> OpenDbConnectionAsync(CancellationToken
136136
public virtual async Task<IDbConnection> OpenDbConnectionAsync(Action<IDbConnection> configure, CancellationToken token = default)
137137
{
138138
var connection = CreateDbConnection();
139-
configure(connection);
139+
configure?.Invoke(connection);
140140
if (connection is OrmLiteConnection ormliteConn)
141141
{
142142
await ormliteConn.OpenAsync(token).ConfigAwait();
@@ -163,7 +163,7 @@ public virtual async Task<IDbConnection> OpenDbConnectionAsync(string namedConne
163163
public virtual async Task<IDbConnection> OpenDbConnectionAsync(string namedConnection, Action<IDbConnection> configure, CancellationToken token = default)
164164
{
165165
var connection = CreateDbConnection(namedConnection);
166-
configure(connection);
166+
configure?.Invoke(connection);
167167
if (connection is OrmLiteConnection ormliteConn)
168168
{
169169
await ormliteConn.OpenAsync(token).ConfigAwait();
@@ -193,7 +193,7 @@ public virtual IDbConnection OpenDbConnectionString(string connectionString, Act
193193

194194
var connection = DialectProvider.CreateOrmLiteConnection(this);
195195
connection.ConnectionString = connectionString;
196-
configure(connection);
196+
configure?.Invoke(connection);
197197

198198
connection.Open();
199199
return connection;
@@ -218,7 +218,7 @@ public virtual async Task<IDbConnection> OpenDbConnectionStringAsync(string conn
218218

219219
var connection = DialectProvider.CreateOrmLiteConnection(this);
220220
connection.ConnectionString = connectionString;
221-
configure(connection);
221+
configure?.Invoke(connection);
222222

223223
await connection.OpenAsync(token).ConfigAwait();
224224
return connection;
@@ -292,7 +292,7 @@ public virtual IDbConnection OpenDbConnection(string namedConnection)
292292
public virtual IDbConnection OpenDbConnection(string namedConnection, Action<IDbConnection> configure)
293293
{
294294
var connection = CreateDbConnection(namedConnection);
295-
configure(connection);
295+
configure?.Invoke(connection);
296296
connection.Open();
297297
return connection;
298298
}

ServiceStack/src/ServiceStack.Jobs/BackgroundJobs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public BackgroundJobs(ILogger<BackgroundJobs> log,
3636
this.services = services;
3737
this.scopeFactory = scopeFactory;
3838

39-
var dialect = SqliteDialect.Provider;
39+
var dialect = feature.DialectProvider;
4040
this.Table = dialect.GetTableName(typeof(BackgroundJob));
4141
this.columns = new(
4242
Logs:dialect.GetQuotedColumnName(nameof(BackgroundJob.Logs)),

ServiceStack/src/ServiceStack.Jobs/BackgroundsJobFeature.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ServiceStack.Configuration;
44
using ServiceStack.Data;
55
using ServiceStack.OrmLite;
6+
using ServiceStack.OrmLite.Sqlite;
67

78
namespace ServiceStack.Jobs;
89

@@ -19,8 +20,13 @@ public class BackgroundsJobFeature : IPlugin, Model.IHasStringId, IConfigureServ
1920
public Func<DateTime, string> DbMonthFile { get; set; } = DefaultDbMonthFile;
2021
public Func<IDbConnectionFactory, IDbConnection> ResolveAppDb { get; set; }
2122
public Func<IDbConnectionFactory, DateTime, IDbConnection> ResolveMonthDb { get; set; }
23+
public Action<SqliteOrmLiteDialectProviderBase>? ConfigureDialectProvider { get; set; }
24+
public SqliteOrmLiteDialectProviderBase DialectProvider { get; set; }
25+
public Action<IDbConnection>? ConfigureDb { get; set; }
26+
public Action<IDbConnection>? ConfigureMonthDb { get; set; }
2227
public bool AutoInitSchema { get; set; } = true;
2328
public bool EnableAdmin { get; set; } = true;
29+
public bool UseWriteLocks { get; set; } = true;
2430
public IDbConnectionFactory DbFactory { get; set; } = null!;
2531
public IAppHostNetCore AppHost { get; set; } = null!;
2632
public CommandsFeature CommandsFeature { get; set; } = null!;
@@ -55,24 +61,27 @@ public void Configure(IServiceCollection services)
5561
AutoQueryFeature.RegisterAutoQueryDbIfNotExists();
5662
}
5763
}
58-
64+
5965
public void Register(IAppHost appHost)
6066
{
67+
DialectProvider = SqliteConfiguration.Configure(SqliteDialect.Create());
68+
ConfigureDialectProvider?.Invoke(DialectProvider);
69+
6170
CommandsFeature ??= appHost.GetPlugin<CommandsFeature>()
62-
?? throw new Exception($"{nameof(CommandsFeature)} is required to use {nameof(BackgroundsJobFeature)}");
71+
?? throw new Exception($"{nameof(CommandsFeature)} is required to use {nameof(BackgroundsJobFeature)}");
6372
Jobs ??= appHost.TryResolve<IBackgroundJobs>()
6473
?? throw new Exception($"{nameof(IBackgroundJobs)} is not registered");
6574
DbFactory ??= appHost.TryResolve<IDbConnectionFactory>()
66-
?? new OrmLiteConnectionFactory("Data Source=:memory:", SqliteDialect.Provider);
75+
?? new OrmLiteConnectionFactory("Data Source=:memory:", DialectProvider);
6776

68-
var dateConverter = SqliteDialect.Provider.GetDateTimeConverter();
77+
var dateConverter = DialectProvider.GetDateTimeConverter();
6978
if (dateConverter.DateStyle == DateTimeKind.Unspecified)
7079
dateConverter.DateStyle = DateTimeKind.Utc;
7180

7281
AppHost ??= (IAppHostNetCore)appHost;
7382
var fullDirPath = GetDbDir();
7483

75-
DbFactory.RegisterConnection(DbFile, fullDirPath.AssertDir().CombineWith(DbFile), SqliteDialect.Provider);
84+
DbFactory.RegisterConnection(DbFile, fullDirPath.AssertDir().CombineWith(DbFile), DialectProvider);
7685

7786
// If DbFile has changed, replace the namedConnection lock with Locks.JobsDb
7887
if (DbFile != Workers.JobsDb)
@@ -101,25 +110,28 @@ public void BeforePluginsLoaded(IAppHost appHost)
101110
});
102111
}
103112
}
104-
113+
105114
public static string DefaultDbMonthFile(DateTime createdDate) => $"jobs_{createdDate.Year}-{createdDate.Month:00}.db";
106115

107116
public IDbConnection DefaultResolveAppDb(IDbConnectionFactory dbFactory) =>
108-
dbFactory.OpenDbConnection(DbFile);
117+
((IDbConnectionFactoryExtended)dbFactory).OpenDbConnection(DbFile, ConfigureDb);
109118

110119
public IDbConnection DefaultResolveMonthDb(IDbConnectionFactory dbFactory, DateTime createdDate)
111120
{
121+
var factory = (IDbConnectionFactoryExtended)dbFactory;
112122
var monthDb = DbMonthFile(createdDate);
113-
if (!OrmLiteConnectionFactory.NamedConnections.ContainsKey(monthDb))
123+
lock (this)
114124
{
115-
var dataSource = GetDbDir(monthDb);
116-
117-
dbFactory.RegisterConnection(monthDb, $"DataSource={dataSource};Cache=Shared", SqliteDialect.Provider);
118-
var db = dbFactory.OpenDbConnection(monthDb);
119-
InitMonthDbSchema(db);
120-
return db;
125+
if (!OrmLiteConnectionFactory.NamedConnections.ContainsKey(monthDb))
126+
{
127+
var dataSource = GetDbDir(monthDb);
128+
dbFactory.RegisterConnection(monthDb, $"DataSource={dataSource};Cache=Shared", DialectProvider);
129+
var db = factory.OpenDbConnection(monthDb, ConfigureMonthDb);
130+
InitMonthDbSchema(db);
131+
return db;
132+
}
121133
}
122-
return dbFactory.OpenDbConnection(monthDb);
134+
return factory.OpenDbConnection(monthDb, ConfigureMonthDb);
123135
}
124136

125137
public IDbConnection OpenDb() => ResolveAppDb(DbFactory);

ServiceStack/src/ServiceStack.Jobs/SqliteRequestLogger.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ServiceStack.DataAnnotations;
88
using ServiceStack.Host;
99
using ServiceStack.OrmLite;
10+
using ServiceStack.OrmLite.Sqlite;
1011
using ServiceStack.Text;
1112
using ServiceStack.Web;
1213

@@ -95,6 +96,9 @@ public class SqliteRequestLogger : InMemoryRollingRequestLogger, IRequiresSchema
9596
typeof(AdminQueryRequestLogs),
9697
];
9798

99+
public Action<SqliteOrmLiteDialectProviderBase>? ConfigureDialectProvider { get; set; }
100+
public SqliteOrmLiteDialectProviderBase DialectProvider { get; set; }
101+
public Action<IDbConnection>? ConfigureDb { get; set; }
98102
public Func<IDbConnectionFactory, DateTime, IDbConnection> ResolveMonthDb { get; set; }
99103
public Func<DateTime, string> DbMonthFile { get; set; } = DefaultDbMonthFile;
100104
public Func<List<string>> ResolveMonthDbs { get; set; }
@@ -250,8 +254,11 @@ public void Configure(IServiceCollection services)
250254

251255
public void Register(IAppHost appHost)
252256
{
257+
DialectProvider = SqliteConfiguration.Configure(SqliteDialect.Create());
258+
ConfigureDialectProvider?.Invoke(DialectProvider);
259+
253260
DbFactory ??= appHost.TryResolve<IDbConnectionFactory>()
254-
?? new OrmLiteConnectionFactory("Data Source=:memory:", SqliteDialect.Provider);
261+
?? new OrmLiteConnectionFactory("Data Source=:memory:", DialectProvider);
255262
AppHost ??= (IAppHostNetCore)appHost;
256263
_ = GetDbDir().AssertDir();
257264

@@ -278,16 +285,20 @@ public List<string> DefaultResolveMonthDbs()
278285

279286
public IDbConnection DefaultResolveMonthDb(IDbConnectionFactory dbFactory, DateTime createdDate)
280287
{
288+
var factory = (IDbConnectionFactoryExtended)dbFactory;
281289
var monthDb = DbMonthFile(createdDate);
282-
if (!OrmLiteConnectionFactory.NamedConnections.ContainsKey(monthDb))
290+
lock (this)
283291
{
284-
var dataSource = GetDbDir(monthDb);
285-
dbFactory.RegisterConnection(monthDb, $"DataSource={dataSource};Cache=Shared", SqliteDialect.Provider);
286-
var db = dbFactory.OpenDbConnection(monthDb);
287-
InitMonthDbSchema(db, createdDate);
288-
return db;
292+
if (!OrmLiteConnectionFactory.NamedConnections.ContainsKey(monthDb))
293+
{
294+
var dataSource = GetDbDir(monthDb);
295+
dbFactory.RegisterConnection(monthDb, $"DataSource={dataSource};Cache=Shared", DialectProvider);
296+
var db = factory.OpenDbConnection(monthDb, ConfigureDb);
297+
InitMonthDbSchema(db, createdDate);
298+
return db;
299+
}
289300
}
290-
return dbFactory.OpenDbConnection(monthDb);
301+
return factory.OpenDbConnection(monthDb, ConfigureDb);
291302
}
292303

293304
public IDbConnection OpenMonthDb(DateTime createdDate) => ResolveMonthDb(DbFactory, createdDate);

0 commit comments

Comments
 (0)