Laravel 6 - Exception Serialization of 'Closure' is not allowed - php

Running into an issue when turning my Mailable into a Queue
When the construct is disabled and the variable is injected directly into the build method, the job loads to the job table without issue, yet the job fails when the worker arrives due to the construct being missing with the following error: Illuminate\Contracts\Container\BindingResolutionException: Unable to resolve dependency [Parameter #0...
That said, when I enable the construct and remove the variable from the build method, I get an Exception Serialization of 'Closure' is not allowed.
Goal is to convert this send() to a queue() so that the db update can be performed ahead of an email being triggered.
Controller
public function store(ContactsStoreRequest $request)
{
$request->validated();
$submission = Contact::create(request()->all());
$request->id = $submission->id;
$emails = explode(',', config('mail.mailto'));
$when = now()->addMinutes(1);
Mail::to($emails)
->bcc(['notifications#xxxx.com'])
->later($when, new ContactSubmission($request));
return redirect('/contact/#loc')->with('status', 'success');
}
Mail Class
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactSubmission extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $topic;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($topic)
{
$this->topic = $topic;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.contact-submission')
->subject('XXX - Contact Submission #' . $this->topic->id)
->replyTo($this->topic->email_address);
}
}
Any help would be appreciated here!

Related

Basic Laravel job throws: "Serialization of 'Closure' is not allowed" on failure

I have created a very basic queue job:
<?php
namespace App\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class FailingJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 1;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
// nothing
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
// nothing
}
/**
* Called when the job fails
*
* #param Exception $exception
*/
public function failed(Exception $exception) {
// nothing
}
}
and I dispatch it like so: FailingJob::dispatch();. This works fine.
Now I am purposefully adding "bugged" code in order for the job to fail, but instead of gracefully making the job fail,
I get the following exception:
Serialization of 'Closure' is not allowed.
I'm not sure where this is coming from, because this job is basically empty. I would like to use the failed() function in order to handle failures, but I can't even do that because this exception gets thrown when the job fails.
For reference, this is the code I use to make the job fail:
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
xxxxxx
// ^ This will make the job fail
}

The failed() method is not being called when an Job fails in Laravel 5.4

Currently im creating a job to be queued but for some reason the failed() method is not being triggered when the code executed in the handle() method of the job throws an exception. Is this some kind of bug in Laravel or am I doing something wrong? the Queue::failing() method in my AppServiceProvider IS being called
<?php
namespace App\Jobs;
use App\Models\Email;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyCommand implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $lead;
/**
* Create a new job instance.
*/
public function __construct($lead)
{
$this->lead = $lead;
}
/**
* Execute the job.
*/
public function handle()
{
throw new \Exception("error!");
}
/**
* The job failed to process.
*
* #param \Exception $exception
*/
public function failed(\Exception $exception)
{
\Log::critical('wooot');
}
}
In the above job. The failed() method is never being called. We use supervisor to keep the queue running
Thank
It looks like the failed() method was being called but because of the $lead property not being public. I got an exception when I wanted to retrieve the contents of the property.

Trying to get property of non-object in Job Laravel

I'm trying to create job to register users and tried to follow Jeffrey's video but looks like dispatchfrom is removed for some reason. This is what i'm trying to do now:
This is my controller:
public function PostSignUp(Request $request)
{
dispatch(new RegisterUser($request->all()));
return 'done';
}
This is my job:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RegisterUser implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $request;
/**
* Create a new job instance.
* #param $request
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = $this->request->email;
var_dump('I should register user with email:' . $email);
}
}
I also tried to put
just $request instead of $request->all()
but then i get
Serialization of 'Closure' is not allowed
And now I'm getting Trying to get property of non-object error. Is this good way to pass whole request to job ? Should i do it some other way ?
try with input()
$request->input()
When you do $request->all(), that means you are passing an array to the job and not the whole $request. Therefore, you can simply do this in your Job Handler.
public function handle()
{
$email = $this->request['email'];
var_dump('I should register user with email:' . $email);
}
Let me know if something else is required :)

Laravel queue with Twitter api

I'm using the thujohn/twitter package on https://github.com/thujohn/twitter to update user status posts from my site to twitter.
I want to use larvae's queues to post this function in the background. But i keep getting a failed-job. with an error:
exception 'Exception' with message '[220] Your credentials do not allow access to this resource.' in /home/vagrant/sites/pms/vendor/thujohn/twitter/src/Thujohn/Twitter/Twitter.php:297
Stack trace:
my job looks like this:
namespace PMS\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Twitter;
use Session;
use Illuminate\Http\Request;
class PostToTwitter implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public $tweetLength;
public $userPage;
public $body;
//public $twitterToken;
//public $secret;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($tweetLength, $userPage, $body)
{
$this->tweetLength = $tweetLength;
$this->userpage = $userPage;
$this->body = $body;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Twitter::postTweet(['status' => str_limit($this->body . $this->userpage,$this->tweetLength), 'format' => 'json']);
}
}
and in my controller I'm dispatching like this:
$this->dispatch(new PostToTwitter($tweetLength, $userPage, $body));
my function works fine if i run it in my controller , but if i try to dispatch it to a job, my jobs fail
I managed to fix this by switching to a different library, following this blog post: http://markusos.github.io/engage/2014/12/06/1000-tweets-per-week.html

Using form request data in Laravel 5.3 mailable views

Looking around I cant find much information on how to do this. Plenty of the same examples just explaining the basic implementation of the mailable class but thats it.
I am sending an email from a contact form and have a route set up for testing that my form posts to:
Route::post('/sendmail', function() {
Mail::to("my email.com")->send(new MyTestMail());
});
I then have my mailable class which I have passed a test variable as a test string to:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyTestMail extends Mailable
{
use Queueable, SerializesModels;
public $test;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
$this->test = "test";
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.myTestMail');
}
}
What I wish to do is have access to my post values from the contact from and use them in the email view.
You can try passing the data you need to the constructor of that mailable.
public $postdata;
public function __construct($data)
{
$this->postdata = $data;
}
Now 'postdata' will be available to your view.
Mail::to("my email.com")->send(new MyTestMail($request->all()))

Categories