1

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?

Vishnukk
  • 524
  • 2
  • 11
  • 27

1 Answers1

0

Step 1. Once your delayed job is complete it will have to save the output of your synchronize method to the database.

Step 2. On the 'UI' side, write a method that sends a GET request to the server at regular intervals.

Step 3. Your controller method checks the database & either returns 'Nil' or it returns a JSON object of the output of the delayed_job.

zoilism
  • 86
  • 1
  • 4