From 59497ba17f30be9ea5a5d3acb59988ec0bee0687 Mon Sep 17 00:00:00 2001 From: Ali Nehzat Date: Wed, 20 Sep 2023 15:05:51 +1000 Subject: [PATCH] add worker disable flag --- README.md | 14 +++++++++----- cmd/wkr-gh-sponsor/main.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0a64f7e..54177b8 100644 --- a/README.md +++ b/README.md @@ -7,20 +7,24 @@ This project is a standalone worker that continuosly: ### Command line flags ``` -wkr-gh-sponsor🐚 ➜ wkr-gh-sponsor git:(master) ./scripts/wkr-gh-sponsor --help -Usage: wkr-gh-sponsor --db-path="db.sql" --td-api-key=TD-API-KEY --gh-classic-access-token=GH-ACCESS-TOKEN --entities=ENTITIES,... +➜ wkr-gh-sponsor git:(master) ./scripts/wkr-gh-sponsor --help +Usage: wkr-gh-sponsor --db-path="db.sql" --td-api-url="https://api.thanks.dev/v1/deps" --td-api-key=TD-API-KEY --gh-classic-access-token=GH-ACCESS-TOKEN --entities=ENTITIES,... Flags: -h, --help Show context-sensitive help. -v, --version Print version and exit. -C, --config=FILE Config file ($CONFIG_PATH). - -e, --env=local Environment (local,prod) ($APP_ENV). - --db-path="db.sql" Path to db file. - --td-api-key=TD-API-KEY Api key for thanks.dev ($TD_API_KEY). + --db-path="db.sql" Path to db file ($DB_PATH). + --td-api-url="https://api.thanks.dev/v1/deps" + thanks.dev API URL ($TD_API_URL). + --td-api-key=TD-API-KEY thanks.dev API key ($TD_API_KEY). --gh-classic-access-token=GH-ACCESS-TOKEN GitHub classis access token with admin:org & user scopes ($GH_CLASSIC_ACCESS_TOKEN). --entities=ENTITIES,... The GitHub entities to process sponsorships for. First entity in the list is considered DEFAULT. + --wkr-td-disabled Disable worker. + --wkr-animate-disabled Disable worker. + --wkr-donate-disabled Disable worker. Observability: --log-level=info Log level (trace,debug,info,warning,error,fatal,panic). diff --git a/cmd/wkr-gh-sponsor/main.go b/cmd/wkr-gh-sponsor/main.go index e8e87f3..4075a49 100644 --- a/cmd/wkr-gh-sponsor/main.go +++ b/cmd/wkr-gh-sponsor/main.go @@ -26,6 +26,10 @@ import ( // Populated during build. var GitCommit string = "dev" +type wkr_config struct { + Disabled bool `help:"Disable worker." default:"false"` +} + var cli struct { Version kong.VersionFlag `short:"v" help:"Print version and exit."` Config kong.ConfigFlag `short:"C" help:"Config file." placeholder:"FILE" env:"CONFIG_PATH"` @@ -39,6 +43,10 @@ var cli struct { GhClassicAccessToken wkrghsponsor.GhAccessToken `help:"GitHub classis access token with admin:org & user scopes." required:"" env:"GH_CLASSIC_ACCESS_TOKEN"` Entities []wkrghsponsor.Entity `help:"The GitHub entities to process sponsorships for. First entity in the list is considered DEFAULT." required:""` + + WkrTd wkr_config `embed:"" prefix:"wkr-td-"` + WkrAnimate wkr_config `embed:"" prefix:"wkr-animate-"` + WkrDonate wkr_config `embed:"" prefix:"wkr-donate-"` } func main() { @@ -103,21 +111,25 @@ func main() { wkrs := []struct { name string fn any + disabled bool workInterval time.Duration }{ { name: "wkr-td", fn: wkrtd.New, + disabled: cli.WkrTd.Disabled, workInterval: time.Hour * 3, // 4 times a day }, { name: "wkr-animate", fn: wkranimate.New, + disabled: cli.WkrAnimate.Disabled, workInterval: time.Minute, // once a minute }, { name: "wkr-donate", fn: wkrdonate.New, + disabled: cli.WkrDonate.Disabled, workInterval: time.Minute, // once a minute }, } @@ -127,6 +139,11 @@ func main() { logger := logger.WithField("name", w.name) ctx := log.LoggerContext(ctx, logger) + if w.disabled { + logger.Infof("Worker %s not enabled", w.name) + continue + } + out, err := kctx.Call(w.fn) if err != nil { logger.WithError(err).Error("failed to start")