douxiexie3574 2019-06-03 15:31
浏览 86
已采纳

如何在X时间后仅发送一次电子邮件

I am making a PHP alarm notification that will send an email if a temperature set point has been reached.

I have CRONJOB that runs in every 1 minute, runs a PHP script that verifies the temperature and if the temperature reaches to a point, it will send an email notification to me.

The problem is, that it will send me a email notification every minute while the temperature is passed the set point due to the CRONJOB. I would like for it to only send me an email once. Not send one minute.

I am using MYSQL to store the alarm information. If I need to some how setup time stamp in mysql and have the script verify the time and send it during x amount of time.?

Thanks,

  • 写回答

1条回答 默认 最新

  • duangu6431 2019-06-03 15:56
    关注

    You could either do the calculation in your application code or directly in the DB. Here's some pseudo code.

    Application Code

    CONST NOTIFICATION_DURATION = '30 minutes';
    
    $mostRecentNotification = query('SELECT MAX(created_at) FROM notifications');
    
    // Notice we're doing the calculation in our application code.
    $nextAvailableNotification = $mostRecentNotification + NOTIFICATION_DURATION;
    
    if (current_time >= $nextAvailableNotification) {
        send_notification();
    }
    

    If you choose to go with the application code version, I highly recommend using a library like Carbon (https://carbon.nesbot.com/) to help you out with your date/time arithmetic. It truly is a life saver even with simple calculations like this one.

    Database Version

    CONST NOTIFICATION_DURATION = '30 minutes';
    
    // Notice we're doing our calculation in our database
    $nextAvailableNotification = query('SELECT MAX(created_at) + INTERVAL ' . NOTIFICATION_DURATION . ' FROM notifications');
    
    if (current_time >= $nextAvailableNotification) {
        send_notification();
    }
    

    You could further compress the database version by doing the comparison directly in your query but I'll leave that up to you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单