javascript - Callback method after for delayed job - render js alert -


i have app delayed job. want use callback once action finish (after, success or finish) , inform user on client side. have this:

class scannerjob < applicationjob   queue_as :default    def perform(links, count = 0)     links |url|        broadcast_it(url.to_s, count +=1)     end     after   end    def after     respond_to |format|       format.js { render :js => "alert('congratulations');" }     end   end 

i got this: unterminated string meets end. found source telling impossible it.

what best way handle , why can't call respond js?

thanks!

yeah it's impossible it. let's find out why. positive how normal http request cycle work. in traditional way client asks server fullfill request. whether it's querying data or it's sending data store on server. server handle client's request , send response client. after server has full filled client's request there no way server gonna communicate client unless client asks/request again. clients requests server , server responds request , wait next request.

no let's go delayed job stuff. know why add delayed job in our project. there might performance hungry or time taking tasks delay user response if handle them synchronously . i-e

assume want send email 100 people separately on user's request , each email takes 5 seconds deliver. take 500 seconds (around 9 minutes) deliver of those.

we cannot in synchronous way bad experience user. user request timeout or if not timeout user treat non responding request , leave site.

so we process these email sending tasks background. , whenever user request send email 100 people send response user @ once , tell them success message. , ask delayed job handle task in background.

class mycontroller  < applicationcontroller def process_heavy_requestion    emailersender.delay.send_email_in_backgroun(params) #we delegating email sending task delayed job. , assume done.    render text: "email sent successfully" end end 

in action responded user message. , sending email in background.

so if map above discussion server has responded client success message. if wanna send response user afterwords it's not possible. unless user asks again.

so let's solve problem.

you want inform use actual progress of background task here way. whenever task executed in background can set flag in user table/or other table. status of task. can add column in custom worker.

before calling background task need change value of column 'pending'. in delayed job worker set value of column accordingly i-e (failed or done)

so task output stored somewhere in database or can store in user session.

so whenever user asks handle request handle taks in background , javascript add script in response check result of task after fixed time i-e (5 seconds) . javascript send request server after every x seconds status of previous task . if server responds it's been done show user success message or if fail show user error message.

i hope clear confusion


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -