{"record":{"id":"73afa8b24530900f","repo":"flipped-aurora/gin-vue-admin","slug":"s-initialize-timer-go-task-register","errorCode":null,"errorMessage":"方法 %s 未注册(需在 initialize/timer.go 经 task.Register 注册)","messagePattern":"方法 (.+?) 未注册\\(需在 initialize/timer\\.go 经 task\\.Register 注册\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_timed_task_runner.go","lineNumber":100,"sourceCode":"\t\tErrorMsg:    errMsg,\n\t\tOutput:      truncateText(output, maxLogTextLen),\n\t}\n\tctx := datascope.WithSystem(context.Background())\n\tif err := global.GVA_DB.WithContext(ctx).Create(&logRow).Error; err != nil {\n\t\tlogger.Bg().Mod(\"timedTask\").Err(err).Error(\"定时任务执行日志落库失败: \" + t.Name)\n\t}\n\tif runErr != nil {\n\t\tlogger.Bg().Mod(\"timedTask\").Err(runErr).Error(\"定时任务执行失败: \" + t.Name)\n\t\ts.alertFailure(t, errMsg)\n\t}\n}\n\n// runMethod 执行已注册本体方法。\n// 超时语义: 只能标记状态, goroutine 无法强杀; 任务函数应响应 ctx 取消。\nfunc (s *TimedTaskService) runMethod(t system.SysTimedTask) (string, error) {\n\tfn, ok := task.Get(t.MethodName)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"方法 %s 未注册(需在 initialize/timer.go 经 task.Register 注册)\", t.MethodName)\n\t}\n\tctx, cancel := context.WithTimeout(datascope.WithSystem(context.Background()), defaultMethodTimeout)\n\tdefer cancel()\n\n\tdone := make(chan error, 1)\n\tgo func() {\n\t\tdefer func() {\n\t\t\tif r := recover(); r != nil {\n\t\t\t\tdone <- fmt.Errorf(\"panic: %v\", r)\n\t\t\t}\n\t\t}()\n\t\tdone <- fn(ctx, json.RawMessage(t.Params))\n\t}()\n\tselect {\n\tcase err := <-done:\n\t\tif err != nil && errors.Is(err, context.DeadlineExceeded) {\n\t\t\treturn \"\", errTaskTimeout\n\t\t}","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_timed_task_runner.go#L82-L118","documentation":"runMethod looks up the task function by t.MethodName in the in-process task registry (task.Get). If no function with that name was registered via task.Register in initialize/timer.go, the run fails with this message. Registration happens at application startup, so the name must match exactly at runtime.","triggerScenarios":"SysTimedTask.MethodName contains a name never passed to task.Register; the registering code was removed/renamed during refactoring; task created before a deploy that renamed the function.","commonSituations":"Typo or case mismatch between task row's method_name and Register call; deploy where initialize/timer.go no longer registers the method but old DB rows still reference it; multi-binary deploys where the target binary doesn't include the registration.","solutions":["Register the function in initialize/timer.go via task.Register(\"<name>\", fn) so it matches t.MethodName exactly","Fix the task row's method_name to an already-registered name (check registration list at startup)","Restart the server after adding the registration — the registry is in-process"],"exampleFix":"// before (initialize/timer.go missing)\n// nothing\n// after\nfunc init() { task.Register(\"cleanExpiredTokens\", cleanExpiredTokens) }","handlingStrategy":"validation","validationCode":"if _, ok := task.Get(methodName); !ok {\n    return fmt.Errorf(\"method %s is not registered\", methodName)\n}","typeGuard":null,"tryCatchPattern":"if err := RunTask(t); err != nil {\n    if strings.Contains(err.Error(), \"未注册\") {\n        log.Warnf(\"task %d references unregistered method %s; register it in initialize/timer.go\", t.ID, t.MethodName)\n    }\n}","preventionTips":["Register every task function in initialize/timer.go at startup and keep the list reviewed in code review","Validate method_name against task registry when the task is created via API/UI","Add a startup health check that scans enabled tasks and logs any unregistered method names"],"tags":["timed-task","registry","go"],"backgroundTag":"function-not-registered","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}