Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Cron lock? #498

Open
robberteggermont opened this issue Oct 29, 2017 · 10 comments
Open

Cron lock? #498

robberteggermont opened this issue Oct 29, 2017 · 10 comments
Assignees

Comments

@robberteggermont
Copy link
Contributor

Multiple mails were sent out after an item sold. I suspect the cron code got executed more than once at the same time. This should be controlled by a locking mechanism (mutex; I could find one in the current code?).

@renlok
Copy link
Owner

renlok commented Dec 1, 2017

could this be done with database flag in the settings or something

@robberteggermont
Copy link
Contributor Author

robberteggermont commented Dec 4, 2017 via email

@MESWEB
Copy link
Contributor

MESWEB commented Dec 4, 2017

My script is working fine and I have one e-mail notification.

@robberteggermont
Copy link
Contributor Author

My script is working fine and I have one e-mail notification.

Your comment is not very helpful...

We had a serious problem with this: 3-4 duplicate rows in the winners table for dozens of auctions (using non-batch cron, when many auctions closed at the same time). Clearly multiple cron instances were processing the same auctions at the same time. This is a problem, it should not be possible. When you provide the option to use non-batch cron, it should work safely.

@renlok
Copy link
Owner

renlok commented Dec 6, 2017

9da4811 should work to stop multiple instances from running

@robberteggermont
Copy link
Contributor Author

robberteggermont commented Dec 6, 2017 via email

@renlok
Copy link
Owner

renlok commented Dec 13, 2017

OK I have replaced the database lock with a file lock that seems to be the most recommended way to deal with the problem

@renlok renlok self-assigned this Dec 13, 2017
@robberteggermont
Copy link
Contributor Author

I briefly looked at 1a73eb9 and the locking concept seems solid. (No rate-limiting, but that was just a nice extra.;-))

I'm wondering about the file path though, shouldn't that be something like MAINPATH . '/cache/cron.lock'? (I guess it should be under MAINPATH, and a more unique and descriptive name may come in handy if you every need another lock in the future?)

@renlok
Copy link
Owner

renlok commented Dec 14, 2017

Yeah your right

@ratfish
Copy link

ratfish commented Dec 14, 2020

Hi

I also had duplicate emails being sent when auctions closed concurrently and multiple people were online (1.2.2).

I solved it prior to finding this conversation.

I looked at the php flock but found it lacking
It has problems on nfs and vfat file systems
It has a race condition between setting and checking the lock
It has no way to detect or mitigate dead locks or looping scripts
it has issues with file path, ownership and permission (especially when both batch and non batch tasks are active)

The solution that is working for me utilizes a purposed database table which has a unique constraint on a field for lock name so the script may attain a lock that is atomic relative to the script and save an associated process identifier that is used to catch dead locks. If a script can insert a record with the locks name, it has obtained the lock and upon completion can delete the record to release the lock. If the insert fails the script can skip the cron tasks. when an actual crond task runs to check for dead locks, if the record exists and the process is not running it can remove the lock, if the process is running it can note the process identifier and address a looping script on its next processing cycle

To implement required
database table with unique constraint
clone and modify direct query to return status rather then log error in its catch clause
code to insert and delete the record in the script
and a script that detects deadlocks, this script can use ps on all *nix or /proc on linux

example available if anyone is interested

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants