PHP Classes

File: examples/example2_error.php

Recommend this page to a friend!
  Packages of Jorge Castro   MultiOne   examples/example2_error.php   Download  
File: examples/example2_error.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: 1 year ago
Size: 4,781 bytes
 

 

Contents

Class file image Download
<?php

use Eftec\MultiOne\MultiOne;

include __DIR__ . '/../vendor/autoload.php';

function factoryBody($taskInit,$taskEnd,$current): array
{
    return [
            'task'=>['init'=>$taskInit,'end'=>$taskEnd],
            'done'=>$current
        ];
}


MultiOne::Factory(
    1000, // every miliseconds
    basename(__FILE__), // the url to call
    4,     // the number of workers
    'try',
    10,
    'try1_'
)->setMethods(
    static function($numWorkers):array { // the initial call
        echo "<h1>Example 2 error (using try)</h1>";
        echo "<h2>In case of error, it tries it again</h2>";
        $result=[];
        $numOper=100;
        $split=ceil($numOper/$numWorkers);
        for($instance=0; $instance<$numWorkers; $instance++) {
            $result[$instance]=factoryBody($split*$instance,($split*$instance)+$split,$split*$instance);
        }
        return $result;
    },
    static function($idWorker,$body):array { // the worker call
        $body['done'] += 1;
        if($body['done']>=$body['task']['end']) {
            $body['done']='ok';
            return MultiOne::msgAnswer('end',$body,'done');
        }
        $x=10/random_int(0,3); // division by zero
        usleep(random_int(500, 1500));

        return MultiOne::msgAnswer('run',$body,"#$idWorker {$body['done']}");
    },
    static function($body) { // the worker-end call
        echo "all worker ended";
    }
)->setUI(
    '<div id="PREFIX_content">%s</div>', // how the content is draw
    '<div id="PREFIX_worker_%s" style="border:solid 1px"></div><div id="PREFIX_worker_error_%s" style="border:solid 1px; color:red"></div>', // how every worker is draw
    '<div id="PREFIX_worker_end"></div>', // how the worker-end is draw.
    '' // no loading ui
)->runAuto();

MultiOne::Factory(
    1000, // every miliseconds
    basename(__FILE__), // the url to call
    4,     // the number of workers
    'stop',
    10,
    'stop1_'
)->setMethods(
    static function($numWorkers):array { // the initial call
        echo "<h1>Example 2 (stop)</h1>";
        echo "<h2>In case of error, it stops the worker</h2>";
        $result=[];
        $numOper=100;
        $split=ceil($numOper/$numWorkers);
        for($instance=0; $instance<$numWorkers; $instance++) {
            $result[$instance]=factoryBody($split*$instance,($split*$instance)+$split,$split*$instance);
        }
        return $result;
    },
    static function($idWorker,$body):array { // the worker call
        $body['done'] += 1;
        if($body['done']>=$body['task']['end']) {
            $body['done']='ok';
            return MultiOne::msgAnswer('end',$body,'done');
        }
        $x=10/random_int(0,3); // division by zero
        usleep(random_int(500, 1500));

        return MultiOne::msgAnswer('run',$body,"#$idWorker {$body['done']}");
    },
    static function($body) { // the worker-end call
        echo "all worker ended";
    }
)->setUI(
    '<div id="PREFIX_content">%s</div>', // how the content is draw
    '<div id="PREFIX_worker_%s" style="border:solid 1px"></div><div id="PREFIX_worker_error_%s" style="border:solid 1px; color:red"></div>', // how every worker is draw
    '<div id="PREFIX_worker_end"></div>', // how the worker-end is draw.
    '' // no loading ui
)->runAuto();

MultiOne::Factory(
    1000, // every miliseconds
    basename(__FILE__), // the url to call
    4,     // the number of workers
    'end',
    10,
    'end1_'
)->setMethods(
    static function($numWorkers):array { // the initial call
        echo "<h1>Example 2 (end)</h1>";
        echo "<h2>In case of error, it stops all workers</h2>";
        $result=[];
        $numOper=100;
        $split=ceil($numOper/$numWorkers);
        for($instance=0; $instance<$numWorkers; $instance++) {
            $result[$instance]=factoryBody($split*$instance,($split*$instance)+$split,$split*$instance);
        }
        return $result;
    },
    static function($idWorker,$body):array { // the worker call
        $body['done'] += 1;
        if($body['done']>=$body['task']['end']) {
            $body['done']='ok';
            return MultiOne::msgAnswer('end',$body,'done');
        }
        $x=10/random_int(0,3); // division by zero
        usleep(random_int(500, 1500));

        return MultiOne::msgAnswer('run',$body,"#$idWorker {$body['done']}");
    },
    static function($body) { // the worker-end call
        echo "all worker ended";
    }
)->setUI(
    '<div id="PREFIX_content">%s</div>', // how the content is draw
    '<div id="PREFIX_worker_%s" style="border:solid 1px"></div><div id="PREFIX_worker_error_%s" style="border:solid 1px; color:red"></div>', // how every worker is draw
    '<div id="PREFIX_worker_end"></div>', // how the worker-end is draw.
    '' // no loading ui
)->runAuto();