Skip to content

Commit

Permalink
Make database repo available in background jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
bobf committed Nov 27, 2024
1 parent b4b7251 commit 1fa6db7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
9 changes: 6 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
.hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af",
},
.jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/147e20907e10f2ae8a373ab202e63cf7ddcec1f3.tar.gz",
.hash = "1220ce19ff91a1ac7f83fae3b7f13d670ba528df3ccdee1931cc475f7d3975dcca66",
.url = "https://github.com/jetzig-framework/jetquery/archive/f48133462e054febe540c8c2864ab2704368d2fa.tar.gz",
.hash = "1220877e86bb70c982ba296d2989cd87e4d95437651f5c4bb20de485aa55e4be3499",
},
.jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/86f24cfdf2aaa0e8ada4539a6edef882708ced2b.tar.gz",
Expand All @@ -26,7 +26,10 @@
.url = "https://github.com/ikskuh/zig-args/archive/0abdd6947a70e6d8cc83b66228cea614aa856206.tar.gz",
.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 = .{
.url = "https://github.com/karlseguin/smtp_client.zig/archive/3cbe8f269e4c3a6bce407e7ae48b2c76307c559f.tar.gz",
.hash = "1220de146446d0cae4396e346cb8283dd5e086491f8577ddbd5e03ad0928111d8bc6",
Expand Down
1 change: 1 addition & 0 deletions src/jetzig/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub fn start(self: *const App, routes_module: type, options: AppOptions) !void {
.mailers = &routes_module.mailers,
.store = &store,
.cache = &cache,
.repo = &repo,
.mutex = &mutex,
},
);
Expand Down
1 change: 1 addition & 0 deletions src/jetzig/http/Request.zig
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ const RequestMail = struct {
.store = self.request.server.store,
.cache = self.request.server.cache,
.mutex = undefined,
.repo = self.request.repo,
},
),
}
Expand Down
11 changes: 8 additions & 3 deletions src/jetzig/http/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ const Dispatcher = struct {
pub fn listen(self: *Server) !void {
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(
self.allocator,
.{
.port = self.env.port,
.address = self.env.bind,
.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"),
},
.workers = .{
.count = jetzig.config.get(u16, "worker_count"),
.count = worker_count,
.max_conn = jetzig.config.get(u16, "max_connections"),
.retain_allocated_bytes = jetzig.config.get(usize, "arena_size"),
},
Expand All @@ -95,10 +98,12 @@ pub fn listen(self: *Server) !void {
);
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.port,
@tagName(self.env.environment),
worker_count,
thread_count,
});

self.initialized = true;
Expand Down
2 changes: 2 additions & 0 deletions src/jetzig/jobs/Job.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub const JobEnv = struct {
store: *jetzig.kv.Store,
/// Global cache
cache: *jetzig.kv.Store,
/// Database repo
repo: *jetzig.database.Repo,
/// Global mutex - use with caution if it is necessary to guarantee thread safety/consistency
/// between concurrent job workers
mutex: *std.Thread.Mutex,
Expand Down

0 comments on commit 1fa6db7

Please sign in to comment.