0

I am not sure if this is doable or not. I am running cron job to process data and they are all independent of each other. For example i have a data [x,y,z] and i have a method in the parent controller that does what it needs to do. Process takes little long and hence my queue is piling up since it is doing one at a time. I tried forking process but it loses connection to the mongo database. Therefore, I had to remove fork for now but please let me know if i can reconnect. Pseudocode

MyTools.php
class MY_Tools extends CI_Controller {
    ...
    public function process($item) {
        Make curl request
        Update database for the item
   }
}
Tools.php
class Tools extends MY_Tools {
    ...
    public function getAllDate() {
        $data = fetchDataFromDB() => [X,Y,Z]
        $i = 0
        while ($i < sizeof($data) { 
            $this->process($data[$i]);
            $i++;
        }
   }
}

if i can do this without waiting for another process to complete and just keep on going, that will be great

In addition, I am using php7 cimongo library for codeigniter and https://github.com/alcaeus/mongo-php-adapter

Possible Solution For php7, i have used this for gearman installation https://techearl.com/php/installing-gearman-module-for-php7-on-ubuntu

Codeigniter gearman library that I used : https://github.com/appleboy/CodeIgniter-Gearman-Library

To overcome static method accessing parent controller, use singleton method I was struggling with this for a bit and hopefully it will help someone

Example

class MY_Tools extends CI_Controller {
    private static $instance;
    function __construct() {
        parent::__construct();
        self::$instance =& $this;
    }
    public static function get_instance()
    {
        return self::$instance;
    }
}

To Access MY_Tools::get_instance()->YOUR_PUBLIC_METHODS();

Hope this can help someone

Asif Alamgir
  • 1,454
  • 1
  • 16
  • 41
  • Hey, not the exact same question so I won't mark it as duplicate but I think your answer can be found here: https://stackoverflow.com/questions/14126696/php-when-to-use-pthread – adamoffat Sep 17 '18 at 15:29
  • I am thinking of using gearman to resolve this. Will update the code once it is in functioning – Asif Alamgir Sep 18 '18 at 15:06
  • 1
    Yeah thats definitely an option. Here's another link that talks about the differences between the two if you would like some more information: https://stackoverflow.com/questions/33219294/asynchronous-database-service-calls-in-php-gearman-vs-pthreads – adamoffat Sep 18 '18 at 15:07

0 Answers0