dssj88098 2019-08-09 15:00
浏览 100

在特定条件下停止执行cron作业

I have a DB with 2 tables:

subscribers:
 _____________________________
|  id    |  name  |   email   |
|________|________|___________|
|  1     |  John  |   e@m.com |
|  ..    |   ..   |     ..    |
|  5000  |  Mark  |   c@b.com |
|________|________|___________|
last_id:
 _______
|  id   |
|_______|
| NULL  |
|_______|

Every day I run a cron job to send all the emails at once, But The script is timing out after 100 sec.

So instead I'm thinking of sending about 200 emails per time:

/* Get the id from last time is script was executed */
$last_id = "SELECT `id` FROM `last_id`";

/* Select the next 200 subscribers starting from the last id */
$query= "SELECT * FROM `subscribers` WHERE `id` > ". $last_id . " LIMIT 200";

/* Send 200 emails */
..

/* Get the last id of these 200 subscribers */
$last = ;

/* Update the value of `id` in `last_id` table */
$query= "UPDATE `last_id` SET `id` = ". $last;

The cron job runs this script every 10 minutes, But after sending all the emails the script would be executed over and over without sending any emails.

So how to stop executing this cron job after all the emails are sent, When last_id = 5000 and start it again the next day?

  • 写回答

1条回答

  • douwen3836 2019-08-09 16:07
    关注

    We had an exercise like this at school once, we sorted it by adding a 'send date' column to the subscriber table. You select with a limit X and a date < then 'now', upon sending you update the records of these subscribers with the 'now' date.

    The query wont select anything if all are send. so the cron will run but doesn't do anything.

    And the next day, it will start with again as the select will return the 'unsend' records.

    And if you don't want to use the dates, you can use a bool or tinyint, select where 'send' is 0 and set them to 1 after sending. And have another cron update all the records back to 0 run once daily/or whatever increment you want to send these emails.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题