When you switch from the "sync" queue driver to the "database" driver, Laravel serializes the job to store it in the database. This serialization process can cause issues with certain types of objects, especially those that represent resources or have open connections, such as an SSH session.
In your ProcessPackBuildJob class, you have a public $ssh property that is not initialized in the constructor. This property is likely causing the serialization issue because it's an instance of Ssh which cannot be serialized properly.
To resolve this issue, you should create the Ssh instance within the handle method rather than storing it as a property of the job class. This ensures that the Ssh instance is fresh and not subject to serialization when the job is dispatched to the queue.
Here's how you can modify your handle method:
public function handle(): void
{
try {
// ... existing code ...
// Initialize the SSH connection within the handle method
$ssh = Ssh::create($secureCdnUser, $secureCdnHost)
->disablePasswordAuthentication()
->usePrivateKey($secureCdnKey)
->disableStrictHostKeyChecking()
->setTimeout(100)
->usePort($secureCdnPort);
// ... rest of your code ...
} catch (Throwable $e) {
// ... existing error handling code ...
}
}
By making this change, you ensure that the Ssh instance is not serialized along with the job, which should resolve the issue you're experiencing when using the "database" queue driver. Remember to remove the public $ssh property from your class if it's not used elsewhere.
Additionally, make sure that your environment variables are correctly set for the queue worker process. Sometimes, environment variables can be different when running CLI commands (like queue workers) compared to web server processes. If necessary, use a configuration file or pass the SSH configuration directly to the job constructor to ensure the correct values are used.