Skip to content

Commit 602a15a

Browse files
committed
make parallel tasks easier
1 parent e602c5d commit 602a15a

1 file changed

Lines changed: 67 additions & 66 deletions

File tree

cmd/keymaster/main.go

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -180,41 +180,17 @@ If no account is specified, deploys to all active accounts in the database.`,
180180
targetAccounts = allAccounts
181181
}
182182

183-
if len(targetAccounts) == 0 {
184-
fmt.Println("No active accounts to deploy to.")
185-
return
183+
deployTask := parallelTask{
184+
name: "deployment",
185+
startMsg: "🚀 Starting deployment to %d account(s)...\n",
186+
successMsg: "✅ Successfully deployed to %s",
187+
failMsg: "💥 Failed to deploy to %s: %v",
188+
successLog: "DEPLOY_SUCCESS",
189+
failLog: "DEPLOY_FAIL",
190+
taskFunc: runDeploymentForAccount,
186191
}
187192

188-
var wg sync.WaitGroup
189-
results := make(chan string, len(targetAccounts))
190-
191-
fmt.Printf("🚀 Starting deployment to %d account(s)...\n", len(targetAccounts))
192-
193-
for _, acc := range targetAccounts {
194-
wg.Add(1)
195-
go func(account model.Account) {
196-
defer wg.Done()
197-
err := runDeploymentForAccount(account)
198-
details := fmt.Sprintf("account: %s", account.String())
199-
if err != nil {
200-
results <- fmt.Sprintf("💥 Failed to deploy to %s: %v", account.String(), err)
201-
_ = db.LogAction("DEPLOY_FAIL", fmt.Sprintf("%s, error: %v", details, err))
202-
} else {
203-
results <- fmt.Sprintf("✅ Successfully deployed to %s", account.String())
204-
_ = db.LogAction("DEPLOY_SUCCESS", details)
205-
}
206-
}(acc)
207-
}
208-
209-
go func() {
210-
wg.Wait()
211-
close(results)
212-
}()
213-
214-
for res := range results {
215-
fmt.Println(res)
216-
}
217-
fmt.Println("\nDeployment complete.")
193+
runParallelTasks(targetAccounts, deployTask)
218194
},
219195
}
220196

@@ -265,41 +241,17 @@ var auditCmd = &cobra.Command{
265241
log.Fatalf("Error getting accounts: %v", err)
266242
}
267243

268-
if len(accounts) == 0 {
269-
fmt.Println("No active accounts to audit.")
270-
return
244+
auditTask := parallelTask{
245+
name: "audit",
246+
startMsg: "🔬 Starting audit of %d account(s)...\n",
247+
successMsg: "✅ OK: %s",
248+
failMsg: "🚨 Drift detected on %s: %v",
249+
successLog: "AUDIT_SUCCESS",
250+
failLog: "AUDIT_FAIL",
251+
taskFunc: runAuditForAccount,
271252
}
272253

273-
var wg sync.WaitGroup
274-
results := make(chan string, len(accounts))
275-
276-
fmt.Printf("🔬 Starting audit of %d account(s)...\n", len(accounts))
277-
278-
for _, acc := range accounts {
279-
wg.Add(1)
280-
go func(account model.Account) {
281-
defer wg.Done()
282-
err := runAuditForAccount(account)
283-
details := fmt.Sprintf("account: %s", account.String())
284-
if err != nil {
285-
results <- fmt.Sprintf("🚨 Drift detected on %s: %v", account.String(), err)
286-
_ = db.LogAction("AUDIT_FAIL", fmt.Sprintf("%s, error: %v", details, err))
287-
} else {
288-
results <- fmt.Sprintf("✅ OK: %s", account.String())
289-
_ = db.LogAction("AUDIT_SUCCESS", details)
290-
}
291-
}(acc)
292-
}
293-
294-
go func() {
295-
wg.Wait()
296-
close(results)
297-
}()
298-
299-
for res := range results {
300-
fmt.Println(res)
301-
}
302-
fmt.Println("\nAudit complete.")
254+
runParallelTasks(accounts, auditTask)
303255
},
304256
}
305257

@@ -362,6 +314,55 @@ var importCmd = &cobra.Command{
362314
},
363315
}
364316

317+
// A task to be run in parallel on an account.
318+
type parallelTask struct {
319+
name string // e.g., "deployment", "audit"
320+
startMsg string // e.g., "🚀 Starting deployment..."
321+
successMsg string // e.g., "✅ Successfully deployed to %s"
322+
failMsg string // e.g., "💥 Failed to deploy to %s: %v"
323+
successLog string // e.g., "DEPLOY_SUCCESS"
324+
failLog string // e.g., "DEPLOY_FAIL"
325+
taskFunc func(model.Account) error
326+
}
327+
328+
func runParallelTasks(accounts []model.Account, task parallelTask) {
329+
if len(accounts) == 0 {
330+
fmt.Printf("No active accounts for %s.\n", task.name)
331+
return
332+
}
333+
334+
var wg sync.WaitGroup
335+
results := make(chan string, len(accounts))
336+
337+
fmt.Printf(task.startMsg, len(accounts))
338+
339+
for _, acc := range accounts {
340+
wg.Add(1)
341+
go func(account model.Account) {
342+
defer wg.Done()
343+
err := task.taskFunc(account)
344+
details := fmt.Sprintf("account: %s", account.String())
345+
if err != nil {
346+
results <- fmt.Sprintf(task.failMsg, account.String(), err)
347+
_ = db.LogAction(task.failLog, fmt.Sprintf("%s, error: %v", details, err))
348+
} else {
349+
results <- fmt.Sprintf(task.successMsg, account.String())
350+
_ = db.LogAction(task.successLog, details)
351+
}
352+
}(acc)
353+
}
354+
355+
go func() {
356+
wg.Wait()
357+
close(results)
358+
}()
359+
360+
for res := range results {
361+
fmt.Println(res)
362+
}
363+
fmt.Printf("\n%s complete.\n", strings.Title(task.name))
364+
}
365+
365366
func runDeploymentForAccount(account model.Account) error {
366367
var connectKey *model.SystemKey
367368
var err error

0 commit comments

Comments
 (0)