Skip to content

Commit

Permalink
Created a child ctx from the parent context for go routines that spanw
Browse files Browse the repository at this point in the history
off commands that restart.

Also added designated signal handlers for each go routines; the signal
handler responds to SIGTERM and cancels the context on trigger.
  • Loading branch information
XuechunHou committed Dec 11, 2024
1 parent b8c2863 commit 15f9725
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/ops_agent_uap_wrapper/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func main() {

ctx := context.Background()
log.Println("Starting Ops Agent UAP Plugin")
//ps.Start(ctx, &pb.StartRequest{Config: &pb.StartRequest_Config{StateDirectoryPath: "/var/log/google-cloud-ops-agent"}})
ps.Start(ctx, &pb.StartRequest{})
ps.Start(ctx, &pb.StartRequest{Config: &pb.StartRequest_Config{StateDirectoryPath: "/var/log/google-cloud-ops-agent"}})

//ps.Start(ctx, &pb.StartRequest{})
for {
status, _ := ps.GetStatus(ctx, &pb.GetStatusRequest{})
log.Print(status)
Expand Down
14 changes: 12 additions & 2 deletions cmd/ops_agent_uap_wrapper/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ func restartCommand(ctx context.Context, wg *sync.WaitGroup, logger logs.Structu
logger.Warnf("Context has been cancelled, exiting")
return
}

childCtx, ctxCancel := context.WithCancel(ctx)
defer childCtx.Done()
sigHandler(childCtx, func(_ os.Signal) {
// We're handling some external signal here, set cleanup to [false].
// If this was Guest Agent trying to stop it would call [Stop] RPC directly
// or do a [SIGKILL] which anyways cannot be intercepted.
ctxCancel()
})
cmd = exec.CommandContext(childCtx, cmd.Path, cmd.Args[1:]...)

cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,

Check failure on line 243 in cmd/ops_agent_uap_wrapper/service.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

unknown field Pdeathsig in struct literal of type "syscall".SysProcAttr

Check failure on line 243 in cmd/ops_agent_uap_wrapper/service.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

unknown field Pdeathsig in struct literal of type "syscall".SysProcAttr
Setpgid: true,

Check failure on line 244 in cmd/ops_agent_uap_wrapper/service.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

unknown field Setpgid in struct literal of type "syscall".SysProcAttr

Check failure on line 244 in cmd/ops_agent_uap_wrapper/service.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

unknown field Setpgid in struct literal of type "syscall".SysProcAttr
Expand All @@ -253,9 +264,8 @@ func restartCommand(ctx context.Context, wg *sync.WaitGroup, logger logs.Structu
}
// Sleep 10 seconds before retarting the task
time.Sleep(5 * time.Second)
cmdToRestart := exec.CommandContext(ctx, cmd.Path, cmd.Args[1:]...)
wg.Add(1)
go restartCommand(ctx, wg, logger, cmdToRestart)
go restartCommand(childCtx, wg, logger, cmd)
}

func CreateOpsAgentUapPluginLogger(logDir string) logs.StructuredLogger {
Expand Down

0 comments on commit 15f9725

Please sign in to comment.