Creating a client after creating a worker

In the Go SDK, a client must be created before the worker can be created. This has the perhaps unintended side effect of forcing the client and worker to be created in the same function in order to defer closing the client. Is there any logical reason why the step of creating the worker, and then setting its client couldn’t be teased apart? Another way to ask the question is, does a Worker actually use the Client between being created and being run? Here’s something like what I’d like to be able to do:

var w worker.Worker

func initApp(config map[string]interface{}) (err error) {
    w, err = worker.New(nil, "queuename",  worker.Options{})
    ...
    w.RegisterWorkflow(...)
    w.RegisterActivity(...)
    w.RegisterActivity(...)
    return err
}

func startServer() (err error) {
    c, err := client.NewClient(client.Options{
       HostPort: client.DefaultHostPort,
    })
    ...
    defer c.Close()
    w.SetClient(c)
    err = w.Run(worker.InterruptCh())
    ...

    return err
}

As we don’t encourage global variables this pattern is not that we plan to optimize for.

You can easily implement your own wrapper of the Worker that supports your API idea. The wrapper caches the registrations. When yourwrapper.SetClient is called the internal worker.Worker is created and worker.register... calls are done using the cached values.

Thanks, that’s a clever idea. I’ll do that.