API Waiting for PHP not working for me

on this page https://developers.cloudways.com/examples.html I tried API to clone app. Everything is working fine except the last three lines.

While waiting it gives error that another operation is in progress

An error occurred code: 422 output: {“message”:“An operation is already in progress for this server.”,“operation”:{“id”:“5969652”,“type”:“clone_app”,“server_id”:“334355”,“estimated_time_remaining”:“4”,“status”:“Process is initiated”,“is_completed”:“0”}}

Without waiting to work fine I can’t proceed. I need to change the password of the associated DB after first operation but I am unable to do this.

Any help.

My Code is here

function clone_app($app_label, $accessToken){
global $server_id;

$cloneAppResponse = callCloudWaysAPI('POST', '/app/clone', $accessToken, [
    'server_id' => $server_id,
    'app_id' => '1028094',
    'app_label' => $app_label,
    ]);

    return $cloneAppResponse;
  }


function get_operation($operation_id, $accessToken)
{
  $operation = callCloudWaysAPI('GET', '/operation/' . $operation_id, $accessToken)->operation;
  return $operation;
}

function change_db_password($app_id,$accessToken){
  global $server_id;

  $dbChangePassword = callCloudWaysAPI('POST', '/app/manage/dbPassword', $accessToken, [
  'server_id' => $server_id,
  'app_id' => $app_id,
  'password' => '2d5e988e',
  ]);

  return $cloneAppResponse;
}

echo "<pre>";

$clone_app_response = clone_app('pakerp_faisal20', $accessToken);

$operation_id = $clone_app_response->operation_id;
$sys_user = $clone_app_response->sys_user;
$app_id = $clone_app_response->app_id;

print_r($clone_app_response);

$op_response = get_operation($operation_id,$accessToken);

while ($op_response->is_completed == 0) {
$op_response = get_operation($operation_id, $accessToken);
sleep(30);
}

print_r($op_response);

Hi Faisal,
Hope you are doing good.

Cloning process will take few minutes may be 4-5 until the process completes. You can’t run multiple operations simultaneously. You need to add any wait call or check if the clone server exist then change the DB password.

That is the problem. In Wait Call I am calling this API via function get_operation. But Server is taking this as another operation and returning an error.

Can you suggest a way to add Wait Call. I have taken this from the CloudWays documentation and just modified it slightly in Function Calls.