Utilizzando PCNTL_FORK Il processo figlio non può accedere all'istanza di codeigniter creata dal genitore

StackOverflow https://stackoverflow.com/questions/9458824

  •  13-11-2019
  •  | 
  •  

Domanda

Sto cercando di scrivere un processo del demone usando la libreria di accensione del codice e il problema è quando sbottino il processo del demone non può più accedere all'istanza CI e tutte le librerie inizializzate in __construct sono anche inaccessibili se creo un nuovo CI istanza nel processo figlio, non riesco ancora ad accedere a nessuna libreria e ottengo i seguenti errori:

Errore-2012-02-27 00:13:07-> Gravità: avviso-> include (applicazione/errori/error_general.php): Impossibile aprire il flusso: nessun file o directory/srv/http/freefileconvert/system /core/exceptions.php 146 ERRORE-2012-02-27 00:13:07-> Gravità: Avviso-> include (): APPERAZIONE DI APPROGGIO INCONTRO 'ERRORS/ERROR_GENERAL.PHP' per inclusione (include_path = '. :/usr/share/pera ') /srv/http/freefileconvert/system/core/exceptions.php 146

Ecco il mio codice:

class Conversion_workers
{
    function __construct() {
        $this->ci =& get_instance();
        $this->ci->load->library('Gearman');
    }

    private function is_locked($lock_file) {
        if(file_exists($lock_file)) {
            $lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
            if (! flock($lock, LOCK_EX | LOCK_NB)) {
                flock($lock, LOCK_UN);
                return TRUE;
            }
        }
        return FALSE;
    }


    private function lock_file($lock_file, &$lock) {
        $lock = fopen($lock_file,"c+"); // open it for WRITING ("w")
        if (! flock($lock, LOCK_EX | LOCK_NB)) {
            //error_log('Unable to lock file.');
            return FALSE;
        }
        fseek($lock, 0);
        ftruncate($lock, 0);
        fwrite($lock, posix_getpid());
        fflush($lock);  
        return TRUE;
    }


    private function daemonize($lock_file, &$lock, &$parent)
    {

        // TODO: In the install check if pcntl_fork supported
        if($this->is_locked($lock_file)) {

            $parent = TRUE;
            return FALSE;
        }

        $pid = pcntl_fork();
        if($pid < 0) {
            log_message('error', 'Unable to fork process');
            return FALSE;
        }

        // If we got a good PID, then we can exit the parent process. 
        if($pid > 0) {
            log_message('info', 'Exiting parent as process forked successfully');
            $parent = TRUE;
            return FALSE;
        }

        ob_start();

        // Change the file mode mask 
        umask(0);

        // Create a new SID for the child process
        if (posix_setsid() < 0) {
            //error_log('Unable to create a new SID for child process');
            return FALSE;
        }

        // Change the current working directory 
        if(chdir("/tmp") < 0) {
            //error_log('Unable to change directory of the daemonize process');
            return FALSE;
        }

        // Lock in child process to get correct pid in lock file
        if(!$this->lock_file($lock_file, $lock)) {
            exit;
        }

        fclose(STDIN);  // Close all of the standard
        fclose(STDOUT); // file descriptors as we
        fclose(STDERR); // are running as a daemon.

        register_shutdown_function(create_function('$pars', 
            'ob_end_clean();posix_kill(posix_getpid(), SIGKILL);'), array());

        // Might be good idea to have register_shutdown_function() here if we want to 
        // check status when the daemon terminates.

        return TRUE;
    } 

   private function start_workers() {

        $lock_file  = $this->ci->config->item('lock_file');
        $parent     = FALSE; 

        if(! $this->daemonize($lock_file, $lock, $parent)) {
            if($parent)
                return;

        } else {

            // Start a worker
            $this->ci->gearman->gearman_worker();
            $this->ci->gearman->add_worker_function('some_function', 'some_function_fn'); 

            error_log('Starting worker ['.posix_getpid().']');

            while($this->ci->gearman->work());
        }

        error_log('Exiting worker');
        exit;
    }

    function add_to_queue($function_name, $params) {
        $this->start_workers();

        $this->ci->gearman->gearman_client();
        return $this->ci->gearman->do_job_background($function_name, serialize($params)); 
    }
} // END class Controller

La funzione "add_to_queue ()" funziona bene, ma il problema è con "start_worker" che dopo aver falsificato smette di funzionare, non riesco nemmeno ad accedere alle funzioni di supporto di registro, ecco perché sto usando ERRIF_LOG () nel mio codice.

Sarei grato se qualcuno potesse aiutare per favore.

È stato utile?

Soluzione

Potrebbe essere che il metodo daemonizzato cambi la directory di lavoro /tmp? Nel qual caso il sistema è alla ricerca di Include in .(/TMP) e /usr/share/pear, nessuno dei quali li tiene?

Forse http://www.php.net/manual/en/ini.core.php#ini.include-path aiuterà.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top