feat(practice): queue practice runs with one running and ten waiting
This commit is contained in:
1 parent
51770eea6c
commit
754c745fba
40 files changed
+3040
-70
No files matched your search
@@ -22,6 +22,7 @@ import (
|
||||
"lincodex/backend/internal/worker/jobrunner"
|
||||
"lincodex/backend/internal/worker/mailsender"
|
||||
"lincodex/backend/internal/worker/outboxrelay"
|
||||
"lincodex/backend/internal/worker/practicequeue"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -37,9 +38,9 @@ type Application struct {
|
||||
// mailSender is the transport selected by EMAIL_PROVIDER. Only the mail
|
||||
// sending worker calls it.
|
||||
mailSender outbound.MailSender
|
||||
// codeRunner is the runner selected by CODE_RUNNER_URL: the disabled one
|
||||
// when unset. Practice usecases receive it from here once they exist.
|
||||
codeRunner outbound.CodeRunner
|
||||
// practiceQueue runs practice jobs on the code runner while the servers
|
||||
// run. It is nil without a configured code runner or database.
|
||||
practiceQueue *practicequeue.Worker
|
||||
// relay publishes outbox events while the servers run. It is nil when
|
||||
// the relay is disabled or the database is not configured.
|
||||
relay *outboxrelay.Relay
|
||||
@@ -156,10 +157,18 @@ func start(ctx context.Context, config config.Config, logger *slog.Logger) (*App
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - initialize application: %w", err)
|
||||
}
|
||||
// No dependency needs the runner yet, so it is attached here rather than
|
||||
// injected; the practice usecases will take it from the injector.
|
||||
application.codeRunner = codeRunner
|
||||
return application, cleanup, nil
|
||||
// The worker is the only consumer of the runner, so it is built here
|
||||
// with the runner rather than through the injector.
|
||||
practiceQueue, closeQueue, err := newPracticeQueueWorker(ctx, config, logger, codeRunner)
|
||||
if err != nil {
|
||||
cleanup()
|
||||
return nil, nil, fmt.Errorf("bootstrap.start - create practice queue worker: %w", err)
|
||||
}
|
||||
application.practiceQueue = practiceQueue
|
||||
return application, func() {
|
||||
closeQueue()
|
||||
cleanup()
|
||||
}, nil
|
||||
}
|
||||
|
||||
func logLevel(value string) logkit.Level {
|
||||
@@ -223,7 +232,8 @@ func (application *Application) run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// startWorkers runs the relay, every consumer, the scheduled job worker, the
|
||||
// mail sending worker, and the delivery metrics sampler until ctx ends and returns a channel that
|
||||
// mail sending worker, the delivery metrics sampler, and the practice queue
|
||||
// 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{} {
|
||||
@@ -246,6 +256,9 @@ func (application *Application) startWorkers(ctx context.Context) <-chan struct{
|
||||
if application.deliveryMetrics != nil {
|
||||
workers.Go(func() { application.deliveryMetrics.Run(ctx) })
|
||||
}
|
||||
if application.practiceQueue != nil {
|
||||
workers.Go(func() { application.practiceQueue.Run(ctx) })
|
||||
}
|
||||
stopped := make(chan struct{})
|
||||
go func() {
|
||||
defer close(stopped)
|
||||
|
||||
Reference in new issue
Block a user