From 4b3bb808167b206145fd129e9583f4e4300dc698 Mon Sep 17 00:00:00 2001 From: Adam Steel Date: Fri, 12 Jan 2024 10:07:10 -0700 Subject: [PATCH 1/3] Support explicit, immediate job execution for tests --- manager.go | 14 ++++++++++++++ runner.go | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/manager.go b/manager.go index 7ba1a20..ff74b66 100644 --- a/manager.go +++ b/manager.go @@ -48,6 +48,20 @@ func (mgr *Manager) Register(name string, fn Perform) { } } +// IsRegistered checks if a given job name is registered with the manager. +func (mgr *Manager) IsRegistered(name string) bool { + _, ok := mgr.jobHandlers[name] + + return ok +} + +// Dispatch immediately executes a job, including all middleware on the manager. +func (mgr *Manager) Dispatch(job *faktory.Job) error { + perform := mgr.jobHandlers[job.Type] + + return dispatch(mgr.middleware, jobContext(mgr.Pool, job), job, perform) +} + // Register a callback to be fired when a process lifecycle event occurs. // These are useful for hooking into process startup or shutdown. func (mgr *Manager) On(event lifecycleEventType, fn LifecycleEventHandler) { diff --git a/runner.go b/runner.go index 458c9de..73eea1d 100644 --- a/runner.go +++ b/runner.go @@ -157,7 +157,7 @@ func processOne(mgr *Manager) error { return je } - joberr := dispatch(mgr.middleware, jobContext(mgr.Pool, job), job, perform) + joberr := mgr.Dispatch(job) if joberr != nil { // job errors are normal and expected, we don't return early from them mgr.Logger.Errorf("Error running %s job %s: %v", job.Type, job.Jid, joberr) From bb550cb9e3ed77543fbb316b5e9d48f34290f2b1 Mon Sep 17 00:00:00 2001 From: Adam Steel Date: Fri, 12 Jan 2024 10:29:02 -0700 Subject: [PATCH 2/3] Add readme example --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 524d83f..93ed795 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,46 @@ func main() { See `test/main.go` for a working example. +# Testing + +`faktory_worker_go` provides helpers that allow you to configure tests to execute jobs inline if you prefer. In this example, the application has defined its own wrapper function for `client.Push`. + +```go +import ( + faktory "github.com/contribsys/faktory/client" + worker "github.com/contribsys/faktory_worker_go" +) + +func Push(mgr worker.Manager, job *faktory.Job) error { + if viper.GetBool("faktory_inline") { + return syntheticPush(mgr worker.Manager, job) + } + return realPush(job) +} + +func syntheticPush(mgr worker.Manager, job *faktory.Job) error { + if mgr.IsRegistered(job.Type) { + mgr.Dispath(job) + } + + return fmt.Errorf("inline job execution failed, unregistered job type %s", job.Type) +} + +func realPush(job *faktory.Job) error { + client, err := faktory.Open() + if err != nil { + return errors.Wrap(err, "failed to open Faktory client connection") + } + + err = client.Push(job) + if err != nil { + return errors.Wrap(err, "failed to enqueue Faktory job") + } + + return nil +} +``` + # FAQ * How do I specify the Faktory server location? From 4b06efdb8bfd0fa544cce38641d061ac34459c5f Mon Sep 17 00:00:00 2001 From: Adam Steel Date: Fri, 12 Jan 2024 10:48:10 -0700 Subject: [PATCH 3/3] fix readme typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 93ed795..04f3eb6 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ func Push(mgr worker.Manager, job *faktory.Job) error { func syntheticPush(mgr worker.Manager, job *faktory.Job) error { if mgr.IsRegistered(job.Type) { - mgr.Dispath(job) + return mgr.Dispatch(job) } return fmt.Errorf("inline job execution failed, unregistered job type %s", job.Type)