Laravel Octane (Swoole) caching/state issues
defunctl opened this issue ยท 3 comments
I'm not sure this is 100% to do with external caching, but given Swoole runs with a number of workers and each worker runs PHP in memory, I see weird issues where something will get partially cached and throw errors until the Swoole workers are restarted.
It's potentially to do with the way the classes internally cache data to static variables and never clear the state. Obviously, this works in a traditional PHP request where the thread dies after the request is over, resetting that static variable to empty for each request, but in Octane, that static variable will just continue grow unless explicitly cleared.
Steps to reproduce (I'm using sail):
sail up -d
.- Load the Log Viewer in your browser and browse some logs.
- Delete the
laravel.log
file from the UI. - Generate a whole bunch of logs (some with very long output).
- Click back on the
laravel.log
item. - Refresh the log viewer a number of times, you'll see the Pagination at the bottom change counts depending on which Swoole worker is processing the request. Clicking on certain page numbers will generate the same Undefined array key 1 error seen there.
- Repeat deleting the file/generating new logs and refreshing to see all the state mismatching.
Dumping the log line, it looks like half parsed log line, that has the date at the start cut off, hence the Undefined array key 1 error seen when trying to set the time/timezone.
I've tried some different cache drivers with no real changes, which is why it makes me think it's the static variables.
LOG_VIEWER_CACHE_DRIVER=array
LOG_VIEWER_CACHE_DRIVER="null"
A work around for now is to just run the traditional local environment, but that wouldn't work if people want this on production running on Octane for some reason:
sail artisan octane:stop; sail artisan serve --host=0.0.0.0 --port=80
hey @defunctl
Thank you so much for such detailed report! Great detective work ๐ต๏ธ
You're right, the log file instances are kept in memory and never reset, thus displaying inconsistent results when the log files change or are deleted.
I have pushed a potential fix in v2.4.3 which taps into the Octane request lifecycle and clears the Log Viewer state. Please try it when you get the chance :)
Keep in mind that the Octane/Swoole cache driver isn't supported at the moment due to Swoole's limitations to key/value size. That's why the LOG_VIEWER_CACHE_DRIVER
environment variable exists. Use redis
, file
, or array
as cache drivers for Laravel to make sure the log file's index has somewhere to go and speeds up further requests. The array
cache driver might be undesirable in Swoole environment as well, because of the requests hitting different octane workers which might not have the array cache from previous requests. So perhaps avoid array
driver with Octane ๐
Let me know how it works out!
Thanks a lot @arukompas!
I reverted back to redis for caching and this is all working great now.
FYI: the new version (v2.4.3) never made it to packagist for some reason: https://packagist.org/packages/opcodesio/log-viewer so I had to test with dev-main
.