RoR equivalent to PHP's flush()?

#1

I’m trying to make a long running script (it runs for about 2 hours) display its progress and the coded in HTML updates as it runs rather than waiting until the script completes to show the entire result.

Having searched for answers, and Stack Overflow, I’ve found an answer, which is to use render stream: true in the controller. However, it doesn’t work with my view for some reason.

Controller

def import
    @shelfunit = @discogs.get_user_folders(@user.username)
  #  above is API connector for Discogs-Wrapper Gem

    render stream: true
  end

View (erb)

<h1>Title</h1>
<% 
# set some variables
# write to database
%>
<h2>Status Update</h2>
<%
# For each do
    # set some variables
    # write to database
%>
    <h2>Status Update</h2>
<%
# End For each
%>

It’s currently waiting until the script has finished to display anything at all, I’m getting no errors, but I want it to stream the view, and all the HTML as it reaches it rather than waiting.

Can anyone please advise where I’m going wrong?

#2

In Ruby you would not run code that writes to the database in your view. Instead you would use a background job, and have your view watch its status asynchronously.

Hope that helps.

#3

@how this is an admin user only view (or at least it will be) at deployment, I will look at chaniging it to a background job, but being new to Ruby/RoR (2 weeks experience at most) I’m probably stuck in PHPs methodology too

1 Like
#4

Keep it up! I recommend trying first with ActiveJob, then moving to Sidekiq or something similar.

#5

oh i will be carrying on with it @how i got into Ruby/RoR because of a job posting for a site I’m a member of that is built on RoR looking for developers. I’m not ready for it yet but they will be recruiting again soon enough by which time I will be :smiley:

Can you please point me at resources for asyncronous polling?

also,

How would I do the status updates in the job, as HTML in the job or something in the view?