Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
fx.Provide(logger.NewSlogLogger),
fx.Provide(client.NewDataStoreFactory),
)
cfgInstance *config.Config = nil
cfgInstance *config.Config
)

func ConfigProvider() *config.Config {
Expand Down
12 changes: 7 additions & 5 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package cmd

import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/dagu-dev/dagu/internal/agent"
"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/dag"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"syscall"
)

func execDAG(ctx context.Context, e engine.Engine, cmd *cobra.Command, args []string, dry bool) {
Expand All @@ -23,7 +24,7 @@ func execDAG(ctx context.Context, e engine.Engine, cmd *cobra.Command, args []st

err = start(ctx, e, loadedDAG, dry)
if err != nil {
log.Fatalf("Failed to start DAG: %v", err)
log.Fatalf("Failed to start DAG: %v", err) // nolint // deep-exit
}
}

Expand Down Expand Up @@ -57,6 +58,7 @@ func listenSignals(ctx context.Context, a signalListener) {
}()
}

// nolint // deep-exit
func checkError(err error) {
if err != nil {
log.Fatal(err)
Expand Down
18 changes: 9 additions & 9 deletions cmd/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ func restartCmd() *cobra.Command {
}
}

func stopDAGIfRunning(e engine.Engine, dag *dag.DAG) {
st, err := e.GetCurrentStatus(dag)
func stopDAGIfRunning(e engine.Engine, d *dag.DAG) {
st, err := e.GetCurrentStatus(d)
checkError(err)

// Stop the DAG if it is running.
if st.Status == scheduler.StatusRunning {
log.Printf("Stopping %s for restart...", dag.Name)
cobra.CheckErr(stopRunningDAG(e, dag))
log.Printf("Stopping %s for restart...", d.Name)
cobra.CheckErr(stopRunningDAG(e, d))
}
}

func stopRunningDAG(e engine.Engine, dag *dag.DAG) error {
func stopRunningDAG(e engine.Engine, d *dag.DAG) error {
for {
st, err := e.GetCurrentStatus(dag)
st, err := e.GetCurrentStatus(d)
checkError(err)

if st.Status != scheduler.StatusRunning {
return nil
}
checkError(e.Stop(dag))
checkError(e.Stop(d))
time.Sleep(time.Millisecond * 100)
}
}
Expand All @@ -78,8 +78,8 @@ func waitForRestart(restartWait time.Duration) {
}
}

func getPreviousExecutionParams(e engine.Engine, dag *dag.DAG) string {
st, err := e.GetLatestStatus(dag)
func getPreviousExecutionParams(e engine.Engine, d *dag.DAG) string {
st, err := e.GetLatestStatus(d)
checkError(err)

return st.Params
Expand Down
7 changes: 4 additions & 3 deletions cmd/restart_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"os"
"testing"
"time"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/engine"
"github.com/dagu-dev/dagu/internal/persistence/client"
"github.com/dagu-dev/dagu/internal/scheduler"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)

func TestRestartCommand(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cmd

import (
"fmt"
"github.com/dagu-dev/dagu/internal/scheduler"
"github.com/stretchr/testify/require"
"os"
"testing"

"github.com/dagu-dev/dagu/internal/scheduler"
"github.com/stretchr/testify/require"
)

func TestRetryCommand(t *testing.T) {
Expand Down
7 changes: 2 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ const legacyPath = ".dagu"

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
func Execute() error {
return rootCmd.Execute()
}

func init() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"github.com/stretchr/testify/require"
"net"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestServerCommand(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/start_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func startAllCmd() *cobra.Command {
config.Get().DAGs = getFlagString(cmd, "dags", config.Get().DAGs)
err := core.NewScheduler(app.TopLevelModule).Start(cmd.Context())
if err != nil {
log.Fatal(err)
log.Fatal(err) // nolint // deep-exit
}
}()

Expand Down
3 changes: 2 additions & 1 deletion cmd/status_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"github.com/dagu-dev/dagu/internal/scheduler"
"os"
"testing"
"time"

"github.com/dagu-dev/dagu/internal/scheduler"
)

func TestStatusCommand(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/stop_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"github.com/dagu-dev/dagu/internal/scheduler"
"os"
"testing"
"time"

"github.com/dagu-dev/dagu/internal/scheduler"
)

func TestStopCommand(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/version_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmd

import (
"github.com/dagu-dev/dagu/internal/constants"
"testing"

"github.com/dagu-dev/dagu/internal/constants"
)

func TestVersionCommand(t *testing.T) {
Expand Down
31 changes: 18 additions & 13 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import (
"github.com/google/uuid"
)

var (
errFailedStartSocketFrontend = errors.New("failed to start the socket frontend")
errDAGAlreadyRunning = errors.New("the DAG is already running")
)

// Agent is the interface to run / cancel / signal / status / etc.
type Agent struct {
*Config
Expand Down Expand Up @@ -151,21 +156,24 @@ func (a *Agent) signal(sig os.Signal, allowOverride bool) {
go func() {
a.scheduler.Signal(a.graph, sig, done, allowOverride)
}()
timeout := time.After(a.DAG.MaxCleanUpTime)
tick := time.After(time.Second * 5)
timeout := time.NewTimer(a.DAG.MaxCleanUpTime)
tick := time.NewTimer(time.Second * 5)
defer timeout.Stop()
defer tick.Stop()

for {
select {
case <-done:
log.Printf("All child processes have been terminated.")
return
case <-timeout:
case <-timeout.C:
log.Printf("Time reached to max cleanup time")
a.Kill()
return
case <-tick:
case <-tick.C:
log.Printf("Sending signal again")
a.scheduler.Signal(a.graph, sig, nil, false)
tick = time.After(time.Second * 5)
tick.Reset(time.Second * 5)
default:
log.Printf("Waiting for child processes to exit...")
time.Sleep(time.Second * 3)
Expand Down Expand Up @@ -252,10 +260,8 @@ func (a *Agent) setupDatabase() error {
if err := a.historyStore.RemoveOld(a.DAG.Location, a.DAG.HistRetentionDays); err != nil {
utils.LogErr("clean old history data", err)
}
if err := a.historyStore.Open(a.DAG.Location, time.Now(), a.requestId); err != nil {
return err
}
return nil

return a.historyStore.Open(a.DAG.Location, time.Now(), a.requestId)
}

func (a *Agent) setupSocketServer() (err error) {
Expand Down Expand Up @@ -299,7 +305,7 @@ func (a *Agent) run(ctx context.Context) error {
listen := make(chan error)
go func() {
err := a.socketServer.Serve(listen)
if err != nil && err != sock.ErrServerRequestedShutdown {
if err != nil && !errors.Is(err, sock.ErrServerRequestedShutdown) {
log.Printf("failed to start socket frontend %v", err)
}
}()
Expand All @@ -309,7 +315,7 @@ func (a *Agent) run(ctx context.Context) error {
}()

if err := <-listen; err != nil {
return fmt.Errorf("failed to start the socket frontend")
return errFailedStartSocketFrontend
}

done := make(chan *scheduler.Node)
Expand Down Expand Up @@ -380,8 +386,7 @@ func (a *Agent) checkIsRunning() error {
return err
}
if status.Status != scheduler.StatusNone {
return fmt.Errorf("the DAG is already running. socket=%s",
a.DAG.SockAddr())
return fmt.Errorf("%w. socket=%s", errDAGAlreadyRunning, a.DAG.SockAddr())
}
return nil
}
Expand Down
15 changes: 10 additions & 5 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package agent_test

import (
"context"
"github.com/dagu-dev/dagu/internal/agent"
"github.com/dagu-dev/dagu/internal/persistence"
"github.com/dagu-dev/dagu/internal/persistence/client"
"net/http"
"net/url"
"os"
Expand All @@ -13,6 +10,10 @@ import (
"testing"
"time"

"github.com/dagu-dev/dagu/internal/agent"
"github.com/dagu-dev/dagu/internal/persistence"
"github.com/dagu-dev/dagu/internal/persistence/client"

"github.com/dagu-dev/dagu/internal/config"
"github.com/dagu-dev/dagu/internal/dag"
"github.com/dagu-dev/dagu/internal/engine"
Expand Down Expand Up @@ -274,7 +275,9 @@ func TestHandleHTTP(t *testing.T) {
require.NoError(t, err)
}()

<-time.After(time.Millisecond * 50)
timer := time.NewTimer(time.Millisecond * 50)
defer timer.Stop()
<-timer.C

var mockResponseWriter = mockResponseWriter{}

Expand Down Expand Up @@ -314,7 +317,9 @@ func TestHandleHTTP(t *testing.T) {
require.Equal(t, http.StatusOK, mockResponseWriter.status)
require.Equal(t, "OK", mockResponseWriter.body)

<-time.After(time.Millisecond * 50)
timer2 := time.NewTimer(time.Millisecond * 50)
defer timer2.Stop()
<-timer2.C

status = a.Status()
require.Equal(t, status.Status, scheduler.StatusCancel)
Expand Down
Loading