Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added graceful shutdown for ctrl-C #123

Merged
merged 6 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 31 additions & 1 deletion cmd/arcaflow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"

Expand Down Expand Up @@ -164,13 +165,21 @@ Options:

func runWorkflow(flow engine.WorkflowEngine, dirContext map[string][]byte, workflowFile string, logger log.Logger, inputData []byte) int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctrlC := make(chan os.Signal, 1)
jaredoconnell marked this conversation as resolved.
Show resolved Hide resolved
signal.Notify(ctrlC, os.Interrupt)

go handleOSInterrupt(ctrlC, cancel, logger)
defer func() {
close(ctrlC) // Ensure that the goroutine exits
cancel()
}()

workflow, err := flow.Parse(dirContext, workflowFile)
if err != nil {
logger.Errorf("Invalid workflow (%v)", err)
return ExitCodeInvalidData
}

outputID, outputData, outputError, err := workflow.Run(ctx, inputData)
if err != nil {
logger.Errorf("Workflow execution failed (%v)", err)
Expand All @@ -193,6 +202,27 @@ func runWorkflow(flow engine.WorkflowEngine, dirContext map[string][]byte, workf
return ExitCodeOK
}

func handleOSInterrupt(ctrlC chan os.Signal, cancel context.CancelFunc, logger log.Logger) {
for i := 1; true; i++ {
sysSignal, ok := <-ctrlC
if !ok || sysSignal != os.Interrupt {
return
}
jaredoconnell marked this conversation as resolved.
Show resolved Hide resolved
switch {
case i <= 1:
logger.Infof("Requesting graceful shutdown.")
// Request graceful shutdown
cancel()
case i == 2:
logger.Warningf("Hit CTRL-C again to forcefully exit workflow without cleanup. You may need to manually delete pods or containers.")
default:
logger.Warningf("Force exiting. You may need to manually delete pods or containers.")
// Second request. Exit now.
os.Exit(1)
}
}
}
jaredoconnell marked this conversation as resolved.
Show resolved Hide resolved

func loadContext(dir string) (map[string][]byte, error) {
absDir, err := filepath.Abs(dir)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ type workflowEngine struct {
config *config.Config
}

func (w workflowEngine) RunWorkflow(ctx context.Context, input []byte, workflowContext map[string][]byte, workflowFileName string) (outputID string, outputData any, outputError bool, err error) {
func (w workflowEngine) RunWorkflow(
ctx context.Context,
input []byte,
workflowContext map[string][]byte,
workflowFileName string,
) (outputID string, outputData any, outputError bool, err error) {
wf, err := w.Parse(workflowContext, workflowFileName)
if err != nil {
return "", nil, true, err
Expand Down Expand Up @@ -126,7 +131,10 @@ type engineWorkflow struct {
workflow workflow.ExecutableWorkflow
}

func (e engineWorkflow) Run(ctx context.Context, input []byte) (outputID string, outputData any, outputIsError bool, err error) {
func (e engineWorkflow) Run(
ctx context.Context,
input []byte,
) (outputID string, outputData any, outputIsError bool, err error) {
decodedInput, err := yaml.New().Parse(input)
if err != nil {
return "", nil, true, fmt.Errorf("failed to YAML decode input (%w)", err)
Expand Down