I recommend using the manufacturer template implemented with the new table as a "work queue". There is no need for a stored procedure because the trigger is so simple.
- The trigger will populate the work queue.
- The code will poll the work queue table. Since the table will be very small, the query will be fast and low in load.
- The code will do everything you need and remove rows from the table when they are completed - as little as possible
Create a table with the notification identifier for processing and the column "processing status", for example:
create table work_queue ( id int not null auto_increment, notification_id int references notifications, status enum ('ready', 'processing', 'failed') );
Create a simple trigger that populates the work queue table:
delimiter $ create trigger producer after insert on notifications for each row begin insert into work_queue (notification_id, status) select new.id, 'ready' where new.unread; end; $ delimiter ;
Your code will have a pseudo-code:
select * from work_queue where status = 'ready' order by id limit 1update work_queue set status = 'processing' where id = <row.id>- Do what you need,
notifications where id = <row.notification_id> - either
delete from work_queue where id = <row.id> , or update work_queue set status = 'failed' where id = <row.id> (you need to figure out what to do with the failed elements) - Sleep 1 second (this pause should be about the same as the maximum speed for sending notifications - you need to configure it to balance the size of workload and server load).
- go 1.
If you have one survey process, there is no need to block anxiety. If you have a survey of several processes, you will need to handle the race conditions.
source share