Skip to content

Commit

Permalink
Merge pull request #13 from hschimpf/2.x-dev
Browse files Browse the repository at this point in the history
Minor fixes and updated README
  • Loading branch information
hschimpf authored Apr 9, 2023
2 parents b03ea34 + ddfbe77 commit ad20526
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ composer require hds-solutions/parallel-sdk
```

## Usage
Firstly, you need to set the bootstrap file for parallel. Setting the composer's autoloader is enough. See reference [#1](#references) for more info.
First, you need to set the bootstrap file for the parallel threads. Setting the composer's autoloader is enough. See reference [#1](#references) for more info.
```php
// check if extension is loaded to allow deploying even in environments where parallel isn't installed
if (extension_loaded('parallel')) {
Expand All @@ -35,6 +35,40 @@ You need to define a `Worker` that will process the tasks. There are two options

Then you can schedule tasks to run in parallel using `Scheduler::runTask()` method.

### Bootstrap a Laravel app
Since ZTS is only available on the cli, you should set the bootstrap file for parallel threads in the `artisan` file.
```diff
#!/usr/bin/env php
<?php

define('LARAVEL_START', microtime(true));

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

$app = require_once __DIR__.'/bootstrap/app.php';

+ // check if parallel extension is loaded
+ if (extension_loaded('parallel')) {
+ // and register the bootstrap file for the threads
+ parallel\bootstrap(__DIR__.'/parallel.php');
+ }
```

Then, in the bootstrap file for the parallel threads, you just need to get an instance of the app and bootstrap the Laravel kernel. This way you will have all Laravel service providers registered.
`bootstrap/parallel.php`:
```php
<?php declare(strict_types=1);

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

$app = require_once __DIR__.'/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

// bootstrap the Kernel
$kernel->bootstrap();
```

### Anonymous worker
Defining an anonymous function as a `Worker` to process the tasks.
```php
Expand Down
13 changes: 3 additions & 10 deletions src/Internals/ProgressBarWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __construct(

// threads memory usage and peak
$this->threads_memory = [
'current' => [ memory_get_usage() ],
'peak' => [ memory_get_usage() ],
'current' => [ '__main__' => 0 ],
'peak' => [ '__main__' => 0 ],
];
}

Expand Down Expand Up @@ -67,13 +67,6 @@ protected function progressBarAction(string $action, array $args): void {
}

protected function statsReport(string $worker_id, int $memory_usage): void {
// update memory usage for this thread
$this->threads_memory['current'][0] = memory_get_usage();
// update peak memory usage
if ($this->threads_memory['current'][0] > $this->threads_memory['peak'][0]) {
$this->threads_memory['peak'][0] = $this->threads_memory['current'][0];
}

// save memory usage of thread
$this->threads_memory['current'][$worker_id] = $memory_usage;
// update peak memory usage
Expand All @@ -87,7 +80,7 @@ protected function statsReport(string $worker_id, int $memory_usage): void {

private function getMemoryUsage(): string {
// main memory used
$main = Helper::formatMemory($this->threads_memory['current'][0]);
$main = Helper::formatMemory($this->threads_memory['current']['__main__']);
// total memory used (sum of all threads)
$total = Helper::formatMemory($total_raw = array_sum($this->threads_memory['current']));
// average of each thread
Expand Down
8 changes: 8 additions & 0 deletions src/Internals/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ protected function update(): void {
}

$this->send($this->hasPendingTasks(), eater: true);

if ($this->progressbar_started) {
//
$this->progressbar_channel->send(new Commands\ProgressBar\StatsReportMessage(
worker_id: '__main__',
memory_usage: memory_get_usage(),
));
}
}

protected function await(): bool {
Expand Down
3 changes: 2 additions & 1 deletion src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use HDSSolutions\Console\Parallel\Contracts\Task;
use HDSSolutions\Console\Parallel\Exceptions\ParallelException;
use HDSSolutions\Console\Parallel\Internals\Commands;
use parallel;
use parallel\Channel;
use parallel\Events\Event;
use parallel\Runtime;
Expand All @@ -30,7 +31,7 @@ private function __construct() {
$this->uuid = substr(md5(uniqid(self::class, true)), 0, 16);
$this->runner = PARALLEL_EXT_LOADED
// create a Runner instance inside a thread
? (new Runtime(PARALLEL_AUTOLOADER))->run(static function($uuid): void {
? parallel\run(static function($uuid): void {
// create runner instance
$runner = new Internals\Runner($uuid);
// listen for events
Expand Down
10 changes: 5 additions & 5 deletions tests/ParallelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public function testThatTasksCanBeCancelled(): void {
}
}

// wait 200ms and stop all
usleep(200_000);
// wait 500ms and stop all
usleep(500_000);
Scheduler::stop();

$has_pending_tasks = false;
Expand All @@ -178,9 +178,9 @@ public function testThatTasksCanBeCancelled(): void {
$has_cancelled_tasks = $has_cancelled_tasks || $task->wasCancelled();
}

$this->assertTrue($has_pending_tasks);
$this->assertTrue($has_processed_tasks);
$this->assertTrue($has_cancelled_tasks);
$this->assertTrue($has_pending_tasks, 'There are no pending Tasks left');
$this->assertTrue($has_processed_tasks, 'There are no processed Tasks');
$this->assertTrue($has_cancelled_tasks, 'There are no cancelled Tasks');

Scheduler::removeAllTasks();
}
Expand Down

0 comments on commit ad20526

Please sign in to comment.