['type' => 'HC','handler' => 'App\\Helpers\\HttpRequestHelper'], 'sub' => ['type' => 'CC','handler' => 'App\\Controllers\\Jobs\SubJob'],'fancy_date_time_format' => [ 'type' => 'HF','handler' => 'fancy_date_time_format'],'addNumber' => ['type' => 'HC','handler' => 'App\\Model\\HttpRequestHelper'],'excelFileFormatValidation' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'excelFileDataValidation' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'employeesOnboardPreprocess' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'employeeDisembark' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'employeesSIEnhanceProcess' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'employeesCorrectionProcess' => ['type' => 'CC','handler' => 'App\\Controllers\\EmployeeServiceController'],'send_email' => ['type' => 'HC','handler' => 'App\\Helpers\\MailHelper'],'bulk_mail' => ['type' => 'HC','handler' => 'App\\Helpers\\MailHelper']]; public function __construct() { // echo 'HiC';//die(); // parent::__construct(); // $this->load->model('JobModel'); // $this->dbm = $this->users_model; // if (php_sapi_name() !== 'cli') // { // die('Invalid context'); // } } public static function streamOutput($data) { ob_implicit_flush(true); // try { ob_end_flush(); } catch(Exception $e) { echo $e->getMessage(); } echo $data; flush(); } public static function processJobs(array $jobdata = []) { // echo 'listen';//die(); $query = " SELECT id, name, payload, uuid FROM jobs WHERE status=? ORDER BY created_dt ASC"; $where_condition = [self::STATUS_QUEUED]; $db = \Config\Database::connect(); $jobs = $db->query($query, $where_condition)->getResult(); if(count($jobs)) { // echo count($jobs); // print_r($jobs);die; foreach($jobs as $key => $job) { // echo $job->id.' - '.$job->name; //sleep(1); SELF::processjob(['id' => $job->id,'uuid' => $job->uuid]); // usleep( 500000 ); } } } /** * process jobs */ public static function processJob(array $jobdata = []) { // print_r($jobdata); // echo 'listen'; // die(); $query = " SELECT id, name, payload, uuid FROM jobs WHERE status=? ORDER BY created_dt ASC LIMIT 1 FOR UPDATE"; $where_condition = [self::STATUS_QUEUED]; if(isset($jobdata) && count($jobdata)) { $query = " SELECT id, name, payload, uuid FROM jobs WHERE status=? AND id=? AND uuid=? ORDER BY created_dt ASC LIMIT 1 FOR UPDATE"; $where_condition = [self::STATUS_QUEUED,$jobdata['id'],$jobdata['uuid']]; } $db = \Config\Database::connect(); $job = $db->query($query, $where_condition)->getResult(); // print_r($job);die(); if ($job !== []) { $job = $job[0]; // echo "\nProcessing job id - " . $job->id . "\n"; SELF::streamOutput("\nProcessing job id - " . $job->id . "\n"); // sleep(5); // echo "Job name - " . $job->name . "\n"; SELF::streamOutput("Job name - " . $job->name . "\n"); // sleep(5); // print_r(SELF::$event_class_mapping); // echo array_key_exists($job->name,SELF::$event_class_mapping) ? 'mapped' : 'notmapped'; // die(); try { $start = microtime(true); $runtime = null; $job_status = self::STATUS_RUNNING; $db->query("UPDATE jobs SET status=? WHERE id=? AND uuid=?", [$job_status, $job->id,$job->uuid]); if (!array_key_exists($job->name,SELF::$event_class_mapping)) { throw new \RuntimeException('Job ' . $job->name . ' handler not registered'); } $handler = SELF::$event_class_mapping[$job->name]; $handleInstance = null; if($handler['type'] == 'CC' || $handler['type'] == 'HC') { // echo 'CLASS - ' . $handler['type'].' - ' . $handler['handler']; $handlerClass = $handler['handler']; if(class_exists($handlerClass)) { $handleInstance = new $handlerClass(); } else { throw new \RuntimeException('Job ' . $job->name . ' or handler class not found'); // echo $e->getMessage(); } if (method_exists($handleInstance, $job->name)) { $jobHandler = [$handleInstance, $job->name]; //throw new \RuntimeException('Job ' . $job->name . ' not found'); } else if(method_exists($handleInstance, 'handle')) { $jobHandler = [$handleInstance, 'handle']; } else { throw new \RuntimeException('Job ' . $job->name . ' or handler not found'); } } else if($handler['type'] == 'HF') { // echo 'HF - ' . $handler['type']; $jobHandler = $handleInstance = $handler['handler']; // echo $jobHandler; } else{ throw new \RuntimeException('Job ' . $job->name . ' Invalid job type'); } $payload = json_decode($job->payload, true); if (!is_array($payload)) { throw new \InvalidArgumentException('Invalid payload format here'); } $payload = is_array($payload) ? $payload : []; $response = $jobHandler($payload,$job->id); $runtime = microtime(true) - $start; $job_status = self::STATUS_DONE; } catch (\Exception $e) { //die(); $job_status = self::STATUS_FAILED; $runtime = $runtime === null ? microtime(true) - $start : $runtime; $response = $e->getMessage(); } $db->query("UPDATE jobs SET status=?, run_time=?, response=? WHERE id=? AND uuid=?", [ $job_status, $runtime, json_encode($response), $job->id,$job->uuid ]); // echo "Job $job->id $job_status. Response - $response \n"; // echo "Job $job->id $job_status \n"; SELF::streamOutput("Job $job->id $job_status \n"); return true; } else { echo 'no job found in queue'; } } }