mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 05:29:51 +00:00
refactor: clean code
This commit is contained in:
parent
3f9fda8a2d
commit
886f166e66
@ -30,18 +30,18 @@ type certificateRepository interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CertificateService struct {
|
type CertificateService struct {
|
||||||
repo certificateRepository
|
certRepo certificateRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCertificateService(repo certificateRepository) *CertificateService {
|
func NewCertificateService(certRepo certificateRepository) *CertificateService {
|
||||||
return &CertificateService{
|
return &CertificateService{
|
||||||
repo: repo,
|
certRepo: certRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CertificateService) InitSchedule(ctx context.Context) error {
|
func (s *CertificateService) InitSchedule(ctx context.Context) error {
|
||||||
app.GetScheduler().MustAdd("certificateExpireSoonNotify", "0 0 * * *", func() {
|
app.GetScheduler().MustAdd("certificateExpireSoonNotify", "0 0 * * *", func() {
|
||||||
certs, err := s.repo.ListExpireSoon(context.Background())
|
certs, err := s.certRepo.ListExpireSoon(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.GetLogger().Error("failed to get certificates which expire soon", "err", err)
|
app.GetLogger().Error("failed to get certificates which expire soon", "err", err)
|
||||||
return
|
return
|
||||||
@ -60,7 +60,7 @@ func (s *CertificateService) InitSchedule(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *CertificateService) ArchiveFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) ([]byte, error) {
|
func (s *CertificateService) ArchiveFile(ctx context.Context, req *dtos.CertificateArchiveFileReq) ([]byte, error) {
|
||||||
certificate, err := s.repo.GetById(ctx, req.CertificateId)
|
certificate, err := s.certRepo.GetById(ctx, req.CertificateId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import "github.com/usual2970/certimate/internal/domain"
|
|||||||
|
|
||||||
type WorkflowStartRunReq struct {
|
type WorkflowStartRunReq struct {
|
||||||
WorkflowId string `json:"-"`
|
WorkflowId string `json:"-"`
|
||||||
Trigger domain.WorkflowTriggerType `json:"trigger"`
|
RunTrigger domain.WorkflowTriggerType `json:"trigger"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkflowCancelRunReq struct {
|
type WorkflowCancelRunReq struct {
|
||||||
|
@ -95,65 +95,6 @@ func (r *WorkflowRepository) Save(ctx context.Context, workflow *domain.Workflow
|
|||||||
return workflow, nil
|
return workflow, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *WorkflowRepository) SaveRun(ctx context.Context, run *domain.WorkflowRun) (*domain.WorkflowRun, error) {
|
|
||||||
collection, err := app.GetApp().FindCollectionByNameOrId(domain.CollectionNameWorkflowRun)
|
|
||||||
if err != nil {
|
|
||||||
return run, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var runRecord *core.Record
|
|
||||||
if run.Id == "" {
|
|
||||||
runRecord = core.NewRecord(collection)
|
|
||||||
} else {
|
|
||||||
runRecord, err = app.GetApp().FindRecordById(collection, run.Id)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
|
||||||
return run, err
|
|
||||||
}
|
|
||||||
runRecord = core.NewRecord(collection)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = app.GetApp().RunInTransaction(func(txApp core.App) error {
|
|
||||||
runRecord.Set("workflowId", run.WorkflowId)
|
|
||||||
runRecord.Set("trigger", string(run.Trigger))
|
|
||||||
runRecord.Set("status", string(run.Status))
|
|
||||||
runRecord.Set("startedAt", run.StartedAt)
|
|
||||||
runRecord.Set("endedAt", run.EndedAt)
|
|
||||||
runRecord.Set("logs", run.Logs)
|
|
||||||
runRecord.Set("error", run.Error)
|
|
||||||
err = txApp.Save(runRecord)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
workflowRecord, err := txApp.FindRecordById(domain.CollectionNameWorkflow, run.WorkflowId)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
workflowRecord.IgnoreUnchangedFields(true)
|
|
||||||
workflowRecord.Set("lastRunId", runRecord.Id)
|
|
||||||
workflowRecord.Set("lastRunStatus", runRecord.GetString("status"))
|
|
||||||
workflowRecord.Set("lastRunTime", runRecord.GetString("startedAt"))
|
|
||||||
err = txApp.Save(workflowRecord)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
run.Id = runRecord.Id
|
|
||||||
run.CreatedAt = runRecord.GetDateTime("created").Time()
|
|
||||||
run.UpdatedAt = runRecord.GetDateTime("updated").Time()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return run, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return run, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *WorkflowRepository) castRecordToModel(record *core.Record) (*domain.Workflow, error) {
|
func (r *WorkflowRepository) castRecordToModel(record *core.Record) (*domain.Workflow, error) {
|
||||||
if record == nil {
|
if record == nil {
|
||||||
return nil, fmt.Errorf("record is nil")
|
return nil, fmt.Errorf("record is nil")
|
||||||
|
@ -94,12 +94,12 @@ func (r *WorkflowOutputRepository) castRecordToModel(record *core.Record) (*doma
|
|||||||
|
|
||||||
node := &domain.WorkflowNode{}
|
node := &domain.WorkflowNode{}
|
||||||
if err := record.UnmarshalJSONField("node", node); err != nil {
|
if err := record.UnmarshalJSONField("node", node); err != nil {
|
||||||
return nil, errors.New("failed to unmarshal node")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
outputs := make([]domain.WorkflowNodeIO, 0)
|
outputs := make([]domain.WorkflowNodeIO, 0)
|
||||||
if err := record.UnmarshalJSONField("outputs", &outputs); err != nil {
|
if err := record.UnmarshalJSONField("outputs", &outputs); err != nil {
|
||||||
return nil, errors.New("failed to unmarshal output")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
workflowOutput := &domain.WorkflowOutput{
|
workflowOutput := &domain.WorkflowOutput{
|
||||||
@ -118,27 +118,27 @@ func (r *WorkflowOutputRepository) castRecordToModel(record *core.Record) (*doma
|
|||||||
return workflowOutput, nil
|
return workflowOutput, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *WorkflowOutputRepository) saveRecord(output *domain.WorkflowOutput) (*core.Record, error) {
|
func (r *WorkflowOutputRepository) saveRecord(workflowOutput *domain.WorkflowOutput) (*core.Record, error) {
|
||||||
collection, err := app.GetApp().FindCollectionByNameOrId(domain.CollectionNameWorkflowOutput)
|
collection, err := app.GetApp().FindCollectionByNameOrId(domain.CollectionNameWorkflowOutput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var record *core.Record
|
var record *core.Record
|
||||||
if output.Id == "" {
|
if workflowOutput.Id == "" {
|
||||||
record = core.NewRecord(collection)
|
record = core.NewRecord(collection)
|
||||||
} else {
|
} else {
|
||||||
record, err = app.GetApp().FindRecordById(collection, output.Id)
|
record, err = app.GetApp().FindRecordById(collection, workflowOutput.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return record, err
|
return record, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
record.Set("workflowId", output.WorkflowId)
|
record.Set("workflowId", workflowOutput.WorkflowId)
|
||||||
record.Set("runId", output.RunId)
|
record.Set("runId", workflowOutput.RunId)
|
||||||
record.Set("nodeId", output.NodeId)
|
record.Set("nodeId", workflowOutput.NodeId)
|
||||||
record.Set("node", output.Node)
|
record.Set("node", workflowOutput.Node)
|
||||||
record.Set("outputs", output.Outputs)
|
record.Set("outputs", workflowOutput.Outputs)
|
||||||
record.Set("succeeded", output.Succeeded)
|
record.Set("succeeded", workflowOutput.Succeeded)
|
||||||
if err := app.GetApp().Save(record); err != nil {
|
if err := app.GetApp().Save(record); err != nil {
|
||||||
return record, err
|
return record, err
|
||||||
}
|
}
|
||||||
|
117
internal/repository/workflow_run.go
Normal file
117
internal/repository/workflow_run.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pocketbase/pocketbase/core"
|
||||||
|
"github.com/usual2970/certimate/internal/app"
|
||||||
|
"github.com/usual2970/certimate/internal/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WorkflowRunRepository struct{}
|
||||||
|
|
||||||
|
func NewWorkflowRunRepository() *WorkflowRunRepository {
|
||||||
|
return &WorkflowRunRepository{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *WorkflowRunRepository) GetById(ctx context.Context, id string) (*domain.WorkflowRun, error) {
|
||||||
|
record, err := app.GetApp().FindRecordById(domain.CollectionNameWorkflowRun, id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return nil, domain.ErrRecordNotFound
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.castRecordToModel(record)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *WorkflowRunRepository) Save(ctx context.Context, workflowRun *domain.WorkflowRun) (*domain.WorkflowRun, error) {
|
||||||
|
collection, err := app.GetApp().FindCollectionByNameOrId(domain.CollectionNameWorkflowRun)
|
||||||
|
if err != nil {
|
||||||
|
return workflowRun, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var record *core.Record
|
||||||
|
if workflowRun.Id == "" {
|
||||||
|
record = core.NewRecord(collection)
|
||||||
|
} else {
|
||||||
|
record, err = app.GetApp().FindRecordById(collection, workflowRun.Id)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return workflowRun, err
|
||||||
|
}
|
||||||
|
record = core.NewRecord(collection)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = app.GetApp().RunInTransaction(func(txApp core.App) error {
|
||||||
|
record.Set("workflowId", workflowRun.WorkflowId)
|
||||||
|
record.Set("trigger", string(workflowRun.Trigger))
|
||||||
|
record.Set("status", string(workflowRun.Status))
|
||||||
|
record.Set("startedAt", workflowRun.StartedAt)
|
||||||
|
record.Set("endedAt", workflowRun.EndedAt)
|
||||||
|
record.Set("logs", workflowRun.Logs)
|
||||||
|
record.Set("error", workflowRun.Error)
|
||||||
|
err = txApp.Save(record)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
workflowRun.Id = record.Id
|
||||||
|
workflowRun.CreatedAt = record.GetDateTime("created").Time()
|
||||||
|
workflowRun.UpdatedAt = record.GetDateTime("updated").Time()
|
||||||
|
|
||||||
|
// 事务级联更新所属工作流的最后运行记录
|
||||||
|
workflowRecord, err := txApp.FindRecordById(domain.CollectionNameWorkflow, workflowRun.WorkflowId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if workflowRecord.GetDateTime("lastRunTime").Time().IsZero() || workflowRun.StartedAt.After(workflowRecord.GetDateTime("lastRunTime").Time()) {
|
||||||
|
workflowRecord.IgnoreUnchangedFields(true)
|
||||||
|
workflowRecord.Set("lastRunId", record.Id)
|
||||||
|
workflowRecord.Set("lastRunStatus", record.GetString("status"))
|
||||||
|
workflowRecord.Set("lastRunTime", record.GetString("startedAt"))
|
||||||
|
err = txApp.Save(workflowRecord)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return workflowRun, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return workflowRun, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *WorkflowRunRepository) castRecordToModel(record *core.Record) (*domain.WorkflowRun, error) {
|
||||||
|
if record == nil {
|
||||||
|
return nil, fmt.Errorf("record is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
logs := make([]domain.WorkflowRunLog, 0)
|
||||||
|
if err := record.UnmarshalJSONField("logs", &logs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
workflowRun := &domain.WorkflowRun{
|
||||||
|
Meta: domain.Meta{
|
||||||
|
Id: record.Id,
|
||||||
|
CreatedAt: record.GetDateTime("created").Time(),
|
||||||
|
UpdatedAt: record.GetDateTime("updated").Time(),
|
||||||
|
},
|
||||||
|
WorkflowId: record.GetString("workflowId"),
|
||||||
|
Status: domain.WorkflowRunStatusType(record.GetString("status")),
|
||||||
|
Trigger: domain.WorkflowTriggerType(record.GetString("trigger")),
|
||||||
|
StartedAt: record.GetDateTime("startedAt").Time(),
|
||||||
|
EndedAt: record.GetDateTime("endedAt").Time(),
|
||||||
|
Logs: logs,
|
||||||
|
Error: record.GetString("error"),
|
||||||
|
}
|
||||||
|
return workflowRun, nil
|
||||||
|
}
|
@ -27,13 +27,14 @@ func Register(router *router.Router[*core.RequestEvent]) {
|
|||||||
certificateSvc = certificate.NewCertificateService(certificateRepo)
|
certificateSvc = certificate.NewCertificateService(certificateRepo)
|
||||||
|
|
||||||
workflowRepo := repository.NewWorkflowRepository()
|
workflowRepo := repository.NewWorkflowRepository()
|
||||||
workflowSvc = workflow.NewWorkflowService(workflowRepo)
|
workflowRunRepo := repository.NewWorkflowRunRepository()
|
||||||
|
workflowSvc = workflow.NewWorkflowService(workflowRepo, workflowRunRepo)
|
||||||
|
|
||||||
statisticsRepo := repository.NewStatisticsRepository()
|
statisticsRepo := repository.NewStatisticsRepository()
|
||||||
statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
|
statisticsSvc = statistics.NewStatisticsService(statisticsRepo)
|
||||||
|
|
||||||
notifyRepo := repository.NewSettingsRepository()
|
settingsRepo := repository.NewSettingsRepository()
|
||||||
notifySvc = notify.NewNotifyService(notifyRepo)
|
notifySvc = notify.NewNotifyService(settingsRepo)
|
||||||
|
|
||||||
group := router.Group("/api")
|
group := router.Group("/api")
|
||||||
group.Bind(apis.RequireSuperuserAuth())
|
group.Bind(apis.RequireSuperuserAuth())
|
||||||
|
@ -8,7 +8,8 @@ import (
|
|||||||
|
|
||||||
func Register() {
|
func Register() {
|
||||||
workflowRepo := repository.NewWorkflowRepository()
|
workflowRepo := repository.NewWorkflowRepository()
|
||||||
workflowSvc := workflow.NewWorkflowService(workflowRepo)
|
workflowRunRepo := repository.NewWorkflowRunRepository()
|
||||||
|
workflowSvc := workflow.NewWorkflowService(workflowRepo, workflowRunRepo)
|
||||||
|
|
||||||
certificateRepo := repository.NewCertificateRepository()
|
certificateRepo := repository.NewCertificateRepository()
|
||||||
certificateSvc := certificate.NewCertificateService(certificateRepo)
|
certificateSvc := certificate.NewCertificateService(certificateRepo)
|
||||||
|
@ -11,15 +11,15 @@ type statisticsRepository interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type StatisticsService struct {
|
type StatisticsService struct {
|
||||||
repo statisticsRepository
|
statRepo statisticsRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStatisticsService(repo statisticsRepository) *StatisticsService {
|
func NewStatisticsService(statRepo statisticsRepository) *StatisticsService {
|
||||||
return &StatisticsService{
|
return &StatisticsService{
|
||||||
repo: repo,
|
statRepo: statRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StatisticsService) Get(ctx context.Context) (*domain.Statistics, error) {
|
func (s *StatisticsService) Get(ctx context.Context) (*domain.Statistics, error) {
|
||||||
return s.repo.Get(ctx)
|
return s.statRepo.Get(ctx)
|
||||||
}
|
}
|
||||||
|
@ -65,9 +65,10 @@ func onWorkflowRecordCreateOrUpdate(ctx context.Context, record *core.Record) er
|
|||||||
|
|
||||||
// 反之,重新添加定时任务
|
// 反之,重新添加定时任务
|
||||||
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflowId), record.GetString("triggerCron"), func() {
|
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflowId), record.GetString("triggerCron"), func() {
|
||||||
NewWorkflowService(repository.NewWorkflowRepository()).StartRun(ctx, &dtos.WorkflowStartRunReq{
|
workflowSrv := NewWorkflowService(repository.NewWorkflowRepository(), repository.NewWorkflowRunRepository())
|
||||||
|
workflowSrv.StartRun(ctx, &dtos.WorkflowStartRunReq{
|
||||||
WorkflowId: workflowId,
|
WorkflowId: workflowId,
|
||||||
Trigger: domain.WorkflowTriggerTypeAuto,
|
RunTrigger: domain.WorkflowTriggerTypeAuto,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,7 +31,7 @@ func NewApplyNode(node *domain.WorkflowNode) *applyNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *applyNode) Run(ctx context.Context) error {
|
func (n *applyNode) Process(ctx context.Context) error {
|
||||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||||
|
|
||||||
// 查询上次执行结果
|
// 查询上次执行结果
|
||||||
|
@ -18,7 +18,7 @@ func NewConditionNode(node *domain.WorkflowNode) *conditionNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *conditionNode) Run(ctx context.Context) error {
|
func (n *conditionNode) Process(ctx context.Context) error {
|
||||||
// 此类型节点不需要执行任何操作,直接返回
|
// 此类型节点不需要执行任何操作,直接返回
|
||||||
n.AddOutput(ctx, n.node.Name, "完成")
|
n.AddOutput(ctx, n.node.Name, "完成")
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ func NewDeployNode(node *domain.WorkflowNode) *deployNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *deployNode) Run(ctx context.Context) error {
|
func (n *deployNode) Process(ctx context.Context) error {
|
||||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||||
|
|
||||||
// 查询上次执行结果
|
// 查询上次执行结果
|
||||||
|
@ -18,7 +18,7 @@ func NewExecuteFailureNode(node *domain.WorkflowNode) *executeFailureNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *executeFailureNode) Run(ctx context.Context) error {
|
func (n *executeFailureNode) Process(ctx context.Context) error {
|
||||||
// 此类型节点不需要执行任何操作,直接返回
|
// 此类型节点不需要执行任何操作,直接返回
|
||||||
n.AddOutput(ctx, n.node.Name, "进入执行失败分支")
|
n.AddOutput(ctx, n.node.Name, "进入执行失败分支")
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ func NewExecuteSuccessNode(node *domain.WorkflowNode) *executeSuccessNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *executeSuccessNode) Run(ctx context.Context) error {
|
func (n *executeSuccessNode) Process(ctx context.Context) error {
|
||||||
// 此类型节点不需要执行任何操作,直接返回
|
// 此类型节点不需要执行任何操作,直接返回
|
||||||
n.AddOutput(ctx, n.node.Name, "进入执行成功分支")
|
n.AddOutput(ctx, n.node.Name, "进入执行成功分支")
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ func NewNotifyNode(node *domain.WorkflowNode) *notifyNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *notifyNode) Run(ctx context.Context) error {
|
func (n *notifyNode) Process(ctx context.Context) error {
|
||||||
n.AddOutput(ctx, n.node.Name, "开始执行")
|
n.AddOutput(ctx, n.node.Name, "开始执行")
|
||||||
|
|
||||||
nodeConfig := n.node.GetConfigForNotify()
|
nodeConfig := n.node.GetConfigForNotify()
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type NodeProcessor interface {
|
type NodeProcessor interface {
|
||||||
Run(ctx context.Context) error
|
Process(ctx context.Context) error
|
||||||
GetLog(ctx context.Context) *domain.WorkflowRunLog
|
GetLog(ctx context.Context) *domain.WorkflowRunLog
|
||||||
AddOutput(ctx context.Context, title, content string, err ...string)
|
AddOutput(ctx context.Context, title, content string, err ...string)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ func NewStartNode(node *domain.WorkflowNode) *startNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *startNode) Run(ctx context.Context) error {
|
func (n *startNode) Process(ctx context.Context) error {
|
||||||
// 此类型节点不需要执行任何操作,直接返回
|
// 此类型节点不需要执行任何操作,直接返回
|
||||||
n.AddOutput(ctx, n.node.Name, "完成")
|
n.AddOutput(ctx, n.node.Name, "完成")
|
||||||
|
|
||||||
|
@ -29,9 +29,7 @@ func NewUploadNode(node *domain.WorkflowNode) *uploadNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run 上传证书节点执行
|
func (n *uploadNode) Process(ctx context.Context) error {
|
||||||
// 包含上传证书的工作流,理论上应该手动执行,如果每天定时执行,也只是重新保存一下
|
|
||||||
func (n *uploadNode) Run(ctx context.Context) error {
|
|
||||||
n.AddOutput(ctx, n.node.Name, "进入上传证书节点")
|
n.AddOutput(ctx, n.node.Name, "进入上传证书节点")
|
||||||
|
|
||||||
nodeConfig := n.node.GetConfigForUpload()
|
nodeConfig := n.node.GetConfigForUpload()
|
||||||
|
@ -8,27 +8,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type workflowProcessor struct {
|
type workflowProcessor struct {
|
||||||
workflow *domain.Workflow
|
workflowId string
|
||||||
workflowRun *domain.WorkflowRun
|
workflowContent *domain.WorkflowNode
|
||||||
workflorRunLogs []domain.WorkflowRunLog
|
runId string
|
||||||
|
runLogs []domain.WorkflowRunLog
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWorkflowProcessor(workflow *domain.Workflow, workflowRun *domain.WorkflowRun) *workflowProcessor {
|
func NewWorkflowProcessor(workflowId string, workflowContent *domain.WorkflowNode, workflowRunId string) *workflowProcessor {
|
||||||
return &workflowProcessor{
|
return &workflowProcessor{
|
||||||
workflow: workflow,
|
workflowId: workflowId,
|
||||||
workflowRun: workflowRun,
|
workflowContent: workflowContent,
|
||||||
workflorRunLogs: make([]domain.WorkflowRunLog, 0),
|
runId: workflowRunId,
|
||||||
|
runLogs: make([]domain.WorkflowRunLog, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *workflowProcessor) Run(ctx context.Context) error {
|
func (w *workflowProcessor) Process(ctx context.Context) error {
|
||||||
ctx = context.WithValue(ctx, "workflow_id", w.workflow.Id)
|
ctx = context.WithValue(ctx, "workflow_id", w.workflowId)
|
||||||
ctx = context.WithValue(ctx, "workflow_run_id", w.workflowRun.Id)
|
ctx = context.WithValue(ctx, "workflow_run_id", w.runId)
|
||||||
return w.processNode(ctx, w.workflow.Content)
|
return w.processNode(ctx, w.workflowContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *workflowProcessor) GetRunLogs() []domain.WorkflowRunLog {
|
func (w *workflowProcessor) GetLogs() []domain.WorkflowRunLog {
|
||||||
return w.workflorRunLogs
|
return w.runLogs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *workflowProcessor) processNode(ctx context.Context, node *domain.WorkflowNode) error {
|
func (w *workflowProcessor) processNode(ctx context.Context, node *domain.WorkflowNode) error {
|
||||||
@ -51,10 +53,10 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
runErr = processor.Run(ctx)
|
runErr = processor.Process(ctx)
|
||||||
log := processor.GetLog(ctx)
|
log := processor.GetLog(ctx)
|
||||||
if log != nil {
|
if log != nil {
|
||||||
w.workflorRunLogs = append(w.workflorRunLogs, *log)
|
w.runLogs = append(w.runLogs, *log)
|
||||||
}
|
}
|
||||||
if runErr != nil {
|
if runErr != nil {
|
||||||
break
|
break
|
||||||
@ -67,9 +69,9 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
|||||||
if runErr != nil && current.Next != nil && current.Next.Type != domain.WorkflowNodeTypeExecuteResultBranch {
|
if runErr != nil && current.Next != nil && current.Next.Type != domain.WorkflowNodeTypeExecuteResultBranch {
|
||||||
return runErr
|
return runErr
|
||||||
} else if runErr != nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
} else if runErr != nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
||||||
current = getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteFailure)
|
current = w.getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteFailure)
|
||||||
} else if runErr == nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
} else if runErr == nil && current.Next != nil && current.Next.Type == domain.WorkflowNodeTypeExecuteResultBranch {
|
||||||
current = getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteSuccess)
|
current = w.getBranchByType(current.Next.Branches, domain.WorkflowNodeTypeExecuteSuccess)
|
||||||
} else {
|
} else {
|
||||||
current = current.Next
|
current = current.Next
|
||||||
}
|
}
|
||||||
@ -78,7 +80,7 @@ func (w *workflowProcessor) processNode(ctx context.Context, node *domain.Workfl
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBranchByType(branches []domain.WorkflowNode, nodeType domain.WorkflowNodeType) *domain.WorkflowNode {
|
func (w *workflowProcessor) getBranchByType(branches []domain.WorkflowNode, nodeType domain.WorkflowNodeType) *domain.WorkflowNode {
|
||||||
for _, branch := range branches {
|
for _, branch := range branches {
|
||||||
if branch.Type == nodeType {
|
if branch.Type == nodeType {
|
||||||
return &branch
|
return &branch
|
||||||
|
@ -13,10 +13,11 @@ import (
|
|||||||
processor "github.com/usual2970/certimate/internal/workflow/processor"
|
processor "github.com/usual2970/certimate/internal/workflow/processor"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultRoutines = 10
|
const defaultRoutines = 16
|
||||||
|
|
||||||
type workflowRunData struct {
|
type workflowRunData struct {
|
||||||
Workflow *domain.Workflow
|
WorkflowId string
|
||||||
|
WorkflowContent *domain.WorkflowNode
|
||||||
RunTrigger domain.WorkflowTriggerType
|
RunTrigger domain.WorkflowTriggerType
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,35 +25,43 @@ type workflowRepository interface {
|
|||||||
ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error)
|
ListEnabledAuto(ctx context.Context) ([]*domain.Workflow, error)
|
||||||
GetById(ctx context.Context, id string) (*domain.Workflow, error)
|
GetById(ctx context.Context, id string) (*domain.Workflow, error)
|
||||||
Save(ctx context.Context, workflow *domain.Workflow) (*domain.Workflow, error)
|
Save(ctx context.Context, workflow *domain.Workflow) (*domain.Workflow, error)
|
||||||
SaveRun(ctx context.Context, workflowRun *domain.WorkflowRun) (*domain.WorkflowRun, error)
|
}
|
||||||
|
|
||||||
|
type workflowRunRepository interface {
|
||||||
|
GetById(ctx context.Context, id string) (*domain.WorkflowRun, error)
|
||||||
|
Save(ctx context.Context, workflowRun *domain.WorkflowRun) (*domain.WorkflowRun, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkflowService struct {
|
type WorkflowService struct {
|
||||||
ch chan *workflowRunData
|
ch chan *workflowRunData
|
||||||
repo workflowRepository
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
|
|
||||||
|
workflowRepo workflowRepository
|
||||||
|
workflowRunRepo workflowRunRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWorkflowService(repo workflowRepository) *WorkflowService {
|
func NewWorkflowService(workflowRepo workflowRepository, workflowRunRepo workflowRunRepository) *WorkflowService {
|
||||||
srv := &WorkflowService{
|
|
||||||
repo: repo,
|
|
||||||
ch: make(chan *workflowRunData, 1),
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
srv.cancel = cancel
|
|
||||||
|
srv := &WorkflowService{
|
||||||
|
ch: make(chan *workflowRunData, 1),
|
||||||
|
cancel: cancel,
|
||||||
|
|
||||||
|
workflowRepo: workflowRepo,
|
||||||
|
workflowRunRepo: workflowRunRepo,
|
||||||
|
}
|
||||||
|
|
||||||
srv.wg.Add(defaultRoutines)
|
srv.wg.Add(defaultRoutines)
|
||||||
for i := 0; i < defaultRoutines; i++ {
|
for i := 0; i < defaultRoutines; i++ {
|
||||||
go srv.run(ctx)
|
go srv.startRun(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
||||||
workflows, err := s.repo.ListEnabledAuto(ctx)
|
workflows, err := s.workflowRepo.ListEnabledAuto(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -62,7 +71,7 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
|||||||
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflow.Id), workflow.TriggerCron, func() {
|
err := scheduler.Add(fmt.Sprintf("workflow#%s", workflow.Id), workflow.TriggerCron, func() {
|
||||||
s.StartRun(ctx, &dtos.WorkflowStartRunReq{
|
s.StartRun(ctx, &dtos.WorkflowStartRunReq{
|
||||||
WorkflowId: workflow.Id,
|
WorkflowId: workflow.Id,
|
||||||
Trigger: domain.WorkflowTriggerTypeAuto,
|
RunTrigger: domain.WorkflowTriggerTypeAuto,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,35 +84,50 @@ func (s *WorkflowService) InitSchedule(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *WorkflowService) StartRun(ctx context.Context, req *dtos.WorkflowStartRunReq) error {
|
func (s *WorkflowService) StartRun(ctx context.Context, req *dtos.WorkflowStartRunReq) error {
|
||||||
workflow, err := s.repo.GetById(ctx, req.WorkflowId)
|
workflow, err := s.workflowRepo.GetById(ctx, req.WorkflowId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
|
app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if workflow.LastRunStatus == domain.WorkflowRunStatusTypeRunning {
|
if workflow.LastRunStatus == domain.WorkflowRunStatusTypePending || workflow.LastRunStatus == domain.WorkflowRunStatusTypeRunning {
|
||||||
return errors.New("workflow is running")
|
return errors.New("workflow is already pending or running")
|
||||||
}
|
|
||||||
|
|
||||||
workflow.LastRunTime = time.Now()
|
|
||||||
workflow.LastRunStatus = domain.WorkflowRunStatusTypePending
|
|
||||||
workflow.LastRunId = ""
|
|
||||||
if resp, err := s.repo.Save(ctx, workflow); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
workflow = resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.ch <- &workflowRunData{
|
s.ch <- &workflowRunData{
|
||||||
Workflow: workflow,
|
WorkflowId: workflow.Id,
|
||||||
RunTrigger: req.Trigger,
|
WorkflowContent: workflow.Content,
|
||||||
|
RunTrigger: req.RunTrigger,
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WorkflowService) CancelRun(ctx context.Context, req *dtos.WorkflowCancelRunReq) error {
|
func (s *WorkflowService) CancelRun(ctx context.Context, req *dtos.WorkflowCancelRunReq) error {
|
||||||
|
workflow, err := s.workflowRepo.GetById(ctx, req.WorkflowId)
|
||||||
|
if err != nil {
|
||||||
|
app.GetLogger().Error("failed to get workflow", "id", req.WorkflowId, "err", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
workflowRun, err := s.workflowRunRepo.GetById(ctx, req.RunId)
|
||||||
|
if err != nil {
|
||||||
|
app.GetLogger().Error("failed to get workflow run", "id", req.RunId, "err", err)
|
||||||
|
return err
|
||||||
|
} else if workflowRun.WorkflowId != workflow.Id {
|
||||||
|
return errors.New("workflow run not found")
|
||||||
|
} else if workflowRun.Status != domain.WorkflowRunStatusTypePending && workflowRun.Status != domain.WorkflowRunStatusTypeRunning {
|
||||||
|
return errors.New("workflow run is not pending or running")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: 取消运行,防止因为某些原因意外挂起(如进程被杀死)导致工作流一直处于 running 状态无法重新运行
|
// TODO: 取消运行,防止因为某些原因意外挂起(如进程被杀死)导致工作流一直处于 running 状态无法重新运行
|
||||||
|
// workflowRun.Status = domain.WorkflowRunStatusTypeCanceled
|
||||||
|
// workflowRun.EndedAt = time.Now()
|
||||||
|
// if _, err := s.workflowRunRepo.Save(ctx, workflowRun); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return nil
|
||||||
|
|
||||||
return errors.New("TODO: 尚未实现")
|
return errors.New("TODO: 尚未实现")
|
||||||
}
|
}
|
||||||
@ -113,13 +137,14 @@ func (s *WorkflowService) Stop(ctx context.Context) {
|
|||||||
s.wg.Wait()
|
s.wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WorkflowService) run(ctx context.Context) {
|
func (s *WorkflowService) startRun(ctx context.Context) {
|
||||||
defer s.wg.Done()
|
defer s.wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case data := <-s.ch:
|
case data := <-s.ch:
|
||||||
if err := s.runWithData(ctx, data); err != nil {
|
if err := s.startRunWithData(ctx, data); err != nil {
|
||||||
app.GetLogger().Error("failed to run workflow", "id", data.Workflow.Id, "err", err)
|
app.GetLogger().Error("failed to run workflow", "id", data.WorkflowId, "err", err)
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
@ -127,27 +152,26 @@ func (s *WorkflowService) run(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WorkflowService) runWithData(ctx context.Context, runData *workflowRunData) error {
|
func (s *WorkflowService) startRunWithData(ctx context.Context, data *workflowRunData) error {
|
||||||
workflow := runData.Workflow
|
|
||||||
run := &domain.WorkflowRun{
|
run := &domain.WorkflowRun{
|
||||||
WorkflowId: workflow.Id,
|
WorkflowId: data.WorkflowId,
|
||||||
Status: domain.WorkflowRunStatusTypeRunning,
|
Status: domain.WorkflowRunStatusTypeRunning,
|
||||||
Trigger: runData.RunTrigger,
|
Trigger: data.RunTrigger,
|
||||||
StartedAt: time.Now(),
|
StartedAt: time.Now(),
|
||||||
}
|
}
|
||||||
if resp, err := s.repo.SaveRun(ctx, run); err != nil {
|
if resp, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
run = resp
|
run = resp
|
||||||
}
|
}
|
||||||
|
|
||||||
processor := processor.NewWorkflowProcessor(workflow, run)
|
processor := processor.NewWorkflowProcessor(data.WorkflowId, data.WorkflowContent, run.Id)
|
||||||
if runErr := processor.Run(ctx); runErr != nil {
|
if runErr := processor.Process(ctx); runErr != nil {
|
||||||
run.Status = domain.WorkflowRunStatusTypeFailed
|
run.Status = domain.WorkflowRunStatusTypeFailed
|
||||||
run.EndedAt = time.Now()
|
run.EndedAt = time.Now()
|
||||||
run.Logs = processor.GetRunLogs()
|
run.Logs = processor.GetLogs()
|
||||||
run.Error = runErr.Error()
|
run.Error = runErr.Error()
|
||||||
if _, err := s.repo.SaveRun(ctx, run); err != nil {
|
if _, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||||
app.GetLogger().Error("failed to save workflow run", "err", err)
|
app.GetLogger().Error("failed to save workflow run", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,14 +179,14 @@ func (s *WorkflowService) runWithData(ctx context.Context, runData *workflowRunD
|
|||||||
}
|
}
|
||||||
|
|
||||||
run.EndedAt = time.Now()
|
run.EndedAt = time.Now()
|
||||||
run.Logs = processor.GetRunLogs()
|
run.Logs = processor.GetLogs()
|
||||||
run.Error = domain.WorkflowRunLogs(run.Logs).ErrorString()
|
run.Error = domain.WorkflowRunLogs(run.Logs).ErrorString()
|
||||||
if run.Error == "" {
|
if run.Error == "" {
|
||||||
run.Status = domain.WorkflowRunStatusTypeSucceeded
|
run.Status = domain.WorkflowRunStatusTypeSucceeded
|
||||||
} else {
|
} else {
|
||||||
run.Status = domain.WorkflowRunStatusTypeFailed
|
run.Status = domain.WorkflowRunStatusTypeFailed
|
||||||
}
|
}
|
||||||
if _, err := s.repo.SaveRun(ctx, run); err != nil {
|
if _, err := s.workflowRunRepo.Save(ctx, run); err != nil {
|
||||||
app.GetLogger().Error("failed to save workflow run", "err", err)
|
app.GetLogger().Error("failed to save workflow run", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
52
ui/src/components/workflow/WorkflowRunDetail.tsx
Normal file
52
ui/src/components/workflow/WorkflowRunDetail.tsx
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { Alert, Typography } from "antd";
|
||||||
|
import dayjs from "dayjs";
|
||||||
|
|
||||||
|
import Show from "@/components/Show";
|
||||||
|
import { WORKFLOW_RUN_STATUSES, type WorkflowRunModel } from "@/domain/workflowRun";
|
||||||
|
|
||||||
|
export type WorkflowRunDetailProps = {
|
||||||
|
className?: string;
|
||||||
|
style?: React.CSSProperties;
|
||||||
|
data: WorkflowRunModel;
|
||||||
|
};
|
||||||
|
|
||||||
|
const WorkflowRunDetail = ({ data, ...props }: WorkflowRunDetailProps) => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div {...props}>
|
||||||
|
<Show when={data.status === WORKFLOW_RUN_STATUSES.SUCCEEDED}>
|
||||||
|
<Alert showIcon type="success" message={<Typography.Text type="success">{t("workflow_run.props.status.succeeded")}</Typography.Text>} />
|
||||||
|
</Show>
|
||||||
|
|
||||||
|
<Show when={data.status === WORKFLOW_RUN_STATUSES.FAILED}>
|
||||||
|
<Alert showIcon type="error" message={<Typography.Text type="danger">{t("workflow_run.props.status.failed")}</Typography.Text>} />
|
||||||
|
</Show>
|
||||||
|
|
||||||
|
<div className="my-4 rounded-md bg-black p-4 text-stone-200">
|
||||||
|
<div className="flex flex-col space-y-4">
|
||||||
|
{data.logs?.map((item, i) => {
|
||||||
|
return (
|
||||||
|
<div key={i} className="flex flex-col space-y-2">
|
||||||
|
<div className="font-semibold">{item.nodeName}</div>
|
||||||
|
<div className="flex flex-col space-y-1">
|
||||||
|
{item.outputs?.map((output, j) => {
|
||||||
|
return (
|
||||||
|
<div key={j} className="flex space-x-2 text-sm" style={{ wordBreak: "break-word" }}>
|
||||||
|
<div className="whitespace-nowrap">[{dayjs(output.time).format("YYYY-MM-DD HH:mm:ss")}]</div>
|
||||||
|
{output.error ? <div className="text-red-500">{output.error}</div> : <div>{output.content}</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default WorkflowRunDetail;
|
@ -1,12 +1,12 @@
|
|||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { useControllableValue } from "ahooks";
|
import { useControllableValue } from "ahooks";
|
||||||
import { Alert, Drawer, Typography } from "antd";
|
import { Drawer } from "antd";
|
||||||
import dayjs from "dayjs";
|
|
||||||
|
|
||||||
import Show from "@/components/Show";
|
import Show from "@/components/Show";
|
||||||
import { WORKFLOW_RUN_STATUSES, type WorkflowRunModel } from "@/domain/workflowRun";
|
import { type WorkflowRunModel } from "@/domain/workflowRun";
|
||||||
import { useTriggerElement } from "@/hooks";
|
import { useTriggerElement } from "@/hooks";
|
||||||
|
|
||||||
|
import WorkflowRunDetail from "./WorkflowRunDetail";
|
||||||
|
|
||||||
export type WorkflowRunDetailDrawerProps = {
|
export type WorkflowRunDetailDrawerProps = {
|
||||||
data?: WorkflowRunModel;
|
data?: WorkflowRunModel;
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
@ -16,8 +16,6 @@ export type WorkflowRunDetailDrawerProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const WorkflowRunDetailDrawer = ({ data, loading, trigger, ...props }: WorkflowRunDetailDrawerProps) => {
|
const WorkflowRunDetailDrawer = ({ data, loading, trigger, ...props }: WorkflowRunDetailDrawerProps) => {
|
||||||
const { t } = useTranslation();
|
|
||||||
|
|
||||||
const [open, setOpen] = useControllableValue<boolean>(props, {
|
const [open, setOpen] = useControllableValue<boolean>(props, {
|
||||||
valuePropName: "open",
|
valuePropName: "open",
|
||||||
defaultValuePropName: "defaultOpen",
|
defaultValuePropName: "defaultOpen",
|
||||||
@ -30,37 +28,19 @@ const WorkflowRunDetailDrawer = ({ data, loading, trigger, ...props }: WorkflowR
|
|||||||
<>
|
<>
|
||||||
{triggerEl}
|
{triggerEl}
|
||||||
|
|
||||||
<Drawer destroyOnClose open={open} loading={loading} placement="right" title={`WorkflowRun #${data?.id}`} width={640} onClose={() => setOpen(false)}>
|
<Drawer
|
||||||
|
afterOpenChange={setOpen}
|
||||||
|
closable
|
||||||
|
destroyOnClose
|
||||||
|
open={open}
|
||||||
|
loading={loading}
|
||||||
|
placement="right"
|
||||||
|
title={`WorkflowRun #${data?.id}`}
|
||||||
|
width={640}
|
||||||
|
onClose={() => setOpen(false)}
|
||||||
|
>
|
||||||
<Show when={!!data}>
|
<Show when={!!data}>
|
||||||
<Show when={data!.status === WORKFLOW_RUN_STATUSES.SUCCEEDED}>
|
<WorkflowRunDetail data={data!} />
|
||||||
<Alert showIcon type="success" message={<Typography.Text type="success">{t("workflow_run.props.status.succeeded")}</Typography.Text>} />
|
|
||||||
</Show>
|
|
||||||
|
|
||||||
<Show when={data!.status === WORKFLOW_RUN_STATUSES.FAILED}>
|
|
||||||
<Alert showIcon type="error" message={<Typography.Text type="danger">{t("workflow_run.props.status.failed")}</Typography.Text>} />
|
|
||||||
</Show>
|
|
||||||
|
|
||||||
<div className="mt-4 rounded-md bg-black p-4 text-stone-200">
|
|
||||||
<div className="flex flex-col space-y-4">
|
|
||||||
{data!.logs?.map((item, i) => {
|
|
||||||
return (
|
|
||||||
<div key={i} className="flex flex-col space-y-2">
|
|
||||||
<div className="font-semibold">{item.nodeName}</div>
|
|
||||||
<div className="flex flex-col space-y-1">
|
|
||||||
{item.outputs?.map((output, j) => {
|
|
||||||
return (
|
|
||||||
<div key={j} className="flex space-x-2 text-sm" style={{ wordBreak: "break-word" }}>
|
|
||||||
<div className="whitespace-nowrap">[{dayjs(output.time).format("YYYY-MM-DD HH:mm:ss")}]</div>
|
|
||||||
{output.error ? <div className="text-red-500">{output.error}</div> : <div>{output.content}</div>}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Show>
|
</Show>
|
||||||
</Drawer>
|
</Drawer>
|
||||||
</>
|
</>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user