L
L
Lexxtor2016-05-15 21:29:30
MySQL
Lexxtor, 2016-05-15 21:29:30

How to send a queue of letters by several threads?

I am sending out letters.
I made a queue in the form of a table in MySql. The table has fields: id and status.
Status can have the following values: 'awaits','sending','sended','error','delivered','opened','clicked'.
How to competently make mailing of letters from this queue so that it can work in several threads?
So far, I came up with the following solution:
Cron or something else, as often as possible, runs a PHP script that makes a request:

SELECT * FROM mail WHERE status='awaits' ORDER BY id LIMIT 10
then takes min and max id from the result and does
UPDATE  mail SET status='sending' WHERE id >= 5 AND id <= 14

Thus, the thread will mark a portion of letters so that another thread does not take it. These 2 requests should be made inside a transaction so that another thread does not take the same data between them.
Then these 10 letters are sent, their status in the database changes to 'sent' and the thread ends.
If you have to send a lot, then instead of cron I use something that can run several threads simultaneously, and not a maximum of 1, once a minute.
If you offer rabbitMQ, then explain what it is and what the essence of its work is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-05-16
@Rsa97

At first

SET @id := 0;
UPDATE `mail` 
  SET `status` = `sending` 
  WHERE (@id := `id`) AND `status` = 'awaits' 
  ORDER BY `id`
  LIMIT 1;

Then
the script can be run from cron once a minute, but make a loop in it. While there are tasks to send, the script should not end. Then the number of scripts will grow every minute until everything is sent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question