简体   繁体   中英

Getcommandinvocation is not being invoked after command id is used once in aws ssm

I am implementing aws ssm send command in asp.net web mvc web application. Below is my code. After sending command, I tried to invoke status of sent command using getcommandinvocation in aws ssm. Problem is this code works fine in debug mode when debugger is there. But in release mode, it's not working(when debugger is not there). In release mode, I am getting exception, that 'invocation does not exist'. I read that this case happens when command id and instance id don't match with any invocation. So I am suspecting, that when the invocation status is in progress, and when it comes back to do loop, command is not being invoked again, as it was invoked in before. I am not understanding where I am going wrong. Can anyone please help?? Any help is appreciated

      var sendCommandRequest = new SendCommandRequest()
      {
        DocumentName = mdl_AWSCommands.command_name,
        InstanceIds = mdl_AWSCommands.instance_ids,
        Parameters = mdl_AWSCommands.parameter_with_values
      };
      SendCommandResponse sendCommandResponse = new SendCommandResponse();
      sendCommandResponse = client.SendCommand(sendCommandRequest);
      var getCommandInvocationRequest = new GetCommandInvocationRequest()
      {
        CommandId = sendCommandResponse.Command.CommandId,
        InstanceId = sendCommandResponse.Command.InstanceIds[0].ToString()
      };`enter code here`       
      do
      {
        GetCommandInvocationResponse getCommandInvocationResponse = client.GetCommandInvocation(getCommandInvocationRequest);
        strGetCommandInvocationStatus = getCommandInvocationResponse.Status.Value.ToString();
        if (getCommandInvocationResponse.StatusDetails.Contains("Success"))
        {
          if (getCommandInvocationResponse.StandardErrorContent == "")
          {
            
            mdl_AwsCmdOutput.Clear();
            mdl_AwsCmdOutput.Add(new Mdl_AWSCommands
            {
              command_output = getCommandInvocationResponse.StandardOutputContent,
              command_status = "Success",
              invocation_command_id = getCommandInvocationResponse.CommandId.ToString()
            });
            status = true;
            break;
          }
          else
          {
            mdl_AwsCmdOutput.Clear();
            mdl_AwsCmdOutput.Add(new Mdl_AWSCommands
            {
              command_output = getCommandInvocationResponse.StandardErrorContent,
              command_status = "Error",
              invocation_command_id = getCommandInvocationResponse.CommandId.ToString()
            });
            status = true;            
            break;
          }

        }
        else if (getCommandInvocationResponse.Status.Value.Contains("InProgress") || getCommandInvocationResponse.Status.Value.Contains("Pending"))
        {
          status = false;//reviewcomment
          var stopwatch = Stopwatch.StartNew();
          while (true)
          {
            if (stopwatch.ElapsedMilliseconds >= 10000)
            {                                   
              break;
            }
          }
        }
        else if (getCommandInvocationResponse.Status.Value.Contains("Delayed") || getCommandInvocationResponse.Status.Value.Contains("Cancelled") ||
          getCommandInvocationResponse.Status.Value.Contains("TimedOut") || getCommandInvocationResponse.Status.Value.Contains("Failed") ||
          getCommandInvocationResponse.Status.Value.Contains("Cancelling"))
        {
          mdl_AwsCmdOutput.Clear();
          mdl_AwsCmdOutput.Add(new Mdl_AWSCommands
          {
            command_status = getCommandInvocationResponse.Status,
            invocation_command_id = getCommandInvocationResponse.CommandId.ToString()
          });
          status = true;
        }
      } while (status == false);
    }

I solved the issue by adding this

 var stopwatch = Stopwatch.StartNew();
        while (true)
        {
          if (stopwatch.ElapsedMilliseconds >= 5000)
          {
            break;
          }
        }

before calling get command invocation. And it resolved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM