Skip to content

Commit 090c7ae

Browse files
authored
Merge pull request #1 from utopia-php/feat-new-loop-method
New loop method
2 parents 0337918 + bc71b5c commit 090c7ae

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ script.php
1919
```php
2020
<?php
2121

22-
require_once '../vendor/autoload.php';
22+
require_once './vendor/autoload.php';
2323

2424
use Utopia\CLI\CLI;
2525
use Utopia\CLI\Console;
@@ -96,6 +96,22 @@ echo $stdout; // ''
9696
echo $stderr; // 'error'
9797
```
9898

99+
### Create a Daemon
100+
101+
You can use the `Console::loop` command to create your PHP daemon. The `loop` method already handles CPU consumption using a configurable sleep function and calls the PHP garbage collector every 5 minutes.
102+
103+
```php
104+
<?php
105+
106+
use Utopia\CLI\Console;
107+
108+
include './vendor/autoload.php';
109+
110+
Console::loop(function() {
111+
echo "Hello World\n";
112+
}, 200000 /* 200ms */);
113+
```
114+
99115
## System Requirements
100116

101117
Utopia Framework requires PHP 7.3 or later. We recommend using the latest PHP version whenever possible.

src/CLI/Console.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,31 @@ static public function isInteractive(): bool
186186
{
187187
return ('cli' === PHP_SAPI && defined('STDOUT'));
188188
}
189+
190+
/**
191+
* @param callable $callback
192+
* @param int $sleep in microseconds
193+
*/
194+
static public function loop(callable $callback, $sleep = 100000 /* 100ms */): void
195+
{
196+
gc_enable();
197+
198+
$time = 0;
199+
200+
while (!connection_aborted() || PHP_SAPI == "cli") {
201+
202+
$callback();
203+
204+
usleep($sleep);
205+
206+
$time = $time + $sleep;
207+
208+
if (PHP_SAPI == "cli") {
209+
if($time >= (1000000 * 300)) { // Every 5 minutes
210+
$time = 0;
211+
gc_collect_cycles(); //Forces collection of any existing garbage cycles
212+
}
213+
}
214+
}
215+
}
189216
}

tests/CLI/ConsoleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,18 @@ public function testExecuteTimeout()
114114
$this->assertEquals('', $stdout);
115115
$this->assertEquals(1, $code);
116116
}
117+
118+
public function testLoop()
119+
{
120+
$file = __DIR__.'/../resources/loop.php';
121+
$stdin = '';
122+
$stdout = '';
123+
$stderr = '';
124+
$code = Console::execute('php '.$file, $stdin, $stdout, $stderr, 3);
125+
126+
$this->assertEquals('', $stderr);
127+
$this->assertGreaterThan(30, count(explode("\n", $stdout)));
128+
$this->assertLessThan(50, count(explode("\n", $stdout)));
129+
$this->assertEquals(1, $code);
130+
}
117131
}

tests/resources/loop.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Utopia\CLI\Console;
4+
5+
include __DIR__.'/../../vendor/autoload.php';
6+
7+
Console::loop(function() {
8+
echo "Hello\n";
9+
});

0 commit comments

Comments
 (0)