I have identical Python scripts I need to run on multiple servers all targeting the same table on a DB server. The script takes 5-20 seconds to run, and must run every 5 minutes.
Server1 ---> -------------
| DB Table |
Server2 ---> -------------
The script looks at a single table that looks like this:
Type | many other fields | DirtyBit | Owner
--------------------------------------------
X | ... | UnUsed | NULL
X | ... | UnUsed | NULL
X | ... | UnUsed | NULL
Y | ... | UnUsed | NULL
Y | ... | UnUsed | NULL
The script does the following:
Grab all records of type X (in a transaction) where
DirtyBit
isUnUsed
andOwner
isNULL
.,Update all the records, set
DirtyBit
toInUse
, andOwner
toServer1
.Perform some operations on the data in Python.
Update all the records according to the operations in 3. Set
DirtyBit
back toUnUsed
, andOwner
back toNULL
Because the script is running on multiple servers, the DirtyBit/Owner combination works to ensure the scripts aren't stepping on each other. Also, note that each row in the table is independent of all the others.
Question: is this a sensible approach to getting the scripts to run concurrently? Is there anyway the database can handle this for me (maybe changing the Transaction Isolation Level?). Ideally, I want this, if the scripts happen to run at the same time:
Script on Server 1 starts running.
Script on Server 2 starts running, notices that 1 is running, and thus decides it doesn't need to run.
Script on Server 1 finishes, updates all the data.