MCPcopy
hub / github.com/QuantumNous/new-api / updateSunoTasks

Function updateSunoTasks

service/task_polling.go:206–301  ·  view source on GitHub ↗
(ctx context.Context, channelId int, taskIds []string, taskM map[string]*model.Task)

Source from the content-addressed store, hash-verified

204}
205
206func updateSunoTasks(ctx context.Context, channelId int, taskIds []string, taskM map[string]*model.Task) error {
207 logger.LogInfo(ctx, fmt.Sprintf("渠道 #%d 未完成的任务有: %d", channelId, len(taskIds)))
208 if ctx.Err() != nil {
209 return ctx.Err()
210 }
211 if len(taskIds) == 0 {
212 return nil
213 }
214 ch, err := model.CacheGetChannel(channelId)
215 if err != nil {
216 common.SysLog(fmt.Sprintf("CacheGetChannel: %v", err))
217 // Collect DB primary key IDs for bulk update (taskIds are upstream IDs, not task_id column values)
218 var failedIDs []int64
219 for _, upstreamID := range taskIds {
220 if t, ok := taskM[upstreamID]; ok {
221 failedIDs = append(failedIDs, t.ID)
222 }
223 }
224 err = model.TaskBulkUpdateByID(failedIDs, map[string]any{
225 "fail_reason": fmt.Sprintf("获取渠道信息失败,请联系管理员,渠道ID:%d", channelId),
226 "status": "FAILURE",
227 "progress": "100%",
228 })
229 if err != nil {
230 common.SysLog(fmt.Sprintf("UpdateSunoTask error: %v", err))
231 }
232 return err
233 }
234 adaptor := GetTaskAdaptorFunc(constant.TaskPlatformSuno)
235 if adaptor == nil {
236 return errors.New("adaptor not found")
237 }
238 proxy := ch.GetSetting().Proxy
239 resp, err := adaptor.FetchTask(*ch.BaseURL, ch.Key, map[string]any{
240 "ids": taskIds,
241 }, proxy)
242 if err != nil {
243 common.SysLog(fmt.Sprintf("Get Task Do req error: %v", err))
244 return err
245 }
246 if resp.StatusCode != http.StatusOK {
247 logger.LogError(ctx, fmt.Sprintf("Get Task status code: %d", resp.StatusCode))
248 return fmt.Errorf("Get Task status code: %d", resp.StatusCode)
249 }
250 defer resp.Body.Close()
251 responseBody, err := io.ReadAll(resp.Body)
252 if err != nil {
253 common.SysLog(fmt.Sprintf("Get Suno Task parse body error: %v", err))
254 return err
255 }
256 var responseItems dto.TaskResponse[[]dto.SunoDataResponse]
257 err = common.Unmarshal(responseBody, &responseItems)
258 if err != nil {
259 logger.LogError(ctx, fmt.Sprintf("Get Suno Task parse body error2: %v, body: %s", err, string(responseBody)))
260 return err
261 }
262 if !responseItems.IsSuccess() {
263 common.SysLog(fmt.Sprintf("渠道 #%d 未完成的任务有: %d, 成功获取到任务数: %s", channelId, len(taskIds), string(responseBody)))

Callers 1

UpdateSunoTasksFunction · 0.85

Calls 15

LogInfoFunction · 0.92
CacheGetChannelFunction · 0.92
TaskBulkUpdateByIDFunction · 0.92
LogErrorFunction · 0.92
LogWarnFunction · 0.92
TaskStatusTypeAlias · 0.92
taskNeedsUpdateFunction · 0.85
RefundTaskQuotaFunction · 0.85
ReadAllMethod · 0.80
UnmarshalMethod · 0.80
IsSuccessMethod · 0.80
FetchTaskMethod · 0.65

Tested by

no test coverage detected