Z3. Debug Guideline - swooletw/laravel-swoole GitHub Wiki
Debug Guideline
Although this package provides a sandbox container to prevent properties of container being influenced by your codes, sometimes there are still other unexpected issues. This guideline can shed some light on your debugging.
- Make some changes in your
config/swoole_http.php
'server' => [
'options' => [
'daemonize' => false,
'worker_num' => 1,
]
],
'ob_output' => false,
Limiting your worker number is to make sure your requests won't be processed by different workers. Turn off
ob_output
can help you dump some debug message on the console.
- For example, if you get the same
auth
user in every request, you can try to dump some message inIlluminate\Auth\AuthServiceProvider::class
protected function registerAuthenticator()
{
$this->app->singleton('auth', function ($app) {
var_dump('auth resoved');
// the reset code
}
}
Restart your server and inspect your console if your auth
instance being resolved every time.
- There are some pre-resolved instances that will be shared by sandbox container:
'view', 'files', 'session', 'session.store', 'routes',
'db', 'db.factory', 'cache', 'cache.store', 'config', 'cookies',
'encrypter', 'hash', 'router', 'translator', 'url', 'log'
These instances won't be resolved in every request. You can still set specific instances to instances
config in swoole_http.php
to reset them like below:
After v2.5.0, you can customize your pre-resolved list in
pre_resolved
inswoole_http.php
config.
/*
|--------------------------------------------------------------------------
| Instances here will be cleared on every request.
|--------------------------------------------------------------------------
*/
'instances' => [
'log', 'cache'
],
- Some providers might use dirty app to register their instances, for example, in
PaginationServiceProvider
:
Paginator::currentPageResolver(function ($pageName = 'page') {
$page = $this->app['request']->input($pageName);
// ...
});
The app variable is not injected from the closure. It's from the service provider itself. However, service providers are only registered in the very beginning of booting up application. So, the request
instance here won't be refreshed. The better way should be:
Paginator::currentPageResolver(function ($pageName = 'page') use ($app) {
$page = $app['request']->input($pageName);
// ...
});
So this package provides a solution for this kind of situations. You can specify the service providers you want to re-register every time. Just add your service providers to providers
config in swoole_http.php
:
/*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
Illuminate\Pagination\PaginationServiceProvider::class,
],
The package will replace the app in service container with the sandbox app automatically.
-
If the packages you installed or any providers are related to authentication(eg. Laravel Passport), please reset them manually.
-
If you install other packages in your application, you should try to make a minimum implementation in a clean Laravel project first to see if the bug still happens. Some packages might have some dirty codes to cause unexpected bugs.