当前位置:网站首页>Laravel queue consumption instance and timed task add task consumption
Laravel queue consumption instance and timed task add task consumption
2022-08-08 18:57:00 【owenzhang24】
本文环境 CentOS8.0,PHP8.1,Nginx1.8,Laravel 5.8
不懂的可以评论或联系我邮箱:[email protected]
著作权归OwenZhang所有.商业转载请联系OwenZhang获得授权,非商业转载请注明出处.

队列介绍
Laravel 队列为不同的后台队列服务提供统一的 API,例如 Beanstalk,Amazon SQS,Redis,甚至其他基于关系型数据库的队列.队列的目的是将耗时的任务延时处理,比如发送邮件,从而大幅度缩短 Web 请求和响应的时间.
队列配置文件存放在 config/queue.php 文件中.每一种队列驱动的配置都可以在该文件中找到,包括数据库, Beanstalkd, Amazon SQS, Redis,以及同步(本地使用)驱动.其中还包含了一个 null 队列驱动用于那些放弃队列的任务.
Queue task instance of consumption
<?phpnamespace App\Jobs;use Carbon\Carbon;use iBrand\ShuaBao\Core\Models\ActivityInviteBonus;use iBrand\ShuaBao\Core\Services\BalanceCashService;use Illuminate\Bus\Queueable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Redis;class ActivityInviteJob implements ShouldQueue{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $bonus_id; /** * Create a new job instance. * * @return void */ public function __construct($bonus_id, $delay=0) { $this->bonus_id = $bonus_id; $this->delay($delay); } /** * Execute the job. * * @return void */ public function handle(BalanceCashService $balanceCashService) { \Log::info('Start processing delay queue[助力活动-自动审核]: bonus_id:' . $this->bonus_id); // 审核通过 try { DB::beginTransaction(); $bonus = ActivityInviteBonus::where('id', $this->bonus_id)->lockForUpdate()->first(); if (!$bonus || $bonus->status != 3 || $bonus->is_delay != 1) { throw new \Exception('数据不存在'); } //提现金额 $data['amount'] = (int) $bonus->amount; $data['user_id'] = $bonus->user_id; $data['status'] = 0; $data['channel'] = 'wechat'; $data['subject'] = 'Power activities show'; // 发起提现 $charge_result = $balanceCashService->transfer($data); if (!$charge_result) { $bonus->phone_type .= ' (transfer false)'; \Log::info("Power activity delay withdrawal wrong", ['bonus_id', $this->bonus_id]); } $bonus->status = 4; $bonus->withdraw_at = Carbon::now(); $bonus->save(); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); $err_msg = $exception->getMessage(); \Log::info("Power activity delay withdrawal wrong", [$err_msg, 'bonus_id', $this->bonus_id]); } }}Call queue task
// Join the queue delay $delay = Carbon::tomorrow()->timestamp - Carbon::now()->timestamp + mt_rand(1, 600); $this->dispatch(new ActivityInviteJob(123, $delay)); Timing task add tasks consumption
<?phpdeclare(strict_types=1);/** * This file is part of the qlantech. * * (c) Qlantech <[email protected]> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */namespace App\Schedule;use iBrand\Scheduling\Scheduling;class Schedule extends Scheduling{ public function schedule() { //IP自动拉黑(用户登录) $this->schedule->command('AutoGrayIpCronJob:stat')->everyFiveMinutes(); //Update the new withdrawal behavior statements $this->schedule->command('ChannelNewCashUserActionReport')->dailyAt('03:01'); }}定时任务服务类
<?phpnamespace App\Console\Commands;use App\Jobs\ChannelCashUserRetentionReportJob;use Illuminate\Console\Command;class ChannelCashUserRetentionReport extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'ChannelCashUserRetentionReport {--date=}'; /** * The console command description. * * @var string */ protected $description = <<<EOFChannelCashUserRetentionReport Command: Usage: ChannelCashUserRetentionReport ChannelCashUserRetentionReport [--date='2021-07-17'] Options: --date={DATE} The default statistics for the front desk EOF; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $date = $this->option('date'); $date = $date ? date('Ymd', strtotime($date)) : date('Ymd', strtotime('-1 days')); dispatch_now((new ChannelCashUserRetentionReportJob($date))); }}<?phpnamespace App\Jobs;use iBrand\ShuaBao\Core\Services\Channel\ChannelCashUserRetentionService;use Illuminate\Bus\Queueable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;class ChannelCashUserRetentionReportJob implements ShouldQueue{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $date; /** * Create a new job instance. * * ChannelUserQualityDateJob constructor. * @param $date */ public function __construct($date) { $this->date = $date; } /** * Daily channel user behavior records. * * @param ChannelCashUserRetentionService $channelCashUserRetentionService * @return bool */ public function handle(ChannelCashUserRetentionService $channelCashUserRetentionService) { \Log::info(__CLASS__ . ': Start processing channel withdrawal retained users report task every day'); for ($i = 1; $i <= 8; $i ++) { $date = date('Ymd', strtotime("-{$i} days", strtotime($this->date))); $channelCashUserRetentionService->update($date); } $dateRange = date('Ymd', strtotime("-1 days", strtotime($this->date))) . '-' . date('Ymd', strtotime("-8 days", strtotime($this->date))); \Log::info(__CLASS__ . ': Processing task channels carry the user data is retained ' . $dateRange); return true; }}Buy me a cup of coffee :)
觉得对你有帮助,就给我打赏吧,谢谢!
边栏推荐
- Flush can buy stock?Is it safe to buy stocks?
- 重读GPDB 和 TiDB 论文引发的 HTAP 数据库再思考
- 【761. 特殊的二进制序列】
- 传音控股:目前公司手机产品暂无明确计划进入中国市场
- 面了个腾讯30k+出来的,他让我见识到什么叫精通MySQL调优
- QT With OpenGL (Bloom) (Bloom)
- ABAP 报表中如何给报表的输入参数增添 F4 Value Help
- hdu1042 N! (large number)
- oracle视图v$active_session_history,dba_hist_active_session_history如何记录IP地址
- 轻量全景查看器 pannellum初探
猜你喜欢

Style Design Principles in Open Office XML Format

Azure Neural TTS 持续上新,助力企业开拓小语种市场

面经刺客 | 关于——字节飞书基础架构产品 日常实习面经

Redis之SDS数据结构

Azure Neural TTS continues to be updated to help enterprises develop small language markets

uniapp parent component uses prop to pass asynchronous data to child components

2021年9月电子学会图形化三级编程题解析含答案:接红包游戏

Rethinking HTAP database caused by rereading GPDB and TiDB papers

第4讲:SQL语句之DDL类型的数据库定义语言

堆排序(Heap Sort)实现
随机推荐
蒲公英R300A 4G路由器,远程监控PLC教程
搭建DG导致归档日志量变多问题排查
响应式pbootcms模板建筑装饰类网站
Dataworks上的ODPS spark处理数据会比直接用ODPS SQL效率高吗?
Oracle--表
十六、一起学习Lua 文件 I/O
Redis Server启动过程
一起了解分层架构&SOA架构
请问shake数据库中mongoshake同步过程中,src_mongo挂了,同步服务不会退出吗?
hdu1042 N!(大数)
在线支付,出款和收款
视图,索引
Is there any function in MAXCOMPUTE SQL to judge whether the string is a number?
性能问题从发现到优化一般思路
关于求最小公倍数的三种常用方法
ABAP 报表中如何给报表的输入参数增添 F4 Value Help 试读版
黑磷量子点/无机荧光量子点/石墨烯量子点水凝胶/量子点/纳米水凝胶荧光探针的研究制备
熬夜拜读349页阿里面试通关手册,成功闯入字节
什么是Shell?从小白到入门你只差一个它
uva1432