Creating a Rails clockwork notification system

Hi everyone,

In an application I am working on, a notification system is currently running with the following characteristics:

Through the clockwork gem, every one second all services created by users are evaluated and those that are open or unresolved are filtered.

every(1.seconds, 'Uptading Booking Invatations') {
  Service.notification_sender
}

then, for each unsolved service, it is evaluated whether 15 seconds have passed since the last time employees were notified about that service.

in notification_sender

service.each do |s|
  next if 15_seconds_have_not_passed_yet
  s.send_notification_to_employees

If 15 seconds have passed, a notification is sent to the employees, who can take the service, that it would cease to be open at the time it is taken.

This system is highly expensive because it evaluates all services every second.

What happens is that it is done this way because this guarantees that the notification is sent to the employees at the moment the service is created and then every 15 seconds until it is taken.

If a clockwork were implemented that directly worked every 15 seconds, costs would be saved, but the newly created service could have a delay of 14.99999 seconds before reaching the employees.

Can you think of a better way to implement this functionality? it can even be with another gem or another system.

How should it works

  • a user requests a service.

  • all available employees are notified of that service.

  • if a user takes the service, the service changes state (from open to taken)

  • if the service is still open 15 seconds later, a new notification is sent to all available employees.