0

I have a strange situation. I have a cronjob running queue tasks one after the other. I have a delay of 1s between the tasks.

After a task is run, it sets processed=1, and continues with the next one, that has processed=0.

I have some situations where a task is executed twice. This means that processed=1 was set, but when we read again for processed=0, it finds it.

I use PHP, and ->execute(). Is it possible changes in the database not to be reflected immediately for some reasons?

3
  • Do not use CRON - use MySQL Scheduler. Do not use separate tasks (and processed flag) - use one "hyper-task" which executes separate tasks one-by-one.
    – Akina
    Commented Jun 11, 2020 at 11:36
  • @Akina i do prefer to use my current architecture. But will check mysql scheduler, thanks!
    – Aris
    Commented Jun 11, 2020 at 11:55
  • Why even use MySQL Scheduler? Won't it have the same problem as "cron"? Instead, simply have a separate task continually running. (You could have a cronjob performing a "keep-alive" on that task.)
    – Rick James
    Commented Jun 11, 2020 at 16:39

1 Answer 1

0

The issue was that since the queue run for 1 minute, there were cases that one cronjob was not finished yet, and another was starting. So they were processing the same tasks from the database.

The solution is to run cronjob using flock(), thus making sure that only 1 is run:

* * * * * /usr/bin/flock -n /tmp/fcj.lockfile /usr/local/bin/frequent_cron_job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.