@@ -68,23 +68,51 @@ func (ptc *GetTaskController) GetTasks(ctx *gin.Context) {
6868
6969 session := ptc .proverMgr .Get (publicKey )
7070
71- // if the priority upsteam is set, we try this upstream first until get the task resp or no task resp
72- priorityUpstream , exist := ptc .priorityUpstream [publicKey ]
73- if exist {
74- cli := ptc .clients [priorityUpstream ]
75- resp , err := session .GetTask (ctx , & getTaskParameter , cli , priorityUpstream )
71+ getTask := func (upStream string , cli Client ) (tryNext bool ) {
72+ resp , err := session .GetTask (ctx , & getTaskParameter , cli , upStream )
7673 if err != nil {
7774 types .RenderFailure (ctx , types .ErrCoordinatorGetTaskFailure , err )
7875 return
7976 } else if resp .ErrCode != types .ErrCoordinatorEmptyProofData {
80- // simply dispatch the error from upstream to prover
81- types .RenderFailure (ctx , resp .ErrCode , fmt .Errorf ("%s" , resp .ErrMsg ))
77+
78+ if resp .ErrCode != 0 {
79+ // simply dispatch the error from upstream to prover
80+ types .RenderFailure (ctx , resp .ErrCode , fmt .Errorf ("%s" , resp .ErrMsg ))
81+ return
82+ }
83+
84+ var task coordinatorType.GetTaskSchema
85+ if err = resp .DecodeData (& task ); err == nil {
86+ task .TaskID = formUpstreamWithTaskName (upStream , task .TaskID )
87+ // TODO: log the new id in debug level
88+ types .RenderSuccess (ctx , & task )
89+ } else {
90+ types .RenderFailure (ctx , types .InternalServerError , fmt .Errorf ("decode task fail: %v" , err ))
91+ }
92+
93+ return
94+ }
95+ tryNext = true
96+ return
97+ }
98+
99+ // if the priority upsteam is set, we try this upstream first until get the task resp or no task resp
100+ priorityUpstream , exist := ptc .priorityUpstream [publicKey ]
101+ if exist {
102+ cli := ptc .clients [priorityUpstream ]
103+ if cli != nil && ! getTask (priorityUpstream , cli ) {
82104 return
105+ } else if cli == nil {
106+ // TODO: log error
83107 }
84108 }
85109
86110 for n , cli := range ptc .clients {
87- // return the first task we can get
88- // TODO: use random array for all clients
111+ if ! getTask (n , cli ) {
112+ return
113+ }
89114 }
115+
116+ // if all get task failed, throw empty proof resp
117+ types .RenderFailure (ctx , types .ErrCoordinatorEmptyProofData , fmt .Errorf ("get empty prover task" ))
90118}
0 commit comments