401 lines
14 KiB
PHP
401 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
use App\Models\JobModel;
|
|
use App\Models\ClientModel;
|
|
use App\Models\ClientPolicyModel;
|
|
use App\Models\NotificationModel;
|
|
use App\Models\EmployeePolicyModel;
|
|
|
|
use App\Helpers\sendMailNotification;
|
|
|
|
class JobWorker extends AdminController
|
|
{
|
|
const STATUS_DONE = 'done';
|
|
const STATUS_QUEUED = 'queued';
|
|
const STATUS_RUNNING = 'running';
|
|
const STATUS_FAILED = 'failed';
|
|
/**
|
|
* Constructs the class
|
|
*/
|
|
|
|
private static $event_class_mapping = [
|
|
'add' => [
|
|
'type' => 'HC', // Handler Category (Possible values: HC, CC, HF)
|
|
'handler' => 'App\Helpers\HttpRequestHelper',
|
|
],
|
|
'sub' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\Jobs\SubJob',
|
|
],
|
|
'fancy_date_time_format' => [
|
|
'type' => 'HF', // Handler Category (Possible values: HC, CC, HF)
|
|
'handler' => 'fancy_date_time_format', // Likely a custom function
|
|
],
|
|
'addNumber' => [
|
|
'type' => 'HC', // Handler Category
|
|
'handler' => 'App\Model\HttpRequestHelper',
|
|
],
|
|
'excelFileFormatValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'excelFileDataValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'employeesOnboardPreprocess' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'employeeDisembark' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'employeesSIEnhanceProcess' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'employeesCorrectionProcess' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
],
|
|
'send_email' => [
|
|
'type' => 'HC', // Handler Category
|
|
'handler' => 'App\Helpers\MailHelper',
|
|
],
|
|
'bulk_mail' => [
|
|
'type' => 'HC', // Handler Category
|
|
'handler' => 'App\Helpers\MailHelper',
|
|
],
|
|
'insertBatchList' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
|
|
// Employee Data Service Events
|
|
'importInceptionFileValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importInceptionUpdateTPAandUHID' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'cashDepositCalculationForInception' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'sendMailForDownloadingECard' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
|
|
'importCorrectionValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importCorrectionUpdateEndorsementID' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
|
|
'cashDepositCalculationForSIEnhancement' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importSIEnhancementValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importSIEnhancementUpdateEndorsementID' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
|
|
'cashDepositCalculationForDeletion' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importDeletionValidation' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'importDeletionUpdateEndorsementID' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmpDataServiceController',
|
|
],
|
|
'employeesEnrollmentInsert' => [
|
|
'type' => 'CC', // Handler Category
|
|
'handler' => 'App\Controllers\EmployeeServiceController',
|
|
]
|
|
];
|
|
|
|
|
|
|
|
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 : [];
|
|
try
|
|
{
|
|
$response = $jobHandler($payload,$job->id);
|
|
}
|
|
catch(\Exception $e)
|
|
{
|
|
$job_status = self::STATUS_FAILED;
|
|
$runtime = $runtime === null ? microtime(true) - $start : $runtime;
|
|
$response = ['file_name' => $e->getFile(),'error' => $e->getMessage(),'line_no' => $e->getLine(),'info' => $e->getTraceAsString()];
|
|
}
|
|
|
|
$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 = ['file_name' => $e->getFile(),'error' => $e->getMessage(),'line_no' => $e->getLine(),'info' => $e->getTraceAsString()];
|
|
}
|
|
|
|
$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';
|
|
}
|
|
}
|
|
|
|
|
|
public static function enrollment_status() {
|
|
$clientModel = new ClientModel();
|
|
$clientPolicyModel = new ClientPolicyModel();
|
|
$notificationModel = new NotificationModel();
|
|
$employeePolicyModel = new EmployeePolicyModel();
|
|
$myLogger = \Config\Services::mylogger();
|
|
|
|
$client_policy_data = $clientPolicyModel->where('is_active', 1)
|
|
->where('policy_status', 1)
|
|
->where('inception_type', 2)
|
|
->findAll();
|
|
$currentDate = date('Y-m-d');
|
|
|
|
// Update policy status based on start and end dates
|
|
foreach ($client_policy_data as $client_policy) {
|
|
$id = $client_policy['id'];
|
|
|
|
if ($currentDate == $client_policy['policy_start_date']) {
|
|
$clientPolicyModel->update($id, ['open_for_enrollment' => 1]);
|
|
}
|
|
if ($client_policy['policy_end_date'] < $currentDate) {
|
|
$clientPolicyModel->update($id, ['open_for_enrollment' => 0]);
|
|
}
|
|
}
|
|
|
|
$reminder_whole_mail = [];
|
|
foreach ($client_policy_data as $client_policy) {
|
|
$employee = $employeePolicyModel->select('employees.*')
|
|
->join('employees', 'employee_polices.employee_id = employees.id', 'left')
|
|
->where('employee_polices.client_policy_id', $client_policy['id'])
|
|
->first();
|
|
$client_data = $clientModel->find($client_policy['client_id']);
|
|
$notification_data = $notificationModel->where('client_id', $client_policy['client_id'])
|
|
->where('template_name', 'member_reminder_mail')
|
|
->first();
|
|
|
|
if ($notification_data && $notification_data['enabled'] == 1 && $currentDate == $client_policy['reminder_date']) {
|
|
$params['emp_data'] = $employee;
|
|
$params['client_data'] = $client_data;
|
|
$params['notification_data'] = $notification_data;
|
|
$reminder_whole_mail[] = sendMailNotification::sendMailNotification('member_reminder_mail', $params);
|
|
|
|
}
|
|
}
|
|
// Process and queue bulk emails
|
|
$temp_whole_data = [];
|
|
$count = 0;
|
|
foreach ($reminder_whole_mail as $whole_index => $values) {
|
|
foreach ($values as $index => $value) {
|
|
$temp_whole_data[] = $value;
|
|
$count++;
|
|
|
|
if ($count == 20 || ($whole_index == count($reminder_whole_mail) - 1 && $index == count($values) - 1)) {
|
|
if (!empty($temp_whole_data)) {
|
|
Jobs::addJob(['job_name' => 'bulk_mail', 'payload' => $temp_whole_data]);
|
|
$temp_whole_data = [];
|
|
$count = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return json_encode(true);
|
|
}
|
|
|
|
|
|
|
|
}
|