Skip to content

Commit

Permalink
scheduler command
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorOlikov committed May 13, 2024
1 parent 0267074 commit 966b784
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 8 deletions.
42 changes: 42 additions & 0 deletions app/Console/Commands/SendDiscounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Console\Commands;

use App\Mail\Discounts;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Mail;

class SendDiscounts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:send-discounts';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Отправляет письма пользователям с информацией о скидках';

/**
* Execute the console command.
*/
public function handle()
{
User::whereNotNull('email_verified_at')->chunk(200, function (Collection $users) {
foreach ($users as $user) {
Mail::to($user->email)->send(new Discounts($user));
}
});




}
}
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\SendDiscounts;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -12,7 +13,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->command(SendDiscounts::class)->weekly();
}

/**
Expand Down
10 changes: 9 additions & 1 deletion app/Jobs/OrderPaidNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Jobs;

use App\Mail\OrderPaid;
use App\Models\Order;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class OrderPaidNotification implements ShouldQueue
{
Expand All @@ -30,6 +32,12 @@ public function __construct(
*/
public function handle(): void
{
//

Mail::to($this->user->email)
->send(new OrderPaid(
$this->user,
$this->order
));
}

}
5 changes: 3 additions & 2 deletions app/Mail/Discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
Expand All @@ -16,7 +17,7 @@ class Discounts extends Mailable
/**
* Create a new message instance.
*/
public function __construct()
public function __construct(public User $user)
{
//
}
Expand All @@ -37,7 +38,7 @@ public function envelope(): Envelope
public function content(): Content
{
return new Content(
view: 'view.name',
view: 'mails.discounts',
);
}

Expand Down
6 changes: 4 additions & 2 deletions app/Mail/OrderPaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Mail;

use App\Models\Order;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
Expand All @@ -16,7 +18,7 @@ class OrderPaid extends Mailable
/**
* Create a new message instance.
*/
public function __construct()
public function __construct(public User $user, public Order $order)
{
//
}
Expand All @@ -37,7 +39,7 @@ public function envelope(): Envelope
public function content(): Content
{
return new Content(
view: 'view.name',
view: 'mails.order-paid',
);
}

Expand Down
2 changes: 1 addition & 1 deletion docker/development/worker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ COPY ./development/worker/conf/supervisord.conf /etc/supervisord.conf
WORKDIR /app


CMD [ "/usr/bin/supervisord", "-n", "-c", "/etc/supervisord.conf" ]
CMD [ "/usr/bin/supervisord","-n", "-c", "/etc/supervisord.conf" ]



Expand Down
3 changes: 2 additions & 1 deletion docker/development/worker/conf/worker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
process_name=%(program_name)s_%(process_num)02d
command=php /app/artisan queue:work --tries=3
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/app/storage/logs/worker.log
autorestart=true


6 changes: 6 additions & 0 deletions resources/views/mails/discounts.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<span>
Здравствуйте, {{ $user->name }} , для вас появились новые скидки!
</span>
<a href="{{config('app.url')}}">
<button>Посмотреть</button>
</a>
11 changes: 11 additions & 0 deletions resources/views/mails/order-paid.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
<div>
<p>Здравствуйте {{ $user->name }}</p>
<p>Заказ № {{ $order->id }} на сумму {{ $order->amount }} рублей, успешно оплачен!</p>
</div>
<div>
<a href="{{ config('app.url') . "/api/v1/order/$order->id" }}">
<button>Информация о заказе</button>
</a>
</div>
</div>

0 comments on commit 966b784

Please sign in to comment.