Appreciate, respect and accept it, don't take it for granted

Batch API Notes

2016-03-28

Batch sets are used to spread processing (primarily, but not exclusively, forms processing) over several page requests. This helps to ensure that the processing is not interrupted due to PHP timeouts, while users are still able to receive feedback on the progress of the ongoing operations.
Batch operations

設定operations

可依照需求切割一個operation內做的事,假設1000個node,可以一個operation處理一個node或一個operation處理多個node。Client端利用ajax對https://sitename/batch?id=[batch_id]&op=do發出HttpRequest。

// 假設$data是從EXCEL中讀出的資料

$split_rows = array_chunk($data, 50);

foreach ($split_rows as $key => $rows) {
  $operations[] = array(
      'import_batch_processing',  // The function to run on 50 row in one operation
      array($rows),  // The rows in the Excel
  );
}

設定batch資訊

$batch = array(
  'title' => t('批次處理中...'),
  'operations' => $operations,  // Runs all of the queued processes from the while loop above.
  'file' => drupal_get_path('module', 'module_name_which_you_create') . '/import_batch_processing.inc',
  'finished' => 'import_batch_processing_finished', // Function to run when the import is successful
'error_message' => t('The installation has encountered an error.'),
'progress_message' => t('資料匯入中...'),
);

設定finish callback function

function import_batch_processing_finished( $success, $results, $operations ){
if($success){
  ...
}
else {
  // An error occurred.
  // $operations contains the operations that remained unprocessed.
  $error_operation = reset($operations);
  drupal_set_message(
    t('An error occurred while processing @operation with arguments : @args',
      array(
        '@operation' => $error_operation[0],
        '@args' => print_r($error_operation[0], TRUE),
      )
    )
  );
}

線上展示

  1. Evaluate Drupal projects online
  2. 安裝模組 Examples for Developers
  3. 登入admin帳號後,開啟模組Devel generate和Batch example
  4. 前往路徑 admin/config/development/generate/content,新增1000個測試Node
  5. 前往路徑 examples/batch_example,開始測試

可配合程式碼觀察
Batch example source code

其他批次處理可用模組

Ref.


Blog comments powered by Disqus