feat: workflow run status & time

This commit is contained in:
Fu Diwei
2025-01-04 22:07:01 +08:00
parent b686579acc
commit 3b9a7fe805
29 changed files with 505 additions and 181 deletions

View File

@@ -44,27 +44,28 @@ func update(ctx context.Context, record *models.Record) error {
// 是不是自动
// 是不是 enabled
id := record.Id
workflowId := record.Id
enabled := record.GetBool("enabled")
trigger := record.GetString("trigger")
scheduler := app.GetScheduler()
if !enabled || trigger == domain.WorkflowTriggerManual {
scheduler.Remove(id)
if !enabled || trigger == string(domain.WorkflowTriggerTypeManual) {
scheduler.Remove(workflowId)
scheduler.Start()
return nil
}
err := scheduler.Add(id, record.GetString("triggerCron"), func() {
err := scheduler.Add(workflowId, record.GetString("triggerCron"), func() {
NewWorkflowService(repository.NewWorkflowRepository()).Run(ctx, &domain.WorkflowRunReq{
Id: id,
WorkflowId: workflowId,
Trigger: domain.WorkflowTriggerTypeAuto,
})
})
if err != nil {
app.GetApp().Logger().Error("add cron job failed", "err", err)
app.GetLogger().Error("add cron job failed", "err", err)
return fmt.Errorf("add cron job failed: %w", err)
}
app.GetApp().Logger().Error("add cron job failed", "subjectAltNames", record.GetString("subjectAltNames"))
app.GetLogger().Error("add cron job failed", "subjectAltNames", record.GetString("subjectAltNames"))
scheduler.Start()
return nil

View File

@@ -98,7 +98,7 @@ func (a *applyNode) Run(ctx context.Context) error {
}
certificateRecord := &domain.Certificate{
Source: string(domain.CERTIFICATE_SOURCE_WORKFLOW),
Source: domain.CertificateSourceTypeWorkflow,
SubjectAltNames: strings.Join(certX509.DNSNames, ";"),
Certificate: certificate.Certificate,
PrivateKey: certificate.PrivateKey,

View File

@@ -3,6 +3,7 @@ package workflow
import (
"context"
"fmt"
"time"
"github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain"
@@ -11,7 +12,7 @@ import (
type WorkflowRepository interface {
Get(ctx context.Context, id string) (*domain.Workflow, error)
SaveRunLog(ctx context.Context, log *domain.WorkflowRun) error
SaveRun(ctx context.Context, run *domain.WorkflowRun) error
ListEnabledAuto(ctx context.Context) ([]domain.Workflow, error)
}
@@ -31,71 +32,80 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
if err != nil {
return err
}
scheduler := app.GetScheduler()
for _, workflow := range workflows {
err := scheduler.Add(workflow.Id, workflow.TriggerCron, func() {
s.Run(ctx, &domain.WorkflowRunReq{
Id: workflow.Id,
WorkflowId: workflow.Id,
Trigger: domain.WorkflowTriggerTypeAuto,
})
})
if err != nil {
app.GetApp().Logger().Error("failed to add schedule", "err", err)
app.GetLogger().Error("failed to add schedule", "err", err)
return err
}
}
scheduler.Start()
app.GetApp().Logger().Info("workflow schedule started")
app.GetLogger().Info("workflow schedule started")
return nil
}
func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) error {
func (s *WorkflowService) Run(ctx context.Context, options *domain.WorkflowRunReq) error {
// 查询
if req.Id == "" {
if options.WorkflowId == "" {
return domain.ErrInvalidParams
}
workflow, err := s.repo.Get(ctx, req.Id)
workflow, err := s.repo.Get(ctx, options.WorkflowId)
if err != nil {
app.GetApp().Logger().Error("failed to get workflow", "id", req.Id, "err", err)
app.GetLogger().Error("failed to get workflow", "id", options.WorkflowId, "err", err)
return err
}
// 执行
if !workflow.Enabled {
app.GetApp().Logger().Error("workflow is disabled", "id", req.Id)
app.GetLogger().Error("workflow is disabled", "id", options.WorkflowId)
return fmt.Errorf("workflow is disabled")
}
// 执行
run := &domain.WorkflowRun{
WorkflowId: workflow.Id,
Status: domain.WorkflowRunStatusTypeRunning,
Trigger: options.Trigger,
StartedAt: time.Now(),
EndedAt: time.Now(),
}
processor := nodeprocessor.NewWorkflowProcessor(workflow)
if err := processor.Run(ctx); err != nil {
log := &domain.WorkflowRun{
WorkflowId: workflow.Id,
Logs: processor.Log(ctx),
Succeeded: false,
Error: err.Error(),
}
if err := s.repo.SaveRunLog(ctx, log); err != nil {
app.GetApp().Logger().Error("failed to save run log", "err", err)
run.Status = domain.WorkflowRunStatusTypeFailed
run.EndedAt = time.Now()
run.Logs = processor.Log(ctx)
run.Error = err.Error()
if err := s.repo.SaveRun(ctx, run); err != nil {
app.GetLogger().Error("failed to save workflow run", "err", err)
}
return fmt.Errorf("failed to run workflow: %w", err)
}
// 保存执行日志
logs := processor.Log(ctx)
runLogs := domain.WorkflowRunLogs(logs)
runErr := runLogs.FirstError()
succeed := true
if runErr != "" {
succeed = false
runStatus := domain.WorkflowRunStatusTypeSucceeded
runError := domain.WorkflowRunLogs(logs).FirstError()
if runError != "" {
runStatus = domain.WorkflowRunStatusTypeFailed
}
log := &domain.WorkflowRun{
WorkflowId: workflow.Id,
Logs: processor.Log(ctx),
Error: runErr,
Succeeded: succeed,
}
if err := s.repo.SaveRunLog(ctx, log); err != nil {
app.GetApp().Logger().Error("failed to save run log", "err", err)
run.Status = runStatus
run.EndedAt = time.Now()
run.Logs = processor.Log(ctx)
run.Error = runError
if err := s.repo.SaveRun(ctx, run); err != nil {
app.GetLogger().Error("failed to save workflow run", "err", err)
return err
}