nhance/app/Controllers/JobWorker.php

476 lines
17 KiB
PHP
Executable File

<?php
namespace App\Controllers;
use App\Models\JobModel;
use App\Models\FileModel;
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',
],
'makeEntryForBDSPolicyTransaction' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmpDataServiceController',
],
'removeBDSPolicyTransactionEntryFromTruncate' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmpDataServiceController',
],
'employeesEnrollmentInsert' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeServiceController',
],
'calculateMembersDemography' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\LeadsController',
],
'updatePolicyTransactionDataWhileClinetPolicyUpdate' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\ClientController',
],
'claimDumpExcelFileFormatValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\TicketServiceController',
],
'claimDumpExcelFileDataValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\TicketServiceController',
],
'claimDumpOnBoardProcess' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\TicketServiceController',
],
'tpaClaimDumpImporter' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\TicketServiceController',
],
'tpaClaimDumpToTicketMasterImporters' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\TicketServiceController',
],
'excelMultieventFileFormateValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeMultiEventServiceController',
],
'excelMultieventFileDataValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeMultiEventServiceController',
],
'excelMultieventFileOnBoard' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeMultiEventServiceController',
],
'memberDataListExcelFileFormatValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\LeadsController',
],
'compareMemberDataAndInceptionData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeServiceController',
],
'MediAssistGetBenefDetails' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\MediAssistApiController',
],
'VidalGetBenefDetails' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\VidalApiController',
],
'saveVidalAPIData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\VidalApiController',
],
'FhplGetBenefDetails' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\FhplApiController',
],
'saveFhplAPIData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\FhplApiController',
],
'HealthIndiaGetBenefDetails' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\HealthIndiaApiController',
],
'saveHealthIndiaAPIData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\HealthIndiaApiController',
],
'VoloGetBenefDetails' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\VoloApiController',
],
'saveVoloAPIData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\VoloApiController',
],
'bdsDumpExcelFileFormatValidation' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\PolicyTransactionController',
],
'insertBulkBdsData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\PolicyTransactionController',
],
'initiateWellnessOnboardJob' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeController',
],
'saveMediAssitAPIData' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\MediAssistApiController',
],
'bulkGenerateEcardAndStoreinS3' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeController',
],
'bulkEcardDownloadAsZipFromS3' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeController',
],
'getEmployeeEcardFromTmpFolderAndZipToS3' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeController',
],
'visitOffBoard' => [
'type' => 'CC', // Handler Category
'handler' => 'App\Controllers\EmployeeController',
]
];
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();
// $jobdata = ['id' => 11954,'uuid' => '001700c2-8127-4d02-80b3-ea26e7603ccf'];
// dd($job);
$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);
$job_status = self::STATUS_DONE;
}
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(),'scope' => 'task failed'];
}
$runtime = microtime(true) - $start;
}
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(),'scope' => 'worker failed'];
}
$db->query("UPDATE jobs SET status=?, run_time=?, response=? WHERE id=? AND uuid=?", [
$job_status,
$runtime,
json_encode($response),
$job->id,$job->uuid
]);
//update file status if this job is directly linked with a file id
if($job_status == self::STATUS_FAILED)
{
//get file from job payload
if(array_key_exists('file_id', $payload) && $payload['file_id'] != NULL && $payload['file_id'] != "" && is_numeric($payload['file_id']) && count($payload) == 1)
{
$fileModel = new FileModel();
$fileModel->where('id', $payload['file_id'])
->set(['status' => 'failed','reason' => json_encode(['error_type' => 0,'error_summary' => [0],'error_data' => 'System Error, please contact Admin/Support team']) ])
->update();
}
}
// 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';
}
}
}