Skip to content

Integration with sidekiq-status #111

Open
@riyaadh-abrahams

Description

@riyaadh-abrahams

Hi

I was looking for a good way to integrate with sidekiq-status, and came up with a base job class that will add the various statuses. Any feedback would be helpful, but maybe there could be a way to get something more integrated? I am more that willing to help with some guidance as I am sure this is not the best way

class BaseJob < Gush::Job
  include Sidekiq::Status::Worker

  def perform(*args)
    setup_initial_metadata(args)
    store_for_id id, initial_metadata, @expiration

    begin
      store_status id, :working, @expiration
      execute(*args)
      store_status id, :complete, @expiration
    rescue Sidekiq::Status::Worker::Stopped
      store_status id, :stopped, @expiration
    rescue SystemExit, Interrupt
      store_status id, :interrupted, @expiration
      raise
    rescue Exception
      store_status id, :failed, @expiration
      raise
    end
  end

  def store(data, expiry = @expiration)
    data.each do |key, value|
      store_for_id(id, { key => value }, expiry)
    end
  end

  private

  def setup_initial_metadata(args)
    @initial_metadata = {
      jid: id,
      status: :queued,
      worker: klass.to_s,
      workflow_data: as_json,
      args: args
    }
  end

  def execute(*args)
    # Override this method in your job class with the specific job logic
    raise NotImplementedError, "You must implement execute in your job class"
  end

  def initial_metadata
    @initial_metadata
  end

end

Then it can be used like this

  class JobOneJob < BaseJob

    def execute(*args)
      puts 'test'
      store message: 'wow'

    end
  end

If you set up sidekiq-status web ui, you can see the results like this

image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions