From 67ceaf69902efc33d1f8b01865158ca96b3b9525 Mon Sep 17 00:00:00 2001 From: Adam Steel Date: Mon, 22 Jan 2024 08:12:56 -0700 Subject: [PATCH] Improve inline testing example **Problem** Previous example depended on the manager having been started via `Run` or `RunWithContext`. This kicks off goroutines that result in data races and flaky builds. **Solution** Manually add the `mgr.Pool` in inline test example to avoid requiring a call to `mgr.Run*` and the resulting data races. --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index aa3109a..ac4ec31 100644 --- a/README.md +++ b/README.md @@ -156,11 +156,28 @@ func Push(mgr worker.Manager, job *faktory.Job) error { } func syntheticPush(mgr worker.Manager, job *faktory.Job) error { - if mgr.IsRegistered(job.Type) { - return mgr.Dispatch(job) + mgr.mut.Lock() + defer mgr.mut.Unlock() + + if !mgr.IsRegistered(job.Type) { + return fmt.Errorf("failed to execute job type %s inline, job not registered", job.Type) + } + + // Manually defining the Pool avoids using mgr.Run/mgr.RunWithContext, which can trigger data races + if mgr.Pool == nil { + pool, err := faktory.NewPool(2) + if err != nil { + return fmt.Errorf("couldn't create Faktory connection pool in synthetic push: %w", err) + } + mgr.Pool = pool } - return fmt.Errorf("inline job execution failed, unregistered job type %s", job.Type) + err := mgr.Dispatch(job) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("job was immediately executed but failed. Job type %s, with args %+v", job.Type, job.Args)) + } + + return nil } func realPush(job *faktory.Job) error {