Class: DhanMarketFeedWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/generators/dhanhq/install/templates/market_feed_worker.rb

Overview

Keeps a DhanHQ market feed connection open and broadcasts ticks over ActionCable. Start it with DhanMarketFeedWorker.perform_async -- the job blocks for the life of the connection rather than completing immediately, so Sidekiq's dashboard reflects whether the feed is actually running.

retry: false because there is nothing to retry: the underlying DhanHQ::WS::Client already reconnects and re-subscribes on its own.

Instance Method Summary collapse

Instance Method Details

#perform(mode = "quote") ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/dhanhq/install/templates/market_feed_worker.rb', line 14

def perform(mode = "quote")
  client = DhanHQ::WS.connect(mode: mode.to_sym) do |tick|
    ActionCable.server.broadcast("dhan_market_feed", tick)
  end

  client.on(:reconnect) { |info| Rails.logger.warn("[DhanMarketFeedWorker] reconnect ##{info[:attempt]}") }
  client.on(:error) { |message| Rails.logger.error("[DhanMarketFeedWorker] #{message}") }

  loop do
    sleep 30
    break unless client.connected?
  end
end