Earlier I had my controller class which was creating an instance of the service and calling the service method 'synchronize' as below. After the 'synchronize' method was getting completed, rails used to render a JSON message back to my node.js.
if @service.valid_connection?
@service.synchronize
render json: {
status: :unprocessable_entity,
errors: @service.message
}, status: :ok
else
render json: {
status: :unprocessable_entity,
errors: @event.errors.full_messages
}, status: :ok
end
However, since my 'synchronize' method was a bit long to execute, I have created a delayed job that takes up my 'synchronize' task. So currently my controller looks like this
if @service.valid_connection?
::Events::WegSyncJob.perform_later(event_id, b_cancelled)
else
render json: {
status: :unprocessable_entity,
errors: @event.errors.full_messages
}, status: :ok
end
Now I cannot place my render JSON here since it will get immediately executed after the job is passed on to delayed_job, I am making use of the after_perform method in my Job class as:
after_perform do |job|
//below code is wrong
render json: {
status: :ok,
message: "Check notifications"
}, status: :ok
end
def perform(event_id, b_cancelled)
//call to synchronize
end
However, I cannot invoke a 'render' from my Job class since it is possible only from a controller. How can I render back a JSON message to my node.js (UI) after the completion of this background job?