Skip to content

Commit

Permalink
Refactor two common function to util file
Browse files Browse the repository at this point in the history
  • Loading branch information
Terryhung committed Oct 6, 2023
1 parent d6c4271 commit 47011e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
45 changes: 4 additions & 41 deletions commands/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/filecoin-project/go-state-types/abi"
Expand All @@ -18,10 +15,8 @@ import (
"github.com/urfave/cli/v2"

"github.com/filecoin-project/lily/chain/actors/builtin"
"github.com/filecoin-project/lily/config"
"github.com/filecoin-project/lily/model"
"github.com/filecoin-project/lily/model/blocks"
"github.com/filecoin-project/lily/storage"

"github.com/filecoin-project/lily/lens/lily"
)
Expand Down Expand Up @@ -311,44 +306,12 @@ var SyncIncomingBlockCmd = &cli.Command{
defer closer()

// Set up a context that is canceled when the command is interrupted
ctx, cancel := context.WithCancel(ctx)
ctx, cancel := SetupContextWithCancel(ctx)
defer cancel()

// Set up a signal handler to cancel the context
go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-interrupt:
cancel()
case <-ctx.Done():
}
}()

// values that may be accessed if user wants to persist to Storage
var strg model.Storage

if syncFlags.storage != "" {
cfg, err := config.FromFile(syncFlags.config)
if err != nil {
return err
}

md := storage.Metadata{
JobName: syncFlags.storage,
}

// context for db connection
ctxDB := context.Background()

sc, err := storage.NewCatalog(cfg.Storage)
if err != nil {
return err
}
strg, err = sc.Connect(ctxDB, syncFlags.storage, md)
if err != nil {
return err
}
strg, err := SetupStorage(syncFlags.config, syncFlags.storage)
if err != nil {
return err
}

state := &SyncingState{
Expand Down
50 changes: 50 additions & 0 deletions commands/util.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package commands

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
"syscall"

"github.com/urfave/cli/v2"

"github.com/filecoin-project/lily/config"
"github.com/filecoin-project/lily/model"
"github.com/filecoin-project/lily/schedule"
"github.com/filecoin-project/lily/storage"
)

func FlagSet(fs ...[]cli.Flag) []cli.Flag {
Expand All @@ -30,3 +37,46 @@ func PrintNewJob(w io.Writer, res *schedule.JobSubmitResult) error {
}
return nil
}

func SetupContextWithCancel(ctx context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(ctx)

go func() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-interrupt:
cancel()
case <-ctx.Done():
}
}()

return ctx, cancel
}

func SetupStorage(configPath string, storageStr string) (strg model.Storage, err error) {
if storageStr != "" {
cfg, err := config.FromFile(configPath)
if err != nil {
return nil, err
}

md := storage.Metadata{
JobName: storageStr,
}

// context for db connection
ctxDB := context.Background()

sc, err := storage.NewCatalog(cfg.Storage)
if err != nil {
return nil, err
}
strg, err = sc.Connect(ctxDB, syncFlags.storage, md)
if err != nil {
return nil, err
}
}

return strg, nil
}

0 comments on commit 47011e0

Please sign in to comment.