@@ -96,10 +96,13 @@ func createPromiseAndTask(
96
96
return nil , err
97
97
}
98
98
var promiseRowsAffected int64
99
- if taskCmd == nil {
99
+
100
+ // CreatePromise could be merged with a task command in `createPromise`
101
+ if completion .Store .Results [0 ].Kind == t_aio .CreatePromise {
100
102
promiseRowsAffected = completion .Store .Results [0 ].CreatePromise .RowsAffected
101
103
} else {
102
104
promiseRowsAffected = completion .Store .Results [0 ].CreatePromiseAndTask .PromiseRowsAffected
105
+ util .Assert (promiseRowsAffected == completion .Store .Results [0 ].CreatePromiseAndTask .TaskRowsAffected , "number of promises and tasks affected must be equal." )
103
106
}
104
107
105
108
if promiseRowsAffected == 0 {
@@ -212,27 +215,9 @@ func createPromise(tags map[string]string, promiseCmd *t_aio.CreatePromiseComman
212
215
promiseCmd .Tags = map [string ]string {}
213
216
}
214
217
215
- isCreatePromiseAndTask := taskCmd != nil
216
-
217
218
return func (c gocoro.Coroutine [* t_aio.Submission , * t_aio.Completion , * t_aio.Completion ]) (* t_aio.Completion , error ) {
218
219
commands := []* t_aio.Command {}
219
220
220
- // Combine both commands if taskCmd is not null otherwise add just the CreatePromiseCmd
221
- if isCreatePromiseAndTask {
222
- commands = append (commands , & t_aio.Command {
223
- Kind : t_aio .CreatePromiseAndTask ,
224
- CreatePromiseAndTask : & t_aio.CreatePromiseAndTaskCommand {
225
- PromiseCommand : promiseCmd ,
226
- TaskCommand : taskCmd ,
227
- },
228
- })
229
- } else {
230
- commands = append (commands , & t_aio.Command {
231
- Kind : t_aio .CreatePromise ,
232
- CreatePromise : promiseCmd ,
233
- })
234
- }
235
-
236
221
// check router to see if a task needs to be created
237
222
completion , err := gocoro .YieldAndAwait (c , & t_aio.Submission {
238
223
Kind : t_aio .Router ,
@@ -254,16 +239,21 @@ func createPromise(tags map[string]string, promiseCmd *t_aio.CreatePromiseComman
254
239
slog .Warn ("failed to match promise" , "cmd" , promiseCmd , "err" , err )
255
240
}
256
241
257
- if isCreatePromiseAndTask && (err != nil || ! completion .Router .Matched ) {
242
+ if taskCmd != nil && (err != nil || ! completion .Router .Matched ) {
258
243
slog .Error ("failed to match promise with router when creating a task" , "cmd" , promiseCmd )
259
244
return nil , t_api .NewError (t_api .StatusPromiseRecvNotFound , err )
260
245
}
261
246
247
+ cmd := & t_aio.Command {
248
+ Kind : t_aio .CreatePromise ,
249
+ CreatePromise : promiseCmd ,
250
+ }
251
+
262
252
if err == nil && completion .Router .Matched {
263
253
util .Assert (completion .Router .Recv != nil , "recv must not be nil" )
264
254
265
255
// If there is a taskCmd just update the Recv otherwise create a tasks for the match
266
- if isCreatePromiseAndTask {
256
+ if taskCmd != nil {
267
257
// Note: we are mutating the taskCmd that is already merged with the createPromiseCmd
268
258
taskCmd .Recv = completion .Router .Recv
269
259
} else {
@@ -275,17 +265,19 @@ func createPromise(tags map[string]string, promiseCmd *t_aio.CreatePromiseComman
275
265
State : task .Init ,
276
266
CreatedOn : promiseCmd .CreatedOn ,
277
267
}
278
-
279
- // add create task command if matched
280
- commands = append (commands , & t_aio.Command {
281
- Kind : t_aio .CreateTask ,
282
- CreateTask : taskCmd ,
283
- })
284
-
285
268
}
286
269
270
+ cmd = & t_aio.Command {
271
+ Kind : t_aio .CreatePromiseAndTask ,
272
+ CreatePromiseAndTask : & t_aio.CreatePromiseAndTaskCommand {
273
+ PromiseCommand : promiseCmd ,
274
+ TaskCommand : taskCmd ,
275
+ },
276
+ }
287
277
}
288
278
279
+ // Add the main command
280
+ commands = append (commands , cmd )
289
281
// add additional commands
290
282
commands = append (commands , additionalCmds ... )
291
283
@@ -307,15 +299,15 @@ func createPromise(tags map[string]string, promiseCmd *t_aio.CreatePromiseComman
307
299
308
300
util .Assert (completion .Store != nil , "completion must not be nil" )
309
301
util .Assert (len (completion .Store .Results ) == len (commands ), "completion must have same number of results as commands" )
310
- if isCreatePromiseAndTask {
302
+ if completion . Store . Results [ 0 ]. Kind == t_aio . CreatePromiseAndTask {
311
303
promiseAndTaskResult := completion .Store .Results [0 ].CreatePromiseAndTask
312
304
util .Assert (promiseAndTaskResult .PromiseRowsAffected == 0 || promiseAndTaskResult .PromiseRowsAffected == 1 , "Creating promise result must return 0 or 1 rows" )
313
- if promiseAndTaskResult .PromiseRowsAffected == 0 {
314
- util .Assert (promiseAndTaskResult .TaskRowsAffected == 0 , "If not promise was created a task must have not been created" )
315
- }
316
- } else {
305
+ util .Assert (promiseAndTaskResult .TaskRowsAffected == promiseAndTaskResult .PromiseRowsAffected , "If not promise was created a task must have not been created" )
306
+ } else if completion .Store .Results [0 ].Kind == t_aio .CreatePromise {
317
307
createPromiseResult := completion .Store .Results [0 ].CreatePromise
318
308
util .Assert (createPromiseResult .RowsAffected == 0 || createPromiseResult .RowsAffected == 1 , "CreatePromise result must return 0 or 1 rows" )
309
+ } else {
310
+ panic ("First result must be CreatePromise or CreatePromiseAndTask" )
319
311
}
320
312
321
313
return completion , nil
0 commit comments