amphp / parallel-functions

Simplified parallel processing for PHP based on Amp.
https://amphp.org/parallel-functions
MIT License
268 stars 18 forks source link

Call to a member function connection() on null #14

Closed andreshg112 closed 4 years ago

andreshg112 commented 5 years ago

I have a database call in the callback. Is that a problem?

{
  "message": "Uncaught Error in worker with message \"Call to a member function connection() on null\" and code \"0\"",
  "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
  "file": "/var/www/vendor/amphp/parallel/lib/Worker/Internal/TaskFailure.php",
  "line": 45,
  "trace": [
    {
      "file": "/var/www/vendor/amphp/parallel/lib/Worker/TaskWorker.php",
      "line": 126,
      "function": "promise",
      "class": "Amp\\Parallel\\Worker\\Internal\\TaskFailure",
      "type": "->"
    },
    {
      "function": "Amp\\Parallel\\Worker\\{closure}",
      "class": "Amp\\Parallel\\Worker\\TaskWorker",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Coroutine.php",
      "line": 76,
      "function": "send",
      "class": "Generator",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Internal/Placeholder.php",
      "line": 130,
      "function": "Amp\\{closure}",
      "class": "Amp\\Coroutine",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Coroutine.php",
      "line": 81,
      "function": "resolve",
      "class": "Amp\\Coroutine",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Internal/Placeholder.php",
      "line": 130,
      "function": "Amp\\{closure}",
      "class": "Amp\\Coroutine",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Coroutine.php",
      "line": 81,
      "function": "resolve",
      "class": "Amp\\Coroutine",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Internal/Placeholder.php",
      "line": 130,
      "function": "Amp\\{closure}",
      "class": "Amp\\Coroutine",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Deferred.php",
      "line": 45,
      "function": "resolve",
      "class": "class@anonymous/var/www/vendor/amphp/amp/lib/Deferred.php0x7fb3039da2d7",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/byte-stream/lib/ResourceInputStream.php",
      "line": 99,
      "function": "resolve",
      "class": "Amp\\Deferred",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Loop/NativeDriver.php",
      "line": 206,
      "function": "Amp\\ByteStream\\{closure}",
      "class": "Amp\\ByteStream\\ResourceInputStream",
      "type": "::"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Loop/NativeDriver.php",
      "line": 97,
      "function": "selectStreams",
      "class": "Amp\\Loop\\NativeDriver",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Loop/Driver.php",
      "line": 134,
      "function": "dispatch",
      "class": "Amp\\Loop\\NativeDriver",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Loop/Driver.php",
      "line": 72,
      "function": "tick",
      "class": "Amp\\Loop\\Driver",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/Loop.php",
      "line": 84,
      "function": "run",
      "class": "Amp\\Loop\\Driver",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/amphp/amp/lib/functions.php",
      "line": 170,
      "function": "run",
      "class": "Amp\\Loop",
      "type": "::"
    },
    {
      "file": "/var/www/vendor/spatie/laravel-collection-macros/src/macros/parallelMap.php",
      "line": 31,
      "function": "Amp\\Promise\\wait"
    },
    {
      "function": "{closure}",
      "class": "Illuminate\\Database\\Eloquent\\Collection",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php",
      "line": 107,
      "function": "call_user_func_array"
    },
    {
      "file": "/var/www/app/Http/Controllers/EdgesController.php",
      "line": 193,
      "function": "__call",
      "class": "Illuminate\\Support\\Collection",
      "type": "->"
    },
    {
      "function": "path",
      "class": "App\\Http\\Controllers\\EdgesController",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
      "line": 54,
      "function": "call_user_func_array"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
      "line": 45,
      "function": "callAction",
      "class": "Illuminate\\Routing\\Controller",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
      "line": 212,
      "function": "dispatch",
      "class": "Illuminate\\Routing\\ControllerDispatcher",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
      "line": 169,
      "function": "runController",
      "class": "Illuminate\\Routing\\Route",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
      "line": 665,
      "function": "run",
      "class": "Illuminate\\Routing\\Route",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 30,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Router",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/barryvdh/laravel-cors/src/HandleCors.php",
      "line": 36,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Barryvdh\\Cors\\HandleCors",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
      "line": 41,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 104,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
      "line": 667,
      "function": "then",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
      "line": 642,
      "function": "runRouteWithinStack",
      "class": "Illuminate\\Routing\\Router",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
      "line": 608,
      "function": "runRoute",
      "class": "Illuminate\\Routing\\Router",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
      "line": 597,
      "function": "dispatchToRoute",
      "class": "Illuminate\\Routing\\Router",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
      "line": 176,
      "function": "dispatch",
      "class": "Illuminate\\Routing\\Router",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 30,
      "function": "Illuminate\\Foundation\\Http\\{closure}",
      "class": "Illuminate\\Foundation\\Http\\Kernel",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/fideloper/proxy/src/TrustProxies.php",
      "line": 57,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Fideloper\\Proxy\\TrustProxies",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
      "line": 31,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
      "line": 31,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
      "line": 27,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
      "line": 62,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/barryvdh/laravel-cors/src/HandlePreflight.php",
      "line": 29,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 151,
      "function": "handle",
      "class": "Barryvdh\\Cors\\HandlePreflight",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
      "line": 53,
      "function": "Illuminate\\Pipeline\\{closure}",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
      "line": 104,
      "function": "Illuminate\\Routing\\{closure}",
      "class": "Illuminate\\Routing\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
      "line": 151,
      "function": "then",
      "class": "Illuminate\\Pipeline\\Pipeline",
      "type": "->"
    },
    {
      "file": "/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
      "line": 116,
      "function": "sendRequestThroughRouter",
      "class": "Illuminate\\Foundation\\Http\\Kernel",
      "type": "->"
    },
    {
      "file": "/var/www/public/index.php",
      "line": 55,
      "function": "handle",
      "class": "Illuminate\\Foundation\\Http\\Kernel",
      "type": "->"
    }
  ]
}
trowski commented 5 years ago

I'm guessing your worker environment needs to be initialized in some way.

What does TaskError::getWorkerTrace() return?

kelunik commented 4 years ago

There hasn't been any further activity, so I'm closing this issue. Please comment with further information, we can always reopen the issue.

kmuenkel commented 2 years ago

The problem is the App instance falls out of scope when the handler Closure gets serialized, so Laravel abstractions become unusable therein. Additionally, Model classes need some special handling when it comes to reconnecting with the database, so if you include any in your Closure's static variables, they won't work properly.

Fortunately, Laravel already figured out how to handle these issues in the context of PhpUnit tests and Queued Jobs, respectively. Specifically, the CreatesApplication and SerializesAndRestoresModelIdentifiers traits. So I've created a wrapper package around this package that leverages those traits to integrates it with Laravel a little better.

https://github.com/kmuenkel/parallel-collection