For a user action, I need to send email to all of his subscribers. In this case, the emails should be queued to send later.
I used jobs for that, which can accept single user instance at a time (followed Laravel Doc) and it inserts a job in job table. This is fine.
Now, as the subscribers number is more that one, how can I add multiple user instance or jobs at a time in the jobs table? In Laravel 5.2, how can I achieve that?
I'm not sure if i'm missing something here by reading your question, but if you are implementing your own job queue, couldnt you just change the constructor to accept an collection (array) of users instead and in the handle method simply run a foreach which would email them?
Example in Laravel docs modified to accept a collection of users instead of single user:
<?php
namespace App\Jobs;
use App\User;
use App\Jobs\Job;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendReminderEmail extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $users = [];
/**
* Create a new job instance.
*
* #param User $user
* #return void
*/
public function __construct($users) //Pass in an array of your user objects
{
$this->users = $users;
}
/**
* Execute the job.
*
* #param Mailer $mailer
* #return void
*/
public function handle(Mailer $mailer)
{
foreach($users as $currentUser){
$mailer->send('emails.reminder', ['user' => $currentUser], function ($){
//code here
});
$currentUser->reminders()->create(...);
}
}
}
Related
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.
Hello i have a laravel queue for save operations to do later
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use SumoCoders\Teamleader;
use Illuminate\Notifications\Messages\SlackMessage;
use Monolog\Handler\Slack;
use Illuminate\Support\Facades\Mail;
/**
* Class ProcessNotifications
*
* #package App\Jobs
* Worker for send the notifications slack / mail / teamleader in asincr way
*/
class ProcessNotifications implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $arrayDataNotification ;
protected $typeNotification;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($arrayDataNotification , $typeNotification)
{
$this->typeNotification = $typeNotification;
$this->arrayDataNotification = $arrayDataNotification;
\Log::debug('createNotif',$arrayDataNotification);
}
/**
* Execute the job.
*
* #return void
*/
public function handle($arrayDataNotification , $typeNotification)
{
//first get type of notification
\Log::debug('debug handle ',$this);
\Log::info('into handle '.$this->typeNotification);
if($this->typeNotification=='mail'){
//mail internal
}
if ($this->typeNotification=='slack'){
//notifications slack
}
if($this->typeNotification=='teamleader'){
//teamleader connection
}
}
}
For send a a new job to the queue i am using dispatch method :
$this->dispatch(new ProcessNotifications(['data1', 'data2', 'data3'], 'slack'));
I have all in ddbb in the job table , then params are ok
i setted my crontab by run schedule:run each 5 minutes, is launched ok , but on method schedule , when the method handle is called , the params are lost , and i have this in the function scheduler:
protected function schedule(Schedule $schedule)
{
Log::debug('running scheduler '.date("d-m-Y H:i:s"));
$schedule->job(ProcessNotifications::dispatch());
}
Then , the params in this point is lost, same if i run in console php artisan queue work i have :
Too few arguments to function App\Jobs`\ProcessNotifications::__construct()`
in my ddbb i have all params, but i dont know how recover or if this is the good way to call the queue ?
Ok i found, parameters under function handle not need parameters, this is serialized / unserialized automatically for the queue
My developer left , so I need to finish our project by myself.
I'was doing Automation using C# so, have some knowledge in coding.
Question is:
How correctly top up new user's balance?
I have in SQL table - users, where all users are stored. (user_id +
name/surname + reg date)
I have in SQL table user_balance, where all users with balance are
stored (user_id + balance ammount)
So, I need to somehow gift to a new user some money.
Do I need to work with blade view, and trying something with
#if user reg date == bla bla
sql query
#else
ignore
#endif
or, better to create with controllers, models?
As #Nate points out, model events will get you what you need, howver, I'd use the Creating event rather than Created as then you can set the balance as the record is saved, saving you the update query.
You definitely do not want to do this in the blade view. Try to keep all business logic out of view files and contained within controllers/models/event listeners, etc.
You can simplify this from the other answer by adding the event handling within the model's static boot method.
public static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->balance = 100;
});
}
Taken from another similar answer, you need to use events, when the user is created
Inside your User's model, you can create an event handlers like so:
/**
* The event map for the model.
*
* #var array
*/
protected $dispatchesEvents = [
'created' => \App\Events\UserCreatedEvent::class,
];
Then you can create an event like so:
UserCreatedEvent
<?php
namespace App\Events;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
class UserCreatedEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
* Create a new event instance.
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Then you can create a listener to create the balance:
UserCreatedListener
<?php
namespace App\Listeners;
use Illuminate\Support\Facades\Mail;
use App\Events\UserCreatedEvent;
class UserCreatedListener
{
/**
* Create the event listener.
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param UserCreatedEvent $event
*/
public function handle(UserCreatedEvent $event)
{
// update their balanace here
$event->user->update(['balance' => 1000]);
}
}
Then inside your eventserviceprovider.php add
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\UserCreatedEvent' => [
'App\Listeners\UserCreatedListener',
],
];
Hi I have a laravel job as follows:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Timeline\Timeline;
class testQueue 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(Timeline $timeline, $tag)
{
$tagData = $timeline->getTagFeed($tag)->getRankedItems();
if ($tagData) {
echo "boom";
}
}
}
I'm running it via a route as follows:
Route::get('/queue', function () {
$timeline= new Timeline();
$timeline->login("test", "testpwd");
Queue::later(5, new testQueue($timeline, "Testtag"));
});
Then on the commandline I ran:
php artisan queue:listen database
However, that one job is running 255 times instead of 1 times and exiting successfully.
What am I doing wrong?
The documentation states:
Binary data, such as raw image contents, should be passed through the
base64_encode function before being passed to a queued job. Otherwise,
the job may not properly serialize to JSON when being placed on the
queue.
So you shouldn't use public function handle(Timeline $timeline, $tag) (or public function handle(Instagram $currentInstagram, $tag) in your conversation, as the Timeline or something is binary data.
Delete the job after execution.
I implemented a laravel queue with the database driver, in an Ubuntu 14.04 server. I execute this code
php /path to app/artisan queue:listen --tries=3 --env=local
It says tries=3. But when I see the jobs table I see jobs with 22 attempts, how is this possible? it should try 3 times and then add it to failed_jobs table.
Also, what does reserved_at means in the jobs table?.
Thank you
Here is the job that, by the way, it works perfectly
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Notifiers\Im_Notification;
use App\Notifiers\Notification;
use App\Notifiers\Mail_Notification;
use App\Reservation;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NotifyPlaceReservationStatus extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
protected $notification;
protected $reservation;
protected $info_changed;
public function __construct(Notification $notification,Reservation $reservation)
{
$this->reservation = $reservation;
$this->notification = $notification;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$this->notification->notifyPlaceReservationStatus($this->reservation);
}
public function failed()
{
error_log('Job failed');
}
}
When you provide the number of tries in the CLI, Laravel worker is going to apply the tries limit only for those jobs currently in the queue (or in your unexecuted job table if you prefer). In order to make every execution of a job have a try limit, you should put a public $tries attribute in your Job class as stated in the Laravel documentation on Dispatching Jobs (for Laravel 5.4)
public $tries = 3;
protected $notification;
protected $reservation;
protected $info_changed;