doulandai0641 2018-02-18 19:55
浏览 31
已采纳

cron是在php中编写基于提醒的应用程序的最佳方式吗?

Suppose I have a database like this.

| task                      |  time                     | 
|---------------------------|---------------------------| 
| Remind me to water plants |  2018-02-18T19:32:10.486Z | 
| English Test              |  2018-10-8T09:30:00.000Z  | 
| Math Test                 |  2018-10-8T10:30:00.000Z  | 

And I want to send a reminder notification to the user at the time it has been requested (or maybe an hour before) by the user and also execute some other block of code at that specific time when reminder notification is triggered.

I am thinking of writing a cron job something like this

* * * * * php /laravel-project/artisan schedule:run >> /dev/null 2>&1

as mentioned in laravel documentation (https://laravel.com/docs/5.6/scheduling).

And inside that script, I will be looping onto current time reminders and reminders within next hour and perform actions.

Is it a good practice? Won't it increase traffic between PHP and database? What if i have lot of reminders within the same hour, looping into that and executing blocks of code foreach reminder will be it a good idea?

Please suggest ideas

  • 写回答

2条回答 默认 最新

  • dongpu3792 2018-02-18 20:02
    关注

    Using the task scheduler is the correct way to go about this. If you have a large number of reminders to process then setting them up to execute as queued jobs is the recommended approach. That way the load on the server is minimized and does't bottleneck the system for user traffic.

    You'll probably want to create a console command to handle the processing logic and create the jobs. Chunking the jobs will also be helpful, as that will stagger out the number of jobs processed in a given batch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格