As Mohammed Gomma mentioned you need to use Batch API to achieve the same. The implementation of batch api may look something like code given below:
- Create Batch array
- Create function to execute commands and call them using batch operation
- set context message in the function created
create finish callback
function mymodule_form_submit(&$form, &$form_state) {
$delete_load = $form_state['values']['delete_load'];
$change_load = $form_state['values']['change_load'];
$load_number = $form_state['values']['check_load'];
if ($change_load == 1) {
# DELETE
# perform some command
# LOAD
# perform other command
drupal_set_message('Deleting. Please wait...', 'error');
drupal_set_message('Changing load. Please wait...', 'warning');
}
elseif ($delete_load == 1 && $change_load == 0) {
# DELETE
drupal_set_message('Deleting. Please wait...', 'error');
$command = "<some shell script command to send>";
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
dpm($output);
ssh2_exec($connection, 'exit');
unset($connection);
}
else {
# do nothing
}
# OTHER DIFFERENT SET OF COMMANDS TO SEND
drupal_set_message('Configuring. Please wait...', 'status');
$commands = array(
"<some shell script command to send>",
"<some shell script command to send>",
"<some shell script command to send>",
);
$batch = array(
'finished' => 'batch_example_finished',
'title' => t('Processing Example Batch'),
'init_message' => t('Example Batch is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Example Batch has encountered an error.'),
);
foreach ($commands as $command) {
$batch['operations'][] = array('_batch_execute_command', array($command));
};
batch_set($batch);
return t('Success');
}
function _batch_execute_command($command, &$context) {
$connection = mymodule_connect();
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
dpm($output);
ssh2_exec($connection, 'exit');
unset($connection);
$context['message'] = t('Now processing %command', array('%command' => $command));
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Batch 'finished' callback
*/
function batch_example_finished($success, $results, $operations) {
if ($success) {
// Here we do something meaningful with the results.
$message = t('@count items successfully processed:', array('@count' => count($results)));
// $message .= theme('item_list', $results); // D6 syntax
$message .= theme('item_list', array('items' => $results));
drupal_set_message($message);
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
drupal_set_message($message, 'error');
}
}
Nested batch example:
function gamify_batch_form($form, $form_state) {
return array(
'submit' => array(
'#type' => 'submit',
'#title' => 'submit',
'#value' => 'submit',
),
);
}
function gamify_batch_form_submit($form, $form_state) {
$batch = array(
'finished' => 'batch_example_finished',
'title' => t('Processing Example Batch'),
'init_message' => t('Example Batch is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Example Batch has encountered an error.'),
);
$commands = array(
'hi',
'hello',
'world',
'dear',
'world',
);
foreach ($commands as $command) {
$batch['operations'][] = array('_batch_execute_command', array($command));
};
batch_set($batch);
}
function _batch_execute_command($command, &$context) {
file_get_contents("https://drupal.stackexchange.com/questions/246767/show-progress-of-code-executed-on-form-submit/247718?noredirect=1#comment303466_247718");
if($command == 'world'){
$batch = array(
'finished' => 'batch_example_finished',
'title' => t('Processing Example Batch'),
'init_message' => t('Example Batch is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Example Batch has encountered an error.'),
);
$commands = array(
'hi 1',
'hello 2',
'dear 3',
);
foreach ($commands as $innercommand) {
$batch['operations'][] = array('_batch_execute_command', array($innercommand));
};
batch_set($batch);
}
$context['message'] = t('Now processing %command', array('%command' => $command));
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Batch 'finished' callback
*/
function batch_example_finished($success, $results, $operations) {
if ($success) {
// Here we do something meaningful with the results.
$message = t('@count items successfully processed:', array('@count' => count($results)));
// $message .= theme('item_list', $results); // D6 syntax
$message .= theme('item_list', array('items' => $results));
drupal_set_message($message);
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
drupal_set_message($message, 'error');
}
}