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?

asked Jun 11, 2020 at 7:26
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. Commented Jun 11, 2020 at 11:36
  • @Akina i do prefer to use my current architecture. But will check mysql scheduler, thanks! 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.) 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
answered Jun 15, 2020 at 6:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.