polling with delayed_job

后端 未结 6 1882
[愿得一人]
[愿得一人] 2020-12-12 14:39

I have a process which takes generally a few seconds to complete so I\'m trying to use delayed_job to handle it asynchronously. The job itself works fine, my question is ho

6条回答
  •  悲&欢浪女
    2020-12-12 15:15

    I ended up using a combination of Delayed_Job with an after(job) callback which populates a memcached object with the same ID as the job created. This way I minimize the number of times I hit the database asking for the status of the job, instead polling the memcached object. And it contains the entire object I need from the completed job, so I don't even have a roundtrip request. I got the idea from an article by the github guys who did pretty much the same thing.

    https://github.com/blog/467-smart-js-polling

    and used a jquery plugin for the polling, which polls less frequently, and gives up after a certain number of retries

    https://github.com/jeremyw/jquery-smart-poll

    Seems to work great.

     def after(job)
        prices = Room.prices.where("space_id = ? AND bookdate BETWEEN ? AND ?", space_id.to_i, date_from, date_to).to_a
        Rails.cache.fetch(job.id) do
          bed = Bed.new(:space_id => space_id, :date_from => date_from, :date_to => date_to, :prices => prices)
        end
      end
    

提交回复
热议问题