feat(jobs): add durable due-time job runner with leases

This commit is contained in:
shira committed 2026-10-06 14:29:23 +03:00
1 parent 7e80c8ad2a
commit 008254e44a
39 files changed
+4909 -43

No files matched your search

+19 -7
View File
@@ -18,6 +18,7 @@ import (
"lincodex/backend/config"
"lincodex/backend/internal/adapter/secondary/prometheus"
"lincodex/backend/internal/port/outbound"
"lincodex/backend/internal/worker/jobrunner"
"lincodex/backend/internal/worker/outboxrelay"
)
@@ -40,6 +41,10 @@ type Application struct {
// consumers apply events from the event bus while the servers run. The
// list is empty until a handler is registered in eventHandlers.
consumers eventConsumers
// jobs runs scheduled jobs when they fall due while the servers run. It
// is nil when the worker is disabled, no job kind is registered, or the
// database is not configured.
jobs *jobrunner.Worker
}
func NewApplication(
@@ -50,6 +55,7 @@ func NewApplication(
mailSender outbound.MailSender,
relay *outboxrelay.Relay,
consumers eventConsumers,
jobs *jobrunner.Worker,
) *Application {
return &Application{
config: config,
@@ -59,6 +65,7 @@ func NewApplication(
mailSender: mailSender,
relay: relay,
consumers: consumers,
jobs: jobs,
}
}
@@ -174,18 +181,20 @@ func (application *Application) run(ctx context.Context) error {
)
application.logger.Info("servers stopped")
stopRun()
// The relay finishes its pass in progress within SHUTDOWN_TIMEOUT and
// each consumer its record in progress within CONSUMER_SHUTDOWN_GRACE
// plus one offset commit, so the cleanup that closes their pools,
// producer, and group members runs after all of them stopped.
// The relay finishes its pass in progress within SHUTDOWN_TIMEOUT, each
// consumer its record in progress within CONSUMER_SHUTDOWN_GRACE plus
// one offset commit, and the job worker its job in progress within
// JOBS_SHUTDOWN_GRACE plus one release, so the cleanup that closes their
// pools, producer, and group members runs after all of them stopped.
<-workersStopped
application.logger.Info("workers stopped")
return err
}
// startWorkers runs the relay and every consumer until ctx ends and returns
// a channel that closes when all of them have stopped. The workers stop
// together with the servers; none of them depends on another.
// startWorkers runs the relay, every consumer, and the scheduled job worker
// until ctx ends and returns a channel that closes when all of them have
// stopped. The workers stop together with the servers; none of them
// depends on another.
func (application *Application) startWorkers(ctx context.Context) <-chan struct{} {
var workers sync.WaitGroup
if application.relay != nil {
@@ -194,6 +203,9 @@ func (application *Application) startWorkers(ctx context.Context) <-chan struct{
for _, consumer := range application.consumers {
workers.Go(func() { consumer.Run(ctx) })
}
if application.jobs != nil {
workers.Go(func() { application.jobs.Run(ctx) })
}
stopped := make(chan struct{})
go func() {
defer close(stopped)