ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.

Home Page:https://www.ardanlabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] what if worker needs to report error?

mrkagelui opened this issue · comments

first of all, let me confirm my understanding: am I right to say that foundation/worker package is supposed to provide a http-like pattern to handle things like mq or kafka messages?

it looks like its JobFunc

// JobFunc defines a function that can execute work for a specific job.
type JobFunc func(ctx context.Context)
doesn't allow error handling, like the net/http.HandlerFunc which you changed.

is there a reason for it not to include it? what if the handler wants to signal some catastrophic failure and the app should shutdown?

It's a package that is not used by service, but I wrote to solve a jobs problem where I needed cancelation so I added it to the project. Think of a job function like any Goroutine where there is no one to send an error back to. A Job function is a parent function. Now, if you want to get elaborate and add the concept of middleware, it could be interesting but I don't think it is necessary.

sorry what do you mean by "parent function"?

When it comes to goroutines you define a function that is called with the keyword go. I can considering this the parent function because there is no other function before it.

go func() {
    // I am in the parent function for the path of execution.
}()

interesting, in my mental model it's the "child function" as its spawned by the outer function 😆

is there a plan to make an example mq worker?

Once you launch this as a G, we could consider this a child of the G that spawned it. I do that as well when it comes to concurrency management. In the end, it's the parent function for this whole new path of execution. In the end, find a mental model that works for you.

There is no plan. This starter-kit is already bigger than I wished it was, but I feel like it is feature complete.

I see. ok. I'm using this package in my queue worker, hence the question. don't think I'd add a whole middleware thing, perhaps just some channel for it to signal shutdown

The package has all that already so don't rewrite that. You use the Shutdown method. The biggest decision is what to set the maxRunningJobs value to be. This restricts the number of G's that will be created in parallel.

yeah of course. I'll probably collect the errors in a channel and once I receive it, start that Shutdown