33using ServiceStack . Configuration ;
44using ServiceStack . Data ;
55using ServiceStack . OrmLite ;
6+ using ServiceStack . OrmLite . Sqlite ;
67
78namespace 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 ) ;
0 commit comments