Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ProcessMaker/Listeners/HandleRedirectListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ protected function setRedirectTo(ProcessRequest $processRequest, string $method,
self::$redirectionParams = $params;
}

/**
* Reset the static state for Octane compatibility.
* This prevents data leaks between requests in long-running workers.
*/
public static function reset(): void
{
self::$processRequest = null;
self::$redirectionMethod = '';
self::$redirectionParams = [];
}

public static function sendRedirectToEvent()
{
$method = self::$redirectionMethod;
Expand Down
17 changes: 17 additions & 0 deletions ProcessMaker/Octane/ResetRequestState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace ProcessMaker\Octane;

use ProcessMaker\Listeners\HandleRedirectListener;
use ProcessMaker\Providers\ProcessMakerServiceProvider;

final class ResetRequestState
{
public function handle(): void
{
ProcessMakerServiceProvider::beginRequestTiming();
HandleRedirectListener::reset();
}
}
37 changes: 30 additions & 7 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\SystemProcessCounter;
use Laravel\Horizon\WorkerCommandString;
use Laravel\Octane\Events\RequestTerminated;
use Laravel\Passport\Client as PassportClient;
use Lavary\Menu\Menu;
use OpenApi\Analysers\AttributeAnnotationFactory;
Expand All @@ -49,6 +51,7 @@
use ProcessMaker\Models;
use ProcessMaker\Multitenancy\Tenant;
use ProcessMaker\Observers;
use ProcessMaker\Octane\ResetRequestState;
use ProcessMaker\PolicyExtension;
use ProcessMaker\Providers\PermissionServiceProvider;
use ProcessMaker\Repositories\SettingsConfigRepository;
Expand Down Expand Up @@ -106,6 +109,9 @@ public function boot(): void

$this->checkConfigCache();

// Register Octane listeners if Octane is enabled
$this->registerOctaneListeners();

// Hook after service providers boot
self::$bootTime = (microtime(true) - self::$bootStart) * 1000; // Convert to milliseconds
}
Expand Down Expand Up @@ -257,7 +263,7 @@ protected static function registerEvents(): void
{
// Listen to the events for our core screen
// types and add our javascript
Facades\Event::listen(ScreenBuilderStarting::class, function ($event) {
Event::listen(ScreenBuilderStarting::class, function ($event) {
// Add any extensions to form builder
// and renderer from packages
$event->manager->addPackageScripts($event->type);
Expand All @@ -276,7 +282,7 @@ protected static function registerEvents(): void
});

// Log Notifications
Facades\Event::listen(NotificationSent::class, function ($event) {
Event::listen(NotificationSent::class, function ($event) {
$id = $event->notifiable->id;
$notifiable = get_class($event->notifiable);
$notification = get_class($event->notification);
Expand All @@ -285,24 +291,24 @@ protected static function registerEvents(): void
});

// Log Broadcasts (messages sent to laravel-echo-server and redis)
Facades\Event::listen(BroadcastNotificationCreated::class, function ($event) {
Event::listen(BroadcastNotificationCreated::class, function ($event) {
$channels = implode(', ', $event->broadcastOn());

Log::debug('Broadcasting Notification ' . $event->broadcastType() . 'on channel(s) ' . $channels);
});

// Fire job when task is assigned to a user
Facades\Event::listen(ActivityAssigned::class, function ($event) {
Event::listen(ActivityAssigned::class, function ($event) {
$task_id = $event->getProcessRequestToken()->id;
// Dispatch the SmartInbox job with the processRequestToken as parameter
SmartInbox::dispatch($task_id);
});

Facades\Event::listen(MadeTenantCurrentEvent::class, function ($event) {
Event::listen(MadeTenantCurrentEvent::class, function ($event) {
event(new TenantResolved($event->tenant));
});

Facades\Event::listen(TenantNotFoundForRequestEvent::class, function ($event) {
Event::listen(TenantNotFoundForRequestEvent::class, function ($event) {
if (config('app.multitenancy') === false || self::actuallyRunningInConsole()) {
// This is expected if multitenancy is disabled.
// We also need to check if we are running in a console command because
Expand All @@ -327,7 +333,7 @@ protected static function registerEvents(): void
}
});

Facades\Event::listen(function (CommandStarting $event) {
Event::listen(function (CommandStarting $event) {
if ($event->command === 'l5-swagger:generate') {
// Set the analyser to use the legacy DocBlockAnnotationFactory. This must
// be set here because this config value is not serializable and cannot be cached.
Expand Down Expand Up @@ -573,6 +579,23 @@ public static function getPackageBootTiming(): array
return self::$packageBootTiming;
}

/**
* Reset per-request static state between Octane requests.
*
* Octane workers stay alive across requests, so static properties must be
* cleared to avoid leaking data from one request into the next. Singletons
* holding mutable state are handled by the 'flush' list in config/octane.php,
* which Octane applies on its own.
*/
private function registerOctaneListeners(): void
{
if (!class_exists(RequestTerminated::class)) {
return;
}

Event::listen(RequestTerminated::class, ResetRequestState::class);
}

/**
* Find the tenant based on the environment variable
*/
Expand Down
232 changes: 232 additions & 0 deletions config/octane.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<?php

use Laravel\Octane\Contracts\OperationTerminated;
use Laravel\Octane\Events\RequestHandled;
use Laravel\Octane\Events\RequestReceived;
use Laravel\Octane\Events\RequestTerminated;
use Laravel\Octane\Events\TaskReceived;
use Laravel\Octane\Events\TaskTerminated;
use Laravel\Octane\Events\TickReceived;
use Laravel\Octane\Events\TickTerminated;
use Laravel\Octane\Events\WorkerErrorOccurred;
use Laravel\Octane\Events\WorkerStarting;
use Laravel\Octane\Events\WorkerStopping;
use Laravel\Octane\Listeners\CloseMonologHandlers;
use Laravel\Octane\Listeners\CollectGarbage;
use Laravel\Octane\Listeners\DisconnectFromDatabases;
use Laravel\Octane\Listeners\EnsureUploadedFilesAreValid;
use Laravel\Octane\Listeners\EnsureUploadedFilesCanBeMoved;
use Laravel\Octane\Listeners\FlushOnce;
use Laravel\Octane\Listeners\FlushTemporaryContainerInstances;
use Laravel\Octane\Listeners\FlushUploadedFiles;
use Laravel\Octane\Listeners\ReportException;
use Laravel\Octane\Listeners\StopWorkerIfNecessary;
use Laravel\Octane\Octane;

return [

/*
|--------------------------------------------------------------------------
| Octane Server
|--------------------------------------------------------------------------
|
| This value determines the default "server" that will be used by Octane
| when starting, restarting, or stopping your server via the CLI. You
| are free to change this to the supported server of your choosing.
|
| Supported: "roadrunner", "swoole", "frankenphp"
|
*/

'server' => env('OCTANE_SERVER', 'roadrunner'),

/*
|--------------------------------------------------------------------------
| Force HTTPS
|--------------------------------------------------------------------------
|
| When this configuration value is set to "true", Octane will inform the
| framework that all absolute links must be generated using the HTTPS
| protocol. Otherwise your links may be generated using plain HTTP.
|
*/

'https' => env('OCTANE_HTTPS', false),

/*
|--------------------------------------------------------------------------
| Octane Listeners
|--------------------------------------------------------------------------
|
| All of the event listeners for Octane's events are defined below. These
| listeners are responsible for resetting your application's state for
| the next request. You may even add your own listeners to the list.
|
*/

'listeners' => [
WorkerStarting::class => [
EnsureUploadedFilesAreValid::class,
EnsureUploadedFilesCanBeMoved::class,
],

RequestReceived::class => [
...Octane::prepareApplicationForNextOperation(),
...Octane::prepareApplicationForNextRequest(),
//
],

RequestHandled::class => [
//
],

RequestTerminated::class => [
// FlushUploadedFiles::class,
],

TaskReceived::class => [
...Octane::prepareApplicationForNextOperation(),
//
],

TaskTerminated::class => [
//
],

TickReceived::class => [
...Octane::prepareApplicationForNextOperation(),
//
],

TickTerminated::class => [
//
],

OperationTerminated::class => [
FlushOnce::class,
FlushTemporaryContainerInstances::class,
// DisconnectFromDatabases::class,
// CollectGarbage::class,
],

WorkerErrorOccurred::class => [
ReportException::class,
StopWorkerIfNecessary::class,
],

WorkerStopping::class => [
CloseMonologHandlers::class,
],
],

/*
|--------------------------------------------------------------------------
| Warm / Flush Bindings
|--------------------------------------------------------------------------
|
| The bindings listed below will either be pre-warmed when a worker boots
| or they will be flushed before every new request. Flushing a binding
| will force the container to resolve that binding again when asked.
|
*/

'warm' => [
...Octane::defaultServicesToWarm(),
// Services to pre-resolve on worker start
ProcessMaker\Managers\PackageManager::class,
ProcessMaker\Managers\LoginManager::class,
ProcessMaker\Managers\IndexManager::class,
],

'flush' => [
// Services with mutable state that must be recreated per request
ProcessMaker\Models\AnonymousUser::class,
ProcessMaker\ImportExport\Extension::class,
ProcessMaker\ImportExport\SignalHelper::class,
ProcessMaker\Managers\MenuManager::class,
],

/*
|--------------------------------------------------------------------------
| Octane Swoole Tables
|--------------------------------------------------------------------------
|
| While using Swoole, you may define additional tables as required by the
| application. These tables can be used to store data that needs to be
| quickly accessed by other workers on the particular Swoole server.
|
*/

'tables' => [
'example:1000' => [
'name' => 'string:1000',
'votes' => 'int',
],
],

/*
|--------------------------------------------------------------------------
| Octane Swoole Cache Table
|--------------------------------------------------------------------------
|
| While using Swoole, you may leverage the Octane cache, which is powered
| by a Swoole table. You may set the maximum number of rows as well as
| the number of bytes per row using the configuration options below.
|
*/

'cache' => [
'rows' => 1000,
'bytes' => 10000,
],

/*
|--------------------------------------------------------------------------
| File Watching
|--------------------------------------------------------------------------
|
| The following list of files and directories will be watched when using
| the --watch option offered by Octane. If any of the directories and
| files are changed, Octane will automatically reload your workers.
|
*/

'watch' => [
'app',
'bootstrap',
'config/**/*.php',
'database/**/*.php',
'public/**/*.php',
'resources/**/*.php',
'routes',
'composer.lock',
'.env',
],

/*
|--------------------------------------------------------------------------
| Garbage Collection Threshold
|--------------------------------------------------------------------------
|
| When executing long-lived PHP scripts such as Octane, memory can build
| up before being cleared by PHP. You can force Octane to run garbage
| collection if your application consumes this amount of megabytes.
|
*/

'garbage' => 50,

/*
|--------------------------------------------------------------------------
| Maximum Execution Time
|--------------------------------------------------------------------------
|
| The following setting configures the maximum execution time for requests
| being handled by Octane. You may set this value to 0 to indicate that
| there isn't a specific time limit on Octane request execution time.
|
*/

'max_execution_time' => 30,

];
Loading
Loading