Skip to content

Commit

Permalink
Added signal handler in sub go routines.
Browse files Browse the repository at this point in the history
  • Loading branch information
XuechunHou committed Dec 4, 2024
1 parent 206f0d1 commit 6aa4435
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/ops_agent_uap_wrapper/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ 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{})
for {
status, _ := ps.GetStatus(ctx, &pb.GetStatusRequest{})
Expand Down
8 changes: 3 additions & 5 deletions cmd/ops_agent_uap_wrapper/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (ps *OpsAgentPluginServer) Cancel() {
// interface notification of the "exiting" state.
func sigHandler(ctx context.Context, cancel func(sig os.Signal)) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGKILL)
signal.Notify(sigChan, syscall.SIGTERM)
go func() {
select {
case sig := <-sigChan:
Expand Down Expand Up @@ -119,7 +119,6 @@ func (ps *OpsAgentPluginServer) runAgent(ctx context.Context) {
}

var wg sync.WaitGroup

// Starting Diagnostics Service
execDiagnosticsCmd := exec.CommandContext(ctx,
Prefix+"/libexec/google_cloud_ops_agent_diagnostics",
Expand Down Expand Up @@ -168,7 +167,6 @@ func (ps *OpsAgentPluginServer) Start(ctx context.Context, msg *pb.StartRequest)
pCtx, cancel := context.WithCancel(context.Background())
ps.cancel = cancel
ps.startContext = pCtx

go ps.runAgent(pCtx)
return &pb.StartResponse{}, nil
}
Expand Down Expand Up @@ -220,7 +218,7 @@ func runCommand(cmd *exec.Cmd, logger logs.StructuredLogger) error {
return nil
}

func restartCommand(ctx context.Context, wg *sync.WaitGroup, logger logs.StructuredLogger, cmd *exec.Cmd) {
func restartCommand(ctx context.Context, wg *sync.WaitGroup, logger logs.StructuredLogger, cmd *exec.Cmd ) {
defer wg.Done()
if cmd == nil {
return
Expand Down Expand Up @@ -255,7 +253,7 @@ 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...)
cmdToRestart := exec.CommandContext(ctx, cmd.Path, cmd.Args[1:]...)
wg.Add(1)
go restartCommand(ctx, wg, logger, cmdToRestart)
}
Expand Down

0 comments on commit 6aa4435

Please sign in to comment.