0

I am running some functions in background, but now I don't want to run those functions in background because perform function only returns a job Id I want to show error message why the perticular job is failed. Does it possible to show error message why the sidekiq job is failed.

  • That's not how asynchronous job processors work. They're two separate apps that run from a shared code base; how would they communicate if they're separate apps? How would it be asynchronous if your Rails app waited for a response for Sidekiq? The typical thing to do is to have your asynchronous job write some return value to a database, but how you read that from Rails is up to you. If you want to get the return status from a job by running it synchronously and directly in Rails you can run [`Worker.new.perform`](https://stackoverflow.com/a/19255259/3784008) (but this is not async) – anothermh Feb 25 '20 at 06:12
  • Actually my question is that how to return messages from workers to controller methods? – Diksha Dhakate Feb 25 '20 at 06:25
  • 1
    You can't return anything from an asynchronous worker to Rails. Not how it works. If you need to perform some logic and return it to your controller action then either do `Worker.new.perform` to run it synchronously, or move that code to some other part of your codebase. (which makes more sense, because if it's not meant to be run asynchronously then it shouldn't be a Sidekiq worker) If this doesn't make sense to you then please provide code samples with a clearer explanation of how you're using the given code. – anothermh Feb 25 '20 at 06:33
  • Ya now I have written a concern, from their am calling that function.Thanks @anothermh – Diksha Dhakate Feb 25 '20 at 06:37

1 Answers1

0

It is possible, in some ways (I only have done 2):

  1. You create a log file for background job -> this is suitable if only the admin/developer who can read why the jobs were failed;

  2. You record the status in your DB -> this is suitable if you want user to see if their transaction is success or not, but keep in mind backgroundjob is running asynchronously. You can show why the job is failed, but not in real-time. User can see it in index or show, depends on your business model.

Either way, you have to use rescue.

KSD Putra
  • 487
  • 2
  • 7