PHP Classes

File: examples/example3.php

Recommend this page to a friend!
  Classes of Jorge Castro   MultiOne   examples/example3.php   Download  
File: examples/example3.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MultiOne
Execute many tasks in parallel using JavaScript
Author: By
Last change:
Date: 3 days ago
Size: 2,301 bytes
 

Contents

Class file image Download
<?php /** @noinspection PhpUnhandledExceptionInspection */

/** @noinspection PhpUnusedParameterInspection */

use Eftec\MultiOne\MultiOne;

include
__DIR__ . '/../vendor/autoload.php';
/** @noinspection PhpRedundantOptionalArgumentInspection */
MultiOne::Factory(
   
1000, // every miliseconds
   
basename(__FILE__), // the url to call
   
4 // the number of workers

)->setMethods(
    static function(
$numWorkers):array { // the initial call
       
?>
<!doctype html>
        <html lang="en">
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
                  integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
                  crossorigin="anonymous">
            <title>Example 3</title>
        </head>
        <?php
       
// it creates the initial payload for every worker.
       
return array_fill(0, $numWorkers - 0, 'initial');
    },
    static function(
$idWorker, $body):array { // the worker call
        // it reads the data and reduces to 1.
       
$workPending=random_int(1,5);
        if(
$workPending===3){
            return [
'type'=>'end','result'=>'done','ui'=>'done'];
        }
       
sleep(random_int(1, 3));
        return [
'result'=>$workPending, 'ui'=>"Worker: $idWorker, time:" . time()." pending: ".$workPending];
    },
    static function(
$bodies) { // the worker-end call
       
echo "all worker ended";
    }
)->
setUI(
   
'<body><h1>Example #3</h1><div class="content-fluid" id="PREFIX_content">
    <div class="row">%s</div></div></body></html>'
, // how the content is draw
   
'<div class="card col-3" >
    <div class="card-body">
    <h5 class="card-title">Worker</h5>
    <p class="card-text" id="PREFIX_worker_%s">....</p>
    <p class="card-text" id="PREFIX_worker_error_%s">....</p>
    </div>
    </div>'
,
   
'<div class="card col" >
    <div class="card-body">
    <h5 class="card-title">Worker End</h5>
    <p class="card-text" id="PREFIX_worker_end"></p>
    </div>
    </div>'
, // how the worker-end is draw.
   
"loading ?" // the loading ui. If empty, then it don't use loading ui
)->runAuto();