@@ -33,6 +33,7 @@ type TemporalClient struct {
3333 mu sync.Mutex
3434 readyCh chan struct {}
3535 closeReadyOnce sync.Once
36+ workersStarted bool
3637}
3738
3839func NewTemporalClient (
@@ -131,7 +132,8 @@ func (a *TemporalClient) Initialize() error {
131132 }
132133 }
133134
134- // Create and start a worker for each identity (task queue)
135+ // Create a worker for each identity (task queue).
136+ // Registration must happen before workers are started.
135137 a .mu .Lock ()
136138 defer a .mu .Unlock ()
137139
@@ -147,32 +149,70 @@ func (a *TemporalClient) Initialize() error {
147149 workerOptions ,
148150 )
149151
152+ a .workers [identity ] = newWorker
153+ }
154+
155+ if len (a .workers ) == 0 {
156+ a .markReady () // Unblock any waiters even on failure
157+ return fmt .Errorf ("failed to create any Temporal workers" )
158+ }
159+
160+ return nil
161+ }
162+
163+ // StartWorkers starts all registered Temporal workers.
164+ // This must be called only after workflow/activity registration is complete.
165+ func (c * TemporalClient ) StartWorkers () error {
166+ c .mu .Lock ()
167+ defer c .mu .Unlock ()
168+
169+ if c .client == nil {
170+ return fmt .Errorf ("temporal client is not initialized" )
171+ }
172+
173+ if len (c .workers ) == 0 {
174+ c .markReady ()
175+ return fmt .Errorf ("no Temporal workers configured" )
176+ }
177+
178+ if c .workersStarted {
179+ logrus .Warn ("Temporal workers already started, skipping worker startup" )
180+ return nil
181+ }
182+
183+ buildID := common .GetBuildIdentifier ()
184+ startedCount := 0
185+
186+ for identity , w := range c .workers {
150187 logrus .WithFields (logrus.Fields {
151188 "BuildID" : buildID ,
152189 "taskQueue" : identity ,
153- }).Infof ("Starting Temporal worker" )
190+ }).Info ("Starting Temporal worker" )
154191
155- if err := newWorker .Start (); err != nil {
192+ if err := w .Start (); err != nil {
156193 logrus .WithError (err ).
157194 WithField ("taskQueue" , identity ).
158195 Error ("Failed to start temporal worker" )
196+ delete (c .workers , identity )
159197 continue
160198 }
161199
162- a . workers [ identity ] = newWorker
200+ startedCount ++
163201 }
164202
165- if len ( a . workers ) == 0 {
166- a .markReady () // Unblock any waiters even on failure
203+ if startedCount == 0 {
204+ c .markReady ()
167205 return fmt .Errorf ("failed to start any Temporal workers" )
168206 }
169207
208+ c .workersStarted = true
209+
170210 // If versioning is enabled, confirm our deployment version is registered
171211 // on the Temporal server before allowing workflow submissions via GetClient().
172- if a .config .DisableVersioning {
173- a .markReady ()
212+ if c .config .DisableVersioning {
213+ c .markReady ()
174214 } else {
175- go a .awaitVersionRegistration (buildID )
215+ go c .awaitVersionRegistration (buildID )
176216 }
177217
178218 return nil
@@ -335,6 +375,7 @@ func (c *TemporalClient) Shutdown() error {
335375
336376 c .workers = nil
337377 c .client = nil
378+ c .workersStarted = false
338379
339380 return nil
340381}
0 commit comments