Skip to content

Commit

Permalink
Merge pull request #50 from kasaikou/fix/graceful-shutdown
Browse files Browse the repository at this point in the history
graceful shutdown
  • Loading branch information
kasaikou committed Mar 1, 2024
2 parents cfcf701 + 6b56d49 commit e4dffae
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
24 changes: 24 additions & 0 deletions cmd/docstak/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"context"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"

"github.com/kasaikou/docstak/app"
"github.com/kasaikou/docstak/cli"
Expand Down Expand Up @@ -53,6 +55,28 @@ func run() int {
chDecoration <- cli.ProcessOutputDecorations[i]
}

ctx, cancel := context.WithCancel(ctx)

sigWaiter := sync.WaitGroup{}
sigWaiter.Add(1)
go func() {
defer sigWaiter.Done()
sig := make(chan os.Signal, 1)
defer close(sig)

signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
select {
case <-ctx.Done():
return
case signal := <-sig:
logger.Warn("signal received, make graceful shutdown started", slog.String("signal", signal.String()))
cancel()
return
}
}()
defer sigWaiter.Wait()
defer cancel()

exit := docstak.ExecuteContext(ctx, document.Document,
docstak.ExecuteOptCalls(Cmds...),
docstak.ExecuteOptProcessExec(func(ctx context.Context, task model.DocumentTask, runner *srun.ScriptRunner) (int, error) {
Expand Down
14 changes: 13 additions & 1 deletion docstak.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ previous: [ci/fmt, ci/depends, ci/coverage-test]

### ci/depends

```yaml:docstak.yml
skips:
file:
not-changed: ["**.go", "go.sum", "go.mod"]
```

```sh
go mod tidy && git diff --no-patch --exit-code go.sum
```
Expand All @@ -68,11 +74,17 @@ gofmt -l .
### ci/coverage-test

```yaml:docstak.yml
previous: [ci/coverage-test/go]
previous: [ci/coverage-test/go, download]
```

#### ci/coverage-test/go

```yaml:docstak.yml
skips:
file:
not-changed: ["**.go", "go.sum", "go.mod"]
```

```sh
go test -coverprofile=coverage.txt -covermode=atomic ./...
```
13 changes: 8 additions & 5 deletions docstak/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func ExecuteContext(ctx context.Context, document model.Document, options ...Exe

func executeTasks(ctx context.Context, document model.Document, option *executeOptions, executeTasks []string) int {
wg := sync.WaitGroup{}
defer wg.Wait()

type taskResp struct {
Call string
Expand Down Expand Up @@ -137,19 +136,22 @@ func executeTasks(ctx context.Context, document model.Document, option *executeO

ch := make(chan taskResp)
wg := sync.WaitGroup{}
defer wg.Wait()
for j := range task.Scripts {
wg.Add(1)
go func(ctx context.Context, task model.DocumentTask, script model.DocumentTaskScript, chRes chan<- taskResp) {
defer wg.Done()
exit := executeTask(ctx, task, script, option)
chRes <- taskResp{
Call: task.Call,
Exit: exit,

if ctx.Err() == nil {
chRes <- taskResp{
Call: task.Call,
Exit: exit,
}
}

}(ctx, task, task.Scripts[j], ch)
}
defer wg.Wait()

ended := 0

Expand Down Expand Up @@ -178,6 +180,7 @@ func executeTasks(ctx context.Context, document model.Document, option *executeO

taskChs = append(taskChs, ch)
}
defer wg.Wait()
ended := 0

for i := range taskChs {
Expand Down
12 changes: 7 additions & 5 deletions docstak/files/markdown/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ func setDocumentTask(ctx context.Context, document *model.DocumentConfig, result
config.Requires.ExistPaths = result.Config.Requires.File.Exists

config.Skips.ExistPaths = result.Config.Skips.File.Exists
config.Skips.NotChangedPaths = append(config.Skips.NotChangedPaths, model.TaskFileNotChangedCondition{
Paths: map[string]struct{}{},
})
for i := range result.Config.Skips.File.NotChangeds {
config.Skips.NotChangedPaths[0].Paths[result.Config.Skips.File.NotChangeds[i]] = struct{}{}
if len(result.Config.Skips.File.NotChangeds) > 0 {
config.Skips.NotChangedPaths = append(config.Skips.NotChangedPaths, model.TaskFileNotChangedCondition{
Paths: map[string]struct{}{},
})
for i := range result.Config.Skips.File.NotChangeds {
config.Skips.NotChangedPaths[0].Paths[result.Config.Skips.File.NotChangeds[i]] = struct{}{}
}
}

for i := range result.Commands {
Expand Down
19 changes: 10 additions & 9 deletions docstak/files/statefile/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ func SaveLocalFile(filename string, s State) error {
func SetStateParsed(result State) model.NewDocumentOption {
return func(ctx context.Context, d *model.DocumentConfig) error {

for call, task := range result.Tasks {
config := d.Document.Tasks[call]

for _, file := range task.Files {
for j := range config.Skips.NotChangedPaths {
if config.Skips.NotChangedPaths[j].IsEqualRule(file.Rule.Paths, file.Rule.Ignores) {
config.Skips.NotChangedPaths[j].MD5 = file.MD5
for call, taskStates := range result.Tasks {
config, exist := d.Document.Tasks[call]

if exist {
for _, file := range taskStates.Files {
for j := range config.Skips.NotChangedPaths {
if config.Skips.NotChangedPaths[j].IsEqualRule(file.Rule.Paths, file.Rule.Ignores) {
config.Skips.NotChangedPaths[j].MD5 = file.MD5
}
}
}
d.Document.Tasks[call] = config
}

d.Document.Tasks[call] = config
}

return nil
Expand Down

0 comments on commit e4dffae

Please sign in to comment.