Summer Sale! All accounts are 50% off this week.

Annaro's avatar

Can you pass a Laravel\Dusk\Browser to a job?

I am trying to dispatch a part of code that requires a browser (used in the code before) but i get this error: Serialization of 'CurlHandle' is not allowed

I do not know what to do with this. I ended up dispatching the whole content of my controller to avoid this, so now i instantiate my Laravel\Dusk\Browser inside the job and it works fine. But I would rather have separated the whole thing in several jobs if possible. Is there a way to do this while reusing the same Laravel\Dusk\Browser that you are already using?

My job now looks something like this:

public function handle()
    {
        try
        {
            $process = (new ChromeProcess)->toProcess();
            $process->start();
            $browser = $this->createBrowser($process);
        }
        catch (\Exception $e) {
            if(isset($browser)){$browser->quit();}   
            if(isset($process)){$process->stop();}

            $error_name = 'ERR_BROWSER_CREATION';
            $message = Error::generateError($this->user, $error_name, $e->getMessage());
        }

        $message = $this->login($browser);

        if($message == null) 
        {
                $number_cycles = $this->user->sequence->number_cycles;
        
                for ($i = 1; $i <= $number_cycles; $i++)
                {      
                    foreach($this->user->sequence->actions as $action)
                    {
                            $args = json_decode($action->args, true);
                            $message .= $this->runOneAction($browser, $action->action_type, $args);
                            // RunOneActionJob::dispatch($browser, $action->action_type, $args);                    
                    }
                }
        }
        $browser->quit();
        $process->stop();

        $this->saveMessagesInLogFile($message);
    }

I would have liked to dispatch the runOneAction function only, instead of what i did. I do need to reuse the $browser though. Thanks.

0 likes
0 replies

Please or to participate in this conversation.