php - affliate window csv to drupal node via batch api -
i have affliate window csv has 1 lac rows, want save nodes, , tried batch api.
but still getting php timeout error ..please help
function module_aw_batch(){ $operations = array(); $csv = file_directory_path().'/aw/datafeed_134642.csv'; $file = fopen($csv, 'r'); while (($data = fgetcsv($file)) !== false) { $operations[] = array('module_aw_op', array($data)); } $batch = array( 'title' => t('generating feeds'), // title display while running. 'operations' => $operations, 'finished' => 'module_aw_finished', // last function call. 'init_message' => t('importing...it may take 4-5 hours'), 'progress_message' => t('processed @current out of @total.'), 'error_message' => t('import feeds has encountered error.'), ); batch_set($batch); batch_process('admin/content/node/overview'); } update (solved)
instead of reading whole csv file @ time, split csv read 5 lines per process
found solution
batch declaration
function modulename_batch(){ $operations[] = array('modulename_op', array()); $batch = array( 'title' => t('importing events'), 'operations' => $operations, 'finished' => 'modulename_finished', 'init_message' => t('message here'), 'progress_message' => 'message here', 'error_message' => t('message here'), ); batch_set($batch); batch_process('admin/content/node/overview');// redirect page } function modulename_aw_op(&$context) { if (!isset($context['sandbox']['offset'])) { $context['sandbox']['offset'] = 0; $context['sandbox']['records'] = 0; } $filename =file_directory_path().'/aw/datafeed_134642.csv'; // csv file $fp = fopen($filename, 'r'); if ( $fp === false ) { // failed open file watchdog('modulename', 'failed open ' . $filename); $context['finished'] = true; return; } $ret = fseek($fp, $context['sandbox']['offset']); if ( $ret != 0 ) { // failed seek watchdog('modulename', 'failed seek ' . $context['sandbox']['offset']); $context['finished'] = true; return; } $limit = 5; // maximum number of rows process @ time $done = false; ( $i = 0; $i < $limit; $i++ ) { $line = fgetcsv($fp); if ( $line === false ) { $done = true; // no more records process break; } else { $context['sandbox']['records']++; $context['sandbox']['offset'] = ftell($fp); $record = $context['sandbox']['records']; // data } } $eof = feof($fp); if ( $eof ) { $context['success'] = true; } $context['message'] = "processed " . $context['sandbox']['records'] . " records"; $context['finished'] = ( $eof || $done ) ? 1 : 0; }
Comments
Post a Comment