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

Fixed graceful process termination #240

Merged
merged 1 commit into from
Feb 26, 2019
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
13 changes: 2 additions & 11 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"os"
"os/signal"
"syscall"
)

func ExecFile(pathToFile string, opts Options) {
Expand Down Expand Up @@ -38,18 +37,10 @@ func Exec(query string, opts Options) {

l := NewLogger()

ctx, err := opts.WithContext(context.Background())
ctx, cancel := opts.WithContext(context.Background())

if err != nil {
fmt.Println("Failed to register HTML drivers")
fmt.Println(err)
os.Exit(1)
return
}

ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
signal.Notify(c, os.Interrupt)

go func() {
for {
Expand Down
26 changes: 15 additions & 11 deletions cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ type Options struct {
ShowTime bool
}

func (opts Options) WithContext(ctx context.Context) (context.Context, error) {
func (opts Options) WithContext(ctx context.Context) (context.Context, context.CancelFunc) {
httpDriver := http.NewDriver(
http.WithProxy(opts.Proxy),
http.WithUserAgent(opts.UserAgent),
)

ctx = drivers.WithContext(
ctx,
http.NewDriver(
http.WithProxy(opts.Proxy),
http.WithUserAgent(opts.UserAgent),
),
httpDriver,
drivers.AsDefault(),
)

cdpDriver := cdp.NewDriver(
cdp.WithAddress(opts.Cdp),
cdp.WithProxy(opts.Proxy),
cdp.WithUserAgent(opts.UserAgent),
)

ctx = drivers.WithContext(
ctx,
cdp.NewDriver(
cdp.WithAddress(opts.Cdp),
cdp.WithProxy(opts.Proxy),
cdp.WithUserAgent(opts.UserAgent),
),
cdpDriver,
)

return ctx, nil
return context.WithCancel(ctx)
}
16 changes: 3 additions & 13 deletions cli/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package cli
import (
"context"
"fmt"
"github.com/MontFerret/ferret/pkg/parser/fql"
"os"
"os/signal"
"strings"
"syscall"

"github.com/MontFerret/ferret/pkg/parser/fql"

"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime"
Expand Down Expand Up @@ -50,18 +48,10 @@ func Repl(version string, opts Options) {

l := NewLogger()

ctx, err := opts.WithContext(context.Background())

if err != nil {
fmt.Println("Failed to register HTML drivers")
fmt.Println(err)
os.Exit(1)
return
}
ctx, cancel := opts.WithContext(context.Background())

ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
signal.Notify(c, os.Interrupt)

exit := func() {
cancel()
Expand Down
6 changes: 4 additions & 2 deletions pkg/runtime/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package runtime

import (
"context"
"runtime"

"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/pkg/errors"
"runtime"
)

type Program struct {
Expand Down Expand Up @@ -61,12 +62,13 @@ func (p *Program) Run(ctx context.Context, setters ...Option) (result []byte, er
}()

scope, closeFn := core.NewRootScope()

defer func() {
if err := closeFn(); err != nil {
logger.Error().
Timestamp().
Err(err).
Msg("Closing root scope")
Msg("closing root scope")
}
}()

Expand Down