diff options
author | Amine <hilalyamine@gmail.com> | 2019-08-19 00:37:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-19 00:37:54 +0200 |
commit | 36c91adddfc16b8c5d04eb66dbb4cf8c25cea321 (patch) | |
tree | 50484f3531e33c2c63c026fcb1c76f60f8b13862 /commands | |
parent | 6428352bd14828f670206b60862de7f71c52d235 (diff) | |
parent | e6931aaf6f3173c634b03d515287e4a12fd20f15 (diff) | |
download | git-bug-36c91adddfc16b8c5d04eb66dbb4cf8c25cea321.tar.gz |
Merge pull request #190 from MichaelMure/bridge-ctx
Bridge: pull/push enhancements
Diffstat (limited to 'commands')
-rw-r--r-- | commands/bridge_pull.go | 55 | ||||
-rw-r--r-- | commands/bridge_push.go | 52 |
2 files changed, 100 insertions, 7 deletions
diff --git a/commands/bridge_pull.go b/commands/bridge_pull.go index 2edabfaf..2bdd24c3 100644 --- a/commands/bridge_pull.go +++ b/commands/bridge_pull.go @@ -1,6 +1,10 @@ package commands import ( + "context" + "fmt" + "os" + "sync" "time" "github.com/spf13/cobra" @@ -31,12 +35,61 @@ func runBridgePull(cmd *cobra.Command, args []string) error { return err } + parentCtx := context.Background() + ctx, cancel := context.WithCancel(parentCtx) + defer cancel() + + // buffered channel to avoid send block at the end + done := make(chan struct{}, 1) + + var mu sync.Mutex + interruptCount := 0 + interrupt.RegisterCleaner(func() error { + mu.Lock() + if interruptCount > 0 { + fmt.Println("Received another interrupt before graceful stop, terminating...") + os.Exit(0) + } + + interruptCount++ + mu.Unlock() + + fmt.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)") + + // send signal to stop the importer + cancel() + + // block until importer gracefully shutdown + <-done + return nil + }) + // TODO: by default import only new events - err = b.ImportAll(time.Time{}) + events, err := b.ImportAll(ctx, time.Time{}) if err != nil { return err } + importedIssues := 0 + importedIdentities := 0 + for result := range events { + if result.Event != core.ImportEventNothing { + fmt.Println(result.String()) + } + + switch result.Event { + case core.ImportEventBug: + importedIssues++ + case core.ImportEventIdentity: + importedIdentities++ + } + } + + // send done signal + close(done) + + fmt.Printf("Successfully imported %d issues and %d identities with %s bridge\n", importedIssues, importedIdentities, b.Name) + return nil } diff --git a/commands/bridge_push.go b/commands/bridge_push.go index 11f5ca82..a56f9bc3 100644 --- a/commands/bridge_push.go +++ b/commands/bridge_push.go @@ -1,7 +1,10 @@ package commands import ( + "context" "fmt" + "os" + "sync" "time" "github.com/spf13/cobra" @@ -32,20 +35,57 @@ func runBridgePush(cmd *cobra.Command, args []string) error { return err } + parentCtx := context.Background() + ctx, cancel := context.WithCancel(parentCtx) + defer cancel() + + done := make(chan struct{}, 1) + + var mu sync.Mutex + interruptCount := 0 + interrupt.RegisterCleaner(func() error { + mu.Lock() + if interruptCount > 0 { + fmt.Println("Received another interrupt before graceful stop, terminating...") + os.Exit(0) + } + + interruptCount++ + mu.Unlock() + + fmt.Println("Received interrupt signal, stopping the import...\n(Hit ctrl-c again to kill the process.)") + + // send signal to stop the importer + cancel() + + // block until importer gracefully shutdown + <-done + return nil + }) + // TODO: by default export only new events - out, err := b.ExportAll(time.Time{}) + events, err := b.ExportAll(ctx, time.Time{}) if err != nil { return err } - for result := range out { - if result.Err != nil { - fmt.Println(result.Err, result.Reason) - } else { - fmt.Printf("%s: %s\n", result.String(), result.ID) + exportedIssues := 0 + for result := range events { + if result.Event != core.ExportEventNothing { + fmt.Println(result.String()) + } + + switch result.Event { + case core.ExportEventBug: + exportedIssues++ } } + // send done signal + close(done) + + fmt.Printf("Successfully exported %d issues with %s bridge\n", exportedIssues, b.Name) + return nil } |