is there any way to redirect (like redirect_to) the url from worker, modules in rails?

459 views Asked by At

Hi I am processing some background jobs and I need to redirect the URL from the module or directly from the worker but as per my knowledge, there is only one method i.e redirect_to but it's not available in module and worker as per the rails MVC architecture but I need to do this.

Please see below is my code:-

@oauth = Koala::Facebook::OAuth.new(Figaro.env.fb_app_id,Figaro.env.fb_secret_token,Figaro.env.fb_callback_url)
oauth_code_url = @oauth.url_for_oauth_code
redirect_to oauth_code_url

I have also included the include ActionController::UrlFor to get the redirect_to method in Module and Worker but it's again throwing the error and I was not able to call controller methods into the module or worker. could anyone please suggest what would be the best approach to do this?

1

There are 1 answers

3
Leonel Galán On

Redirects only make sense inside the Request/Response cycle, workers usually happen on the background and asynchronously, so the user that might have initiated isn't waiting for the worker's response.

If you do want to wait (run the worker synchronously), it's not up to the worker to redirect, the worker should simply "signal" the controller to perform the redirect (this is because you want to keep a separation of concerns).