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 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办