feat(workflow): log failed workflow run preparation when version is disabled
This commit is contained in:
+1
-1
Submodule docs updated: 57c8077187...47d22d9d57
@@ -41,6 +41,7 @@ func (s *Service) Run(ctx context.Context, req Request) (*Summary, error) {
|
||||
req.UserMessage.Content = utils.BuildRuntimeMessageText(req.UserMessage.MessageType, req.UserMessage.Content)
|
||||
aiAgent, workflow, err := prepareWorkflowAgent(req.AIAgent)
|
||||
if err != nil {
|
||||
_, _ = writeWorkflowPrepareFailedRun(req, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
req.AIAgent = aiAgent
|
||||
@@ -183,6 +184,33 @@ func writeWorkflowRun(req Request, workflow resolvedWorkflow, result *workflowex
|
||||
return writeWorkflowRunWithExistingID(req, workflow, result, errorMessage, 0)
|
||||
}
|
||||
|
||||
func writeWorkflowPrepareFailedRun(req Request, errorMessage string) (int64, error) {
|
||||
now := time.Now()
|
||||
endedAt := now
|
||||
workflowID := int64(0)
|
||||
workflowVersionID := req.AIAgent.WorkflowVersionID
|
||||
if workflowVersionID > 0 {
|
||||
if version := repositories.AIWorkflowVersionRepository.Get(sqls.DB(), workflowVersionID); version != nil {
|
||||
workflowID = version.WorkflowID
|
||||
}
|
||||
}
|
||||
run := &models.AIWorkflowRun{
|
||||
WorkflowID: workflowID,
|
||||
WorkflowVersionID: workflowVersionID,
|
||||
ConversationID: req.Conversation.ID,
|
||||
AIAgentID: req.AIAgent.ID,
|
||||
MessageID: req.UserMessage.ID,
|
||||
Status: workflowRunStatusFailed,
|
||||
StartedAt: now,
|
||||
EndedAt: &endedAt,
|
||||
ErrorMessage: errorMessage,
|
||||
}
|
||||
if err := repositories.AIWorkflowRunRepository.Create(sqls.DB(), run); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return run.ID, nil
|
||||
}
|
||||
|
||||
func writeWorkflowRunWithExistingID(req Request, workflow resolvedWorkflow, result *workflowexecutor.Result, errorMessage string, existingRunID int64) (int64, error) {
|
||||
if result == nil {
|
||||
return 0, nil
|
||||
|
||||
@@ -232,6 +232,48 @@ func TestServiceRunWritesFailedWorkflowRun(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceRunWritesFailedWorkflowRunWhenVersionDisabled(t *testing.T) {
|
||||
db := setupWorkflowResumeTestDB(t)
|
||||
version := models.AIWorkflowVersion{
|
||||
WorkflowID: 9,
|
||||
Version: 1,
|
||||
Status: enums.StatusDisabled,
|
||||
Definition: mustMarshalDefinition(t, runtimeHumanConfirmDefinition()),
|
||||
}
|
||||
if err := db.Create(&version).Error; err != nil {
|
||||
t.Fatalf("create disabled workflow version: %v", err)
|
||||
}
|
||||
|
||||
_, err := NewService().Run(context.Background(), Request{
|
||||
Conversation: models.Conversation{ID: 10},
|
||||
UserMessage: models.Message{ID: 20, Content: "hello"},
|
||||
AIAgent: models.AIAgent{
|
||||
ID: 30,
|
||||
WorkflowVersionID: version.ID,
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("expected disabled workflow version error")
|
||||
}
|
||||
var run models.AIWorkflowRun
|
||||
if err := db.First(&run, "workflow_version_id = ?", version.ID).Error; err != nil {
|
||||
t.Fatalf("find prepare-stage failed workflow run: %v", err)
|
||||
}
|
||||
if run.WorkflowID != version.WorkflowID || run.ConversationID != 10 || run.AIAgentID != 30 || run.MessageID != 20 {
|
||||
t.Fatalf("unexpected prepare-stage failed workflow run identity: %#v", run)
|
||||
}
|
||||
if run.Status != workflowRunStatusFailed || !strings.Contains(run.ErrorMessage, "workflow version does not exist") {
|
||||
t.Fatalf("unexpected prepare-stage failed workflow run: %#v", run)
|
||||
}
|
||||
var nodeCount int64
|
||||
if err := db.Model(&models.AIWorkflowNodeRun{}).Where("workflow_run_id = ?", run.ID).Count(&nodeCount).Error; err != nil {
|
||||
t.Fatalf("count node runs: %v", err)
|
||||
}
|
||||
if nodeCount != 0 {
|
||||
t.Fatalf("expected no node runs for prepare-stage failure, got %d", nodeCount)
|
||||
}
|
||||
}
|
||||
|
||||
func setupWorkflowResumeTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
dbName := strings.NewReplacer("/", "_", " ", "_").Replace(t.Name())
|
||||
|
||||
Reference in New Issue
Block a user