forked from raystack/entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Femi Novia Lina
committed
Aug 13, 2024
1 parent
136b628
commit 4895f60
Showing
8 changed files
with
129 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/goto/entropy/core" | ||
"github.com/goto/entropy/core/module" | ||
"github.com/goto/entropy/pkg/logger" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func cmdWorker() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "worker", | ||
Short: "Start workers", | ||
Example: heredoc.Doc(` | ||
$ entropy worker --count 2 --scope project=example-project,example-project-2 | ||
`), | ||
Annotations: map[string]string{ | ||
"group:other": "server", | ||
}, | ||
} | ||
|
||
cmd.RunE = handleErr(func(cmd *cobra.Command, args []string) error { | ||
cfg, err := loadConfig(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = logger.Setup(&cfg.Log) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store := setupStorage(cfg.PGConnStr, cfg.Syncer, cfg.Service) | ||
moduleService := module.NewService(setupRegistry(), store) | ||
resourceService := core.New(store, moduleService, time.Now, cfg.Syncer.SyncBackoffInterval, cfg.Syncer.MaxRetries) | ||
|
||
var wg *sync.WaitGroup | ||
go func() { | ||
wg = spawnWorkers(cmd.Context(), resourceService, cfg.Syncer.WorkerModules, cfg.Syncer.SyncInterval) | ||
}() | ||
|
||
quitChannel := make(chan os.Signal, 1) | ||
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM) | ||
<-quitChannel | ||
|
||
wg.Wait() | ||
zap.L().Info("all syncer workers exited") | ||
|
||
return nil | ||
}) | ||
|
||
return cmd | ||
} | ||
|
||
func spawnWorkers(ctx context.Context, resourceService *core.Service, workerModules []workerModule, syncInterval time.Duration) (wg *sync.WaitGroup) { | ||
if len(workerModules) == 0 { | ||
wg = resourceService.RunSyncer(ctx, 1, syncInterval, map[string][]string{}) | ||
} else { | ||
for _, module := range workerModules { | ||
wg = resourceService.RunSyncer(ctx, module.Count, syncInterval, module.Scope) | ||
} | ||
} | ||
return | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters