Sha256: 2c2702183308ac97dc2b5aa9a986f21120164f58148c9a89ce11c17a4454664f

Contents?: true

Size: 1.24 KB

Versions: 13

Compression:

Stored size: 1.24 KB

Contents

module SalesforceStreamer
  class Server
    attr_writer :push_topics
    attr_reader :client

    def initialize(push_topics: [])
      @push_topics = push_topics
    end

    def run
      Log.info 'Starting server'
      catch_signals
      reset_client
      EM.run { subscribe }
    end

    def restart
      Log.info 'Restarting server'
      reset_client
      EM.next_tick { subscribe }
    end

    private

    def catch_signals
      %w[INT USR1 USR2 TERM TTIN TSTP].each do |sig|
        trap sig do
          puts "Caught signal #{sig}. Shutting down..."
          exit 0
        end
      end
    end

    def reset_client
      @client = Restforce.new
      client.authenticate!
      Configuration.instance.faye_extensions.each do |extension|
        Log.debug %(adding Faye extension #{extension})
        extension.server = self if extension.respond_to?(:server=)
        client.faye.add_extension extension
      end
    end

    def subscribe
      @push_topics.each do |topic|
        client.subscribe topic.name, replay: Configuration.instance.replay_adapter do |message|
          Log.info "Message #{message.dig('event', 'replayId')} received from topic #{topic.name}"
          topic.handle message
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
salesforce_streamer-2.12.1 lib/salesforce_streamer/server.rb
salesforce_streamer-2.12.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.11.2 lib/salesforce_streamer/server.rb
salesforce_streamer-2.11.1 lib/salesforce_streamer/server.rb
salesforce_streamer-2.11.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.10.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.9.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.8.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.6.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.5.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.4.0 lib/salesforce_streamer/server.rb
salesforce_streamer-2.4.0.rc1 lib/salesforce_streamer/server.rb
salesforce_streamer-2.3.0 lib/salesforce_streamer/server.rb