mirror of
https://github.com/MontFerret/ferret.git
synced 2025-03-03 15:02:32 +02:00
Fixed graceful process termination (#240)
This commit is contained in:
parent
63dfcdeefd
commit
b702f127ed
13
cli/exec.go
13
cli/exec.go
@ -9,7 +9,6 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func ExecFile(pathToFile string, opts Options) {
|
||||
@ -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 {
|
||||
|
@ -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(),
|
||||
)
|
||||
|
||||
ctx = drivers.WithContext(
|
||||
ctx,
|
||||
cdp.NewDriver(
|
||||
cdp.WithAddress(opts.Cdp),
|
||||
cdp.WithProxy(opts.Proxy),
|
||||
cdp.WithUserAgent(opts.UserAgent),
|
||||
),
|
||||
cdpDriver := cdp.NewDriver(
|
||||
cdp.WithAddress(opts.Cdp),
|
||||
cdp.WithProxy(opts.Proxy),
|
||||
cdp.WithUserAgent(opts.UserAgent),
|
||||
)
|
||||
|
||||
return ctx, nil
|
||||
ctx = drivers.WithContext(
|
||||
ctx,
|
||||
cdpDriver,
|
||||
)
|
||||
|
||||
return context.WithCancel(ctx)
|
||||
}
|
||||
|
16
cli/repl.go
16
cli/repl.go
@ -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"
|
||||
@ -50,18 +48,10 @@ func Repl(version 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)
|
||||
|
||||
exit := func() {
|
||||
cancel()
|
||||
|
@ -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 {
|
||||
@ -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")
|
||||
}
|
||||
}()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user