handle exit logic

This commit is contained in:
yoan 2025-01-10 07:25:09 +08:00
parent ebffac7ba4
commit db10ed8378
4 changed files with 32 additions and 8 deletions

View File

@ -94,16 +94,16 @@ func (w *WorkflowRepository) SaveRun(ctx context.Context, run *domain.WorkflowRu
}
// unable trigger sse using DB()
wordflowRecord, err := txDao.FindRecordById("workflow", run.WorkflowId)
workflowRecord, err := txDao.FindRecordById("workflow", run.WorkflowId)
if err != nil {
return err
}
wordflowRecord.Set("lastRunId", record.GetId())
wordflowRecord.Set("lastRunStatus", record.GetString("status"))
wordflowRecord.Set("lastRunTime", record.GetString("startedAt"))
workflowRecord.Set("lastRunId", record.GetId())
workflowRecord.Set("lastRunStatus", record.GetString("status"))
workflowRecord.Set("lastRunTime", record.GetString("startedAt"))
return txDao.SaveRecord(wordflowRecord)
return txDao.SaveRecord(workflowRecord)
})
if err != nil {
return err

View File

@ -10,6 +10,7 @@ import (
type WorkflowService interface {
Run(ctx context.Context, req *domain.WorkflowRunReq) error
Stop()
}
type workflowHandler struct {

View File

@ -1,6 +1,8 @@
package routes
import (
"sync"
"github.com/usual2970/certimate/internal/notify"
"github.com/usual2970/certimate/internal/repository"
"github.com/usual2970/certimate/internal/rest"
@ -11,14 +13,26 @@ import (
"github.com/pocketbase/pocketbase/apis"
)
var (
workflowSvc rest.WorkflowService
workflowSvcOnce sync.Once
)
func getWorkflowService() rest.WorkflowService {
workflowSvcOnce.Do(func() {
workflowRepo := repository.NewWorkflowRepository()
workflowSvc = workflow.NewWorkflowService(workflowRepo)
})
return workflowSvc
}
func Register(e *echo.Echo) {
group := e.Group("/api", apis.RequireAdminAuth())
notifyRepo := repository.NewSettingsRepository()
notifySvc := notify.NewNotifyService(notifyRepo)
workflowRepo := repository.NewWorkflowRepository()
workflowSvc := workflow.NewWorkflowService(workflowRepo)
workflowSvc := getWorkflowService()
statisticsRepo := repository.NewStatisticsRepository()
statisticsSvc := statistics.NewStatisticsService(statisticsRepo)
@ -29,3 +43,7 @@ func Register(e *echo.Echo) {
rest.NewStatisticsHandler(group, statisticsSvc)
}
func Unregister() {
getWorkflowService().Stop()
}

View File

@ -55,7 +55,12 @@ func main() {
return nil
})
defer log.Println("Exit!")
app.OnTerminate().Add(func(e *core.TerminateEvent) error {
routes.Unregister()
log.Println("Exit!")
return nil
})
log.Printf("Visit the website: http://%s", httpFlag)
if err := app.Start(); err != nil {