Laravel queue with Twitter api - php

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

Related

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

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!

Laravel job ModelNotFoundException when dispatching from controller and running

I'm working with one of my Laravel 8 API project's that I've built where a user is able to add a domain to their account. The domain needs to be crawled to obtain expiration dates etc and is available in the user's account.
This process takes some time and so I've extracted the logic that fetches the domain data into a job called DomainExpiryChecker.
When a new domain is added, a job is dispatched, but may not execute straight away, and in some cases the user may even delete the domain before the job runs.
For some reason, even after dispatching my job after a record has been made, I'm getting the following failed job error and I'm not sure what I'm missing:
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Domains]
My controller where the job is dispatched resembles the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use App\Models\Domains;
use Carbon\Carbon;
use App\Jobs\DomainExpiryChecker;
class DomainsController extends Controller
{
/**
* Instantiate a new DomainsController instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Add a new domain
*
* #param Request $request
* #return Response
*/
public function add(Request $request)
{
$domain = new Domains();
$domain->domain = '';
$domain->crawled = 'pending';
$domain->has_valid_ssl = false;
$domain->user_id = Auth::id();
$domain->save();
// on-demand
if ($domain) {
DomainExpiryChecker::dispatch($domain)->onQueue('on-demand-runs-now');
}
// everything went okay!
return response()->json(['success' => true, 'message' => "Added $domain_name successfully, please allow between 24 and 48 hours for crawling"], 201);
}
}
Whilst my job looks like the following:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
use App\Models\Settings;
use App\Models\Domains;
use Carbon\Carbon;
class DomainExpiryChecker implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The domain instance.
*/
protected $domain;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($domain)
{
$this->domain = $domain;
}
/**
* Get single domain
*
* #return object
*/
public function getDomain($domain)
{
$domain = Domains::where('id', $domain['id'])
->first();
return $domain;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$updatableDomain = $this->getDomain($this->domain);
if (!$updatableDomain) {
return;
}
// stuff happens here
}
}
What am I missing, I'm sure it's just one thing.

Fatal error: Trait 'Illuminate\Foundation\Events\Dispatchable' not found in C:\xampp\htdocs\chat-app\app\Events\NewMessage.php on line 14

Using Pusher with Laravel 5.8 to send messages in real-time generates this error on NewMessage Event file.
Steps I took to try and debug:
tried removing the line "use Dispatchable, InteractsWithSockets, SerializesModels;" inside the Class;
tried without using the Classes at the top of the file
none of those worked. On Laravel 5.8 Documentation they don't mention using this line inside our Event Classes, maybe it's outdated?!
File looks like this:
`
namespace StyxEminus\Events;
use StyxEminus\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->to);
}
public function broadcastWith() {
return ["message" => $this->message];
}
}`
Local Server: Apache on Xampp;
Operating System: W10 64bit
Browser: Brave(chromium) & Chrome
you need import Illuminate\Foundation\Bus\Dispatchable

Sending Mails with Queues in Larvael

First time using queue in laravel i have a simple contact form page that user submit information and i am trying to use queue to receive that information. I am pretty sure that my setup is incorrect because when i run the queue it saying processing. My question is how come my data isn't sending and what the correct way of sending the data in the array.
AskEmailController.php
<?php
namespace App\Http\Controllers;
use App\Jobs\HelpEmailJob;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Carbon\Carbon;
class AskEmailController extends Controller
{
public function askemail()
{
return view('help.question');
}
public function store(Request $request)
{
HelpEmailJob::dispatch()
->delay(Carbon::now()->addSeconds(5));
}
}
HelpEmailJob
namespace App\Jobs;
use App\Mail\HelpEmailMailable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class HelpEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to('mikeshasaco#gmail.com')->queue(new HelpEmailMailable());
}
}
HelpEmailMailiable
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class HelpEmailMailable extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->$data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data = array(
'sirname' => $this->data['sirname'],
'email' => $this->data['email'],
'bodymessage' => $this->data['bodymessage'],
);
return $this->view('help.contactinfo')
->with([
'data' =>$data,
]);
}
}
You can directly send the mail from your controller like this:
class AskEmailController extends Controller
{
public function store(Request $request)
{
Mail::to('mikeshasaco#gmail.com')->send(new HelpEmailMailable);
}
}
instead of using a job. And to make the email queue by default by implementing the ShouldQueue interface like this:
class HelpEmailMailable extends Mailable implements ShouldQueue
{
...
}
Make sure your queue is running when you test this and always restart it after you make a change in the code.

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 :)

Categories