Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vilim67's avatar

Beanstalk queue

Hy everyone, I couldn't find answer to my problem so I'm hoping that someone over here has it :)

I have server that hosts admin part of the system and another server that worker. In the admin part users have an ability to export users in xlsx format. When admin hits export job is being sent to queue and worker server listens for it.

The problem is that when exporting all of users (about 6500) job fails but there is no error logs. When I tried with about 4000 users it passes. I have set timeout for worker 300s and the problem is that job fails at about 120-150th second. I tried it locally with the sam DB (over sync and beanstalk driver) and it works (but my mac has 8GB of RAM and worker server has 512) but I think it should only be slower not fail. I was also looking with top command in terminal while job is being processed and it uses only about 50% of memory (CPU is at 97-99%). I also tried to do it with sync driver on server but I get 502 Bad Gateway after about 2 mins. I have set set_time_limit(300), max_execution_time to 300 and 0, request_terminate_timeout to 300 and 0 and nothing helps. I'm usually using forge for queues but I also tried listening for job manually and it listens don't breaks and after 3 tries job fails with out any log.

For export to excel I'm using phpoffice/phpexcel.

Thanks

0 likes
5 replies
fideloper's avatar

I have a feeling this is more about php's memory_limit than hitting the maximum time. Definitely try increasing that on the worker server. Make sure you edit the php.ini specific to running PHP on CLI (if you're on debian/ubuntu, the php.ini for php-fpm, apache and CLI are separate files).

How do you keep the worker alive? In theory, there should be a way to get output sent to a file (esp. if you're using supervisord).

I'd also suggest checking out bugsnag to send exceptions to - I've found to be absolutely invaluable when debugging queues job errors. Well worth the low cost.

vilim67's avatar

@fideloper thank you for your reply.

I'm using nginx and I've bumped memory_limit in /etc/php5/cli/php.ini and in /etc/php5/fpm/php.ini to 512MB (servers total). Also I noticed that while job is processing in the top command output memory percentage doesn't go over 50. I have also installed Bugsnag and it didn't catch any errors :(

I also don't think that the problem is maximum time because I commented out the part of the code where the problem starts and put

sleep(150);
Log::info("Slept for 150s");

and info log has been recorded in log file.

This is the code if it helps

Log::info("Creating PHPExcel object");

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator(Config::get('companySettings.company_name'))->setTitle(Lang::get('users.export_users'));

$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->SetCellValue('A1', Lang::get('global.nr'));
// Setting other column titles


Log::info("Looping over obtained users");

$i = 2;
foreach ($users as $key => $user) {

    $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $i-1);
    // Setting other column values

    $i++;
    
}
Log::info("All users saved in object");

$objPHPExcel->getActiveSheet()->setTitle(Lang::get('users.export_users'));
Log::info("File title set");

$file_name = date('YmdHis_').Lang::get('users.export_users')."_".rand(1000, 9999).".xlsx";
Log::info("File name created");

$file = Config::get('custom.user_export_file_path')."/".$file_name;
Log::info("File path created");

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Log::info("Object writer created");

$objWriter->save($file);
Log::info("File saved in " . $file);
bashy's avatar

Tried running it from CLI? All those values are not limited then.

fideloper's avatar

Hmm so I guess I misunderstood - the part that is failing when exporting that via the web? (Instead of failing with in the queue job).

In other words, telling me you're using Nginx makes sense for web request, but I was asking about the queues. I see you're using beanstalkd as the worker queue, but then the workers consuming the jobs are typically something run on the command line (php artisan queue:work --daemon).

vilim67's avatar

@bashy thanks for suggestion, I have tried it but it also fails.

@fideloper I'm using forge for adding queue workers, I think commands php artisan queue:work {driver} --queue={queue} --delay=0 --memory=128 --sleep={sleep} --tries={tries} --env={environment} and php /home/forge/{website}/artisan queue:listen {driver} --timeout={timeout} --sleep={sleep} --quiet --tries={tries} --env={environment} --queue={queue} are used.

I have been putting logs a lot so see where the stop showing and to determine the root of the problem (btw. I have noticed that the was an exception while I was testing and the was nothing in log file until I catched the exception and logged it myself, I didn't know that happens). I'm using phpoffice/phpexcel library for creating excel files and I have realised that job is failing when trying to save the .xlsx file to the server. So I wanted to know why so I was tracking and logging further and came to: $this->_spreadSheet->garbageCollect():192; in Excel2007.php file, class PHPExcel_Writer_Excel2007.

After that I tried to do everything on the server where admin part of the system is and over there it exported users. That server has 1GB of RAM so it is a memory problem obviously, but it was weird to me why memory didn't went over 50% on the smaller server if thats the problem.

One more question, can I send mail attachment if I don't have it on se server as a file. I'm asking this because I don't really need to have that xlsx file on the server. Currently I'm sending mail in standard way like this

Mail::send('emails.export-users', $data, function($message) use($mails, $file){
    $message->to($mails)->subject('User export')->attach($file);
});     

so it would be cool if I could put an object(or something) in attach instead of file if something like that is possible.

One more time thank you for the effort @fideloper and @bashy

Please or to participate in this conversation.