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.

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站