Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Dalibor Tomic
Dalibor Tomic
273 Points

Is it possible to execute functions after redirection to other page?

For example:

post '/create' do
  data = {
    :owner => params[:email],
    :hostname => params[:hostname],
    :ip => params[:ip],
    :path => params[:path]
  logger.info "Backup request: #{data}"
  save_content(data, 'backup', @conf)
  redirect to ('/welcome')
  client_ssh(data, @conf)
  email_approver(@conf, 'backup')

My client_ssh and email_approver never get executed. If I put redirect to /welcome at the end, they get executed but it takes time. Wanted to avoid client waiting experience.

Please help

2 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Redirect basically exits current action and executes a completely new action, a GET in this example, which either does its own thing or just displays something static, so it will not execute anything after the redirect. I guess this is what you'd call "blocking" execution, where every line gets executed once the previous one finished.

One way to solve this would be using some kind of queue system, where you'd send those longer jobs, and continue executing your code, while these jobs are done in the background and finish at some point later on. Resque with Redis is one popular solution, but these are not covered in Treehouse courses at this point, so you'd have to try implementing this on your own or find a course or article that would guide you through the basics.

Dalibor Tomic
Dalibor Tomic
273 Points

Hey Maciej Thanks for the response, I rally appreciate. I have found solution in Threding. Function I wanted to execute in background I put them in Thred.new block.

Thanks again