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 | abc@gmail.com |2020-09-14 14:35:35 UTC |2020-09-14 14:35:35 UTC |
|---------------------|------------------|------------------------|------------------------|
| 2 | def@gmail.com | NULL | NULL |
|---------------------|------------------|------------------------|------------------------|
| 3 | xyz@gmail.com |2020-09-14 14:35:35 UTC |2020-09-14 14:35:35 UTC |
|---------------------|------------------|------------------------|------------------------|
| 4 | ran@gmail.com | 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.