phpmysqlredisphp-resque

php-reque: worker fails with "MySQL server has gone away"


I started using php-resque (https://github.com/chrisboulton/php-resque) along with php and mysql server. I am seeing "MySQL server has gone away" errors by the workers.

Job queuing code:

if ($request->command == 'custom_script') {       
  //error_log("before queue");
  Resque::enqueue($queue, 'AsyncTest', array('text'=>'yolo'));
  //error_log("after queue");
}

Job class:

class AsyncTest{
public function setUp() {
   # Set up something before perform, like establishing a database connection
    $mysql = Mysql::getInstance();
    EH::setErrorHandler();
}

public function perform()
{
//      $mysql = Mysql::getInstance();
    // EH::setErrorHandler();
    //error_log($this->args['text']);
    $haiku_step_query= $mysql->query("SELECT id FROM haiku LIMIT 10 OFFSET 1");
    $row = $mysql->fetch_assoc($haiku_step_query);
    $haiku_id = $row['id'];
}
public function tearDown() {
   # Run after perform, like closing resources
}
}

I see "MySQL server has gone away" error messages:

 MySQL ERROR №2006 MySQL server has gone away[qs:SELECT id FROM haiku LIMIT 10 OFFSET 1] 

(/var/www/html/haiku_server/vendor/chrisboulton/php-resque/resque.php   line 77
Resque_Worker->work(5) >> /var/www/html/haiku_server/vendor/chrisboulton/php-resque/lib/Resque/Worker.php   line 199  
Resque_Worker->perform(Object) >> /var/www/html/haiku_server/vendor/chrisboulton/php-resque/lib/Resque/Worker.php   line 237
Resque_Job->perform() >> /var/www/html/haiku_server/vendor/chrisboulton/php-resque/lib/Resque/Job.php   line 182
AsyncTest->perform() >> /var/www/html/haiku_server/includes/bgJobs/AsyncThreads.php   line 48          
Mysql->query("SELECT id FROM haiku LIMIT 10 OFFSET 1") >> /var/www/html/haiku_server/includes/Mysql.class.php   line 113   )

Please note that this does not happen, that is mysql works fine, when I do the operation whitout php-resque, like:

if ($request->command == 'custom_script') {
    $mysql = Mysql::getInstance();
    EH::setErrorHandler();
    $haiku_step_query= $mysql->query("SELECT id FROM haiku LIMIT 10 OFFSET 1");
    $row = $mysql->fetch_assoc($haiku_step_query);
    $haiku_id = $row['id'];

}


Solution

  • Take a look at this issue: https://github.com/chrisboulton/php-resque/issues/269

    A possible cause is because after the job runs for the first time, your objects will stay loaded in memory, including your MySQL connection. A solution is to reconnect to MySQL in your perform method.