Updating status when an ActiveJob process is finished

I have a CSV file import process running in background using ActiveJob and Sidekiq.

is it possible to update a status notification when background job is finished? Or do I have to use ActionCable?

1 Like

You should use ActionCable. I was able to wrap it all into a Stimulus controller that uses this kind of setup:

<div data-controller='download' data-download-slug="<%= @tool.slug %>">
  <a href='#' class='btn-norm' data-action='download#downloadFile'>Download Tool</a>
</div>

The downloadFile method subscribes to the channel, then makes a call to the download process on the server, and handles the received file when it comes in from the channel.

The main part of the controller looks like this (note I’m using some jQuery here, but the concepts don’t rely on it at all):

downloadFile(e) {
  e.preventDefault()
  const slug = this.data.get('slug')
  this.subscribeTo(slug)

  // Request the file by ajax
  $.get(`/tools/${ slug }.pdf`)
}

subscribeTo(slug) {
  App.downloads = App.cable.subscriptions.create({ channel: "DownloadsChannel", slug: slug }, {
    connected: () => {
      return $('.download-blocker').fadeIn('fast');
    },

    disconnected: () => {},

    received: (data) => {
      App.downloads.unsubscribe()
      this.downloadFile(data['filename'], atob(data['file_data']))
    }
  })
}
4 Likes