We have automation to login into different websites and perform some operations on website accounts. It is required that only one BOT can login simultaneously into a specific account to avoid inconsistency in automation flow.
We have a table in which we have stored website credentials:
|---------------------|------------------|------------------------|------------------------|
| id | email | locked_at | last_fetched_at |
|---------------------|------------------|------------------------|------------------------|
| 1 | [email protected] |2020-09-14 14:35:35 UTC |2020-09-14 14:35:35 UTC |
|---------------------|------------------|------------------------|------------------------|
| 2 | [email protected] | NULL | NULL |
|---------------------|------------------|------------------------|------------------------|
| 3 | [email protected] |2020-09-14 14:35:35 UTC |2020-09-14 14:35:35 UTC |
|---------------------|------------------|------------------------|------------------------|
| 4 | [email protected] | NULL | NULL |
|---------------------|------------------|------------------------|------------------------|
We fetch credentials with this query to be precise:
SELECT `credentials`.* FROM `credentials` WHERE `credentials`.`locked_at` IS NULL ORDER BY last_fetched_at asc LIMIT 1
Then we update the locked_at field with current time to lock the credential row for next process.
This is happening in a node.js app with mysql as backend db and being accessed by multiple BOT processes concurrently. We want to make sure two process don't get same credentials & used transactions/ select for update to make this operation atomic but no good successful way/query to do so so far.
We are open to any third party integration like redis or is there is something in node which we can use to achieve this.
Thank you for your time.
bot_connection
with CONNECTION_ID() of the process which reserves the row. Bot tries to update the row with its own connection ID then checks does it was successful. If a row with its ID is present yet then the row is successfully reserved, if not then some another bot re-reserves this row concurrently, and current bot must try to reserve another row.locked_at
column must be updated too - this will allow some service procedure to find and reset the rows if something happens with the bot which reserves it (hangs, for example). – Brainstormingfor update
withselect
statement for row level locking – Trenton