I work on daemon and I've got problem with parent database connection, when first forked child ends his job. It is known that when forking the child process inherits the parent's database connection. When the child exits, the connection is closed. I tried to use (in parent and child process):
Database::instance()->disconnect();
Database::instance()->connect();
but it didn't work.
This is my main daemon's loop:
$this->model_tasks = new Model_Queue_Tasks();
while (!$this->_sigterm) {
pcntl_signal_dispatch();
$task = $this->model_tasks->get_next_task();
if ( ! empty($task)) {
// Write log to prevent memory issues
Kohana::$log->write();
// Fork process to execute task
$pid = pcntl_fork();
if ($pid) // Parent so add the pid to the list
{
// Parent - add the child's PID to the running list
$this->_pids[$pid] = time();
Database::instance()->disconnect();
Database::instance()->connect();
}
else // We are child
{
// We need to detach from the master process and become our own master process.
if (posix_setsid() == -1) {
exit(1);
}
Database::instance()->disconnect();
Database::instance()->connect();
$this->model_tasks->run_task($task);
}
}
Model_Tasks extends Model_Database
When child process ends I receive mysqli_error during executing get_next_task method, which is simple select.
How should I handle the DB connections? Should I use static model for that?