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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题