Merge pull request #124 from jetzig-framework/database-in-jobs

Make database repo available in background jobs
This commit is contained in:
bobf 2024-11-27 20:52:08 +00:00 committed by GitHub
commit a3e3cb6dfe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 6 deletions

View File

@ -15,8 +15,8 @@
.hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af", .hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af",
}, },
.jetquery = .{ .jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/147e20907e10f2ae8a373ab202e63cf7ddcec1f3.tar.gz", .url = "https://github.com/jetzig-framework/jetquery/archive/f48133462e054febe540c8c2864ab2704368d2fa.tar.gz",
.hash = "1220ce19ff91a1ac7f83fae3b7f13d670ba528df3ccdee1931cc475f7d3975dcca66", .hash = "1220877e86bb70c982ba296d2989cd87e4d95437651f5c4bb20de485aa55e4be3499",
}, },
.jetcommon = .{ .jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/86f24cfdf2aaa0e8ada4539a6edef882708ced2b.tar.gz", .url = "https://github.com/jetzig-framework/jetcommon/archive/86f24cfdf2aaa0e8ada4539a6edef882708ced2b.tar.gz",
@ -26,7 +26,10 @@
.url = "https://github.com/ikskuh/zig-args/archive/0abdd6947a70e6d8cc83b66228cea614aa856206.tar.gz", .url = "https://github.com/ikskuh/zig-args/archive/0abdd6947a70e6d8cc83b66228cea614aa856206.tar.gz",
.hash = "1220411a8c46d95bbf3b6e2059854bcb3c5159d428814099df5294232b9980517e9c", .hash = "1220411a8c46d95bbf3b6e2059854bcb3c5159d428814099df5294232b9980517e9c",
}, },
.pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/f376f4b30c63f1fdf90bc3afe246d3bc4175cd46.tar.gz", .hash = "12200a55304988e942015b6244570b2dc0e87e5764719c9e7d5c812cd7ad34f6b138" }, .pg = .{
.url = "https://github.com/karlseguin/pg.zig/archive/f376f4b30c63f1fdf90bc3afe246d3bc4175cd46.tar.gz",
.hash = "12200a55304988e942015b6244570b2dc0e87e5764719c9e7d5c812cd7ad34f6b138",
},
.smtp_client = .{ .smtp_client = .{
.url = "https://github.com/karlseguin/smtp_client.zig/archive/3cbe8f269e4c3a6bce407e7ae48b2c76307c559f.tar.gz", .url = "https://github.com/karlseguin/smtp_client.zig/archive/3cbe8f269e4c3a6bce407e7ae48b2c76307c559f.tar.gz",
.hash = "1220de146446d0cae4396e346cb8283dd5e086491f8577ddbd5e03ad0928111d8bc6", .hash = "1220de146446d0cae4396e346cb8283dd5e086491f8577ddbd5e03ad0928111d8bc6",

View File

@ -120,6 +120,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
.mailers = &routes_module.mailers, .mailers = &routes_module.mailers,
.store = &store, .store = &store,
.cache = &cache, .cache = &cache,
.repo = &repo,
.mutex = &mutex, .mutex = &mutex,
}, },
); );

View File

@ -610,6 +610,7 @@ const RequestMail = struct {
.store = self.request.server.store, .store = self.request.server.store,
.cache = self.request.server.cache, .cache = self.request.server.cache,
.mutex = undefined, .mutex = undefined,
.repo = self.request.repo,
}, },
), ),
} }

View File

@ -73,17 +73,20 @@ const Dispatcher = struct {
pub fn listen(self: *Server) !void { pub fn listen(self: *Server) !void {
try self.decodeStaticParams(); try self.decodeStaticParams();
const worker_count = jetzig.config.get(u16, "worker_count");
const thread_count: u16 = jetzig.config.get(?u16, "thread_count") orelse @intCast(try std.Thread.getCpuCount());
var httpz_server = try httpz.Server(Dispatcher).init( var httpz_server = try httpz.Server(Dispatcher).init(
self.allocator, self.allocator,
.{ .{
.port = self.env.port, .port = self.env.port,
.address = self.env.bind, .address = self.env.bind,
.thread_pool = .{ .thread_pool = .{
.count = jetzig.config.get(?u16, "thread_count") orelse @intCast(try std.Thread.getCpuCount()), .count = thread_count,
.buffer_size = jetzig.config.get(usize, "buffer_size"), .buffer_size = jetzig.config.get(usize, "buffer_size"),
}, },
.workers = .{ .workers = .{
.count = jetzig.config.get(u16, "worker_count"), .count = worker_count,
.max_conn = jetzig.config.get(u16, "max_connections"), .max_conn = jetzig.config.get(u16, "max_connections"),
.retain_allocated_bytes = jetzig.config.get(usize, "arena_size"), .retain_allocated_bytes = jetzig.config.get(usize, "arena_size"),
}, },
@ -95,10 +98,12 @@ pub fn listen(self: *Server) !void {
); );
defer httpz_server.deinit(); defer httpz_server.deinit();
try self.logger.INFO("Listening on http://{s}:{} [{s}]", .{ try self.logger.INFO("Listening on http://{s}:{d} [{s}] [workers:{d} threads:{d}]", .{
self.env.bind, self.env.bind,
self.env.port, self.env.port,
@tagName(self.env.environment), @tagName(self.env.environment),
worker_count,
thread_count,
}); });
self.initialized = true; self.initialized = true;

View File

@ -25,6 +25,8 @@ pub const JobEnv = struct {
store: *jetzig.kv.Store, store: *jetzig.kv.Store,
/// Global cache /// Global cache
cache: *jetzig.kv.Store, cache: *jetzig.kv.Store,
/// Database repo
repo: *jetzig.database.Repo,
/// Global mutex - use with caution if it is necessary to guarantee thread safety/consistency /// Global mutex - use with caution if it is necessary to guarantee thread safety/consistency
/// between concurrent job workers /// between concurrent job workers
mutex: *std.Thread.Mutex, mutex: *std.Thread.Mutex,