Can I use queue_classic to make a synchronized serial queue?

63 views
Skip to first unread message

Felipe Cypriano

unread,
Oct 8, 2013, 5:20:59 PM10/8/13
to queue_...@googlegroups.com
Hello guys,
 a
I've posted this questions on SO: Rails, how can I make a synchronous serial queue than works in multi-thread/process and I'm wondering if QC could be used to solve the problem. I'll reproduce the SO question here in case you don't want to go there or prefer here.


In my Rails 4 with Ruby 2 app I've the following model:

A Playlist can have many Tracks and each Track has its position in its Playlist. For each new Track I want to set its position to be the last, in thread-safe and/or multi-process-safe way.
In my research I found a bunch of projects that handle background jobs but I don't want this to be background, I want it to be synchronous. So the caller of my API will get the response with the correct position.
The question is: what's the best way to make a synchronous serial queue that will be used to set the position of a Track in its Playlist?


From what I've seen in QC docs the perfect solution for me would be to have dynamic queues (possible?) and one for each Playlist. But it might be too much for such a simple operation like that.

This is what the worker would execute for each Track that gets added:

    def set_next_position
      unless self.playlist_id.nil?
        max = Track.where(playlist_id: self.playlist_id).maximum(:position) || 0
        self.position = max + 1
      end
    end

I want to avoid problems where another Track could be added in between the `select max` and `max + 1` so I don't end up with duplicate positions.


Thanks,
---
Felipe Cypriano

P.S.: Sorry Ryan that I sent you a direct email I missed the link to this group the first time I read the README.

Yves Senn

unread,
Oct 9, 2013, 5:56:40 AM10/9/13
to queue_...@googlegroups.com
Hey Felipe

Queue Classic is designed for asynchronous background jobs. I don't think QC is a viable solution for your problem.
I'd suggest to use your databases locking / transaction functionality to solve the problem.

Cheers,
-- Yves

Felipe Cypriano

unread,
Oct 9, 2013, 10:48:30 AM10/9/13
to queue_...@googlegroups.com
Thanks Yves. I'll search for other alternatives. Maybe using PostgreSQL arrays to hold track ids in order, but this is a discussion to another group.


--
You received this message because you are subscribed to the Google Groups "queue_classic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to queue_classi...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages