cron是用php编写基于提醒的应用程序的最佳方法吗? [英] Is cron the best way of writing reminder based application in php?

查看:64
本文介绍了cron是用php编写基于提醒的应用程序的最佳方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的数据库.

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

我想在用户请求提醒通知时(或者可能是一个小时之前)向用户发送提醒通知,并在触发提醒通知的那个特定时间执行其他一些代码块. /p>

我正在考虑写一份像这样的cron作业

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

如laravel文档( https://laravel.com/docs/5.6/scheduling).

在该脚本中,我将循环播放当前时间提醒,并在下一个小时内提醒并执行操作.

这是一个好习惯吗?它不会增加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

解决方案

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.

这篇关于cron是用php编写基于提醒的应用程序的最佳方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆