activerecord - rails - Prefetching records to process in a queue -
i have active record relation defined this:
contacts = contact.where("status = 'waiting'")
then, following:
if contacts batch_id = randomstringoflength(32) #set processing contacts.update_all(status: 'processing', batch_id: batch_id) #todo: best way this? contacts = contact.where("batch_id = ?", batch_id) contacts.each |contact| executefor(contact) end end
as can see, i'm having update records specific batch_id
in order later able fetch them.
this because first instance of contacts doesn't fetch records. first database call update status processing
, fetching them batch_id allows me run each loop.
is there better way this? although have indexed batch_id
think there might better way in rails.
if don't update batch_id
, remove line fetch batch_id
, .each
not return because status updated.
thanks
if not need batch_id
in database can write following:
contacts = contact.where(status: 'waiting') if contacts #set processing contacts.update_all(status: 'processing') #todo: best way this? contacts = contact.where(status: 'processing') contacts.each |contact| executefor(contact) end end
and option may little bit faster:
if contact.where(status: 'waiting').update_all(status: 'processing') > 0 contacts = contact.where(status: 'processing') contacts.each |contact| executefor(contact) end end
do not forget check indexes on database. status needs one.
keeping batch_id
:
batch_id = randomstringoflength(32) if contact.where(status: 'waiting').update_all(status: 'processing', batch_id: batch_id) > 0 contacts = contact.where(batch_id: batch_id) contacts.each |contact| executefor(contact) end end
Comments
Post a Comment