PHP Classes

File: src/thread.php

Recommend this page to a friend!
  Classes of Mahesh S   Simple PHP Multithreading   src/thread.php   Download  
File: src/thread.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Simple PHP Multithreading
Create threads that run multiple tasks in parallel
Author: By
Last change:
Date: 4 years ago
Size: 1,284 bytes
 

Contents

Class file image Download
<?php
use Opis\Closure\SerializableClosure;
use function
Opis\Closure\{serialize as s, unserialize as u};
require_once(
dirname(__DIR__, 4).'/vendor/autoload.php');
$jobId = $argv[1];
$jobsDir = $argv[2];
$logsDir = $argv[3];
$helperClass = $argv[4];
$helper = new $helperClass;
$basePath = $helper->getAppBasePath();
if(!
file_exists("{$basePath}/{$jobsDir}/{$jobId}_closure.ser"))
    die(
"Closure file for Job ID: $jobId doesn't exist");
if(!
file_exists("{$basePath}/{$jobsDir}/{$jobId}_arguments.ser"))
    die(
"Arguments file for Job ID: $jobId doesn't exist");
try{
   
$helper->bootstrap();
   
$wrapper = unserialize(file_get_contents("{$basePath}/{$jobsDir}/{$jobId}_closure.ser"));
   
$arguments = u(file_get_contents("{$basePath}/{$jobsDir}/{$jobId}_arguments.ser"));
   
file_put_contents("{$basePath}/{$logsDir}/smt_{$jobId}.log", $wrapper($arguments));
}catch(\
Exception $e){
   
file_put_contents("{$basePath}/{$logsDir}/smt_{$jobId}_error.log", "Caught Exception at ".date('Y-m-d H:i:s').": ".$e->getMessage()." at line: ".$e->getLine()." on file: ".$e->getFile().". Stack trace: ".$helper::getExceptionTraceAsString($e));
}
//garbage collection..
unlink("{$basePath}/{$jobsDir}/{$jobId}_closure.ser");
unlink("{$basePath}/{$jobsDir}/{$jobId}_arguments.ser");
exit;