-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(octane): add support for ticks and intervals with FrankenPHP
- Loading branch information
1 parent
ee88fe3
commit affb97d
Showing
5 changed files
with
221 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Laravel\Octane\FrankenPhp; | ||
|
||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\Support\Carbon; | ||
use Throwable; | ||
|
||
class InvokeTickCallable | ||
{ | ||
public function __construct( | ||
protected string $key, | ||
protected $callback, | ||
protected int $seconds, | ||
protected bool $immediate, | ||
protected $cache, | ||
protected ExceptionHandler $exceptionHandler | ||
) { | ||
} | ||
|
||
/** | ||
* Invoke the tick listener. | ||
* | ||
* @throws Throwable | ||
*/ | ||
public function __invoke(): void | ||
{ | ||
$lastInvokedAt = $this->cache->get('tick-'.$this->key); | ||
|
||
if (! is_null($lastInvokedAt) && | ||
(Carbon::now()->getTimestamp() - $lastInvokedAt) < $this->seconds) { | ||
return; | ||
} | ||
|
||
$this->cache->forever('tick-'.$this->key, Carbon::now()->getTimestamp()); | ||
|
||
if (is_null($lastInvokedAt) && ! $this->immediate) { | ||
return; | ||
} | ||
|
||
try { | ||
call_user_func($this->callback); | ||
} catch (Throwable $e) { | ||
$this->exceptionHandler->report($e); | ||
} | ||
} | ||
|
||
/** | ||
* Indicate how often the listener should be invoked. | ||
*/ | ||
public function seconds(int $seconds): static | ||
{ | ||
$this->seconds = $seconds; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Indicate that the listener should be invoked on the first tick after the server starts. | ||
*/ | ||
public function immediate(): static | ||
{ | ||
$this->immediate = true; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Laravel\Octane\Tests; | ||
|
||
use Illuminate\Contracts\Debug\ExceptionHandler; | ||
use Illuminate\Support\Carbon; | ||
use Laravel\Octane\FrankenPhp\InvokeTickCallable; | ||
use Mockery; | ||
|
||
class FrankenPhpInvokeTickCallableTest extends TestCase | ||
{ | ||
/** | ||
* @throws \Throwable | ||
*/ | ||
public function test_callable_is_invoked_when_due(): void | ||
{ | ||
Carbon::setTestNow($now = now()); | ||
|
||
$instance = new InvokeTickCallable( | ||
'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, true, | ||
$cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) | ||
); | ||
|
||
$cache->shouldReceive('get')->with('tick-key')->andReturn(time() - 100); | ||
|
||
$cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); | ||
|
||
$instance(); | ||
|
||
$this->assertTrue($_SERVER['__test.invokeTickCallable'] ?? false); | ||
|
||
Carbon::setTestNow(); | ||
unset($_SERVER['__test.invokeTickCallable']); | ||
} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
public function test_callable_is_not_invoked_when_not_due(): void | ||
{ | ||
Carbon::setTestNow(now()); | ||
|
||
$_SERVER['__test.invokeTickCallable'] = false; | ||
|
||
$instance = new InvokeTickCallable( | ||
'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 30, true, | ||
$cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) | ||
); | ||
|
||
$cache->shouldReceive('get')->with('tick-key')->andReturn(time() - 10); | ||
|
||
$cache->shouldReceive('forever')->never(); | ||
|
||
$instance(); | ||
|
||
$this->assertFalse($_SERVER['__test.invokeTickCallable'] ?? false); | ||
|
||
Carbon::setTestNow(); | ||
unset($_SERVER['__test.invokeTickCallable']); | ||
} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
public function test_callable_is_invoked_when_first_run_and_immediate(): void | ||
{ | ||
Carbon::setTestNow($now = now()); | ||
|
||
$instance = new InvokeTickCallable( | ||
'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, true, | ||
$cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) | ||
); | ||
|
||
$cache->shouldReceive('get')->with('tick-key')->andReturn(null); | ||
|
||
$cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); | ||
|
||
$instance(); | ||
|
||
$this->assertTrue($_SERVER['__test.invokeTickCallable'] ?? false); | ||
|
||
Carbon::setTestNow(); | ||
unset($_SERVER['__test.invokeTickCallable']); | ||
} | ||
|
||
public function test_callable_is_not_invoked_when_first_run_and_not_immediate() | ||
{ | ||
Carbon::setTestNow($now = now()); | ||
|
||
$instance = new InvokeTickCallable( | ||
'key', fn () => $_SERVER['__test.invokeTickCallable'] = true, 1, false, | ||
$cache = Mockery::mock('stdClass'), Mockery::mock(ExceptionHandler::class) | ||
); | ||
|
||
$cache->shouldReceive('get')->with('tick-key')->andReturn(null); | ||
|
||
$cache->shouldReceive('forever')->once()->with('tick-key', $now->getTimestamp()); | ||
|
||
$instance(); | ||
|
||
$this->assertFalse($_SERVER['__test.invokeTickCallable'] ?? false); | ||
|
||
Carbon::setTestNow(); | ||
unset($_SERVER['__test.invokeTickCallable']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters