Class: EventStoreClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_client/client.rb

Constant Summary collapse

NoCallMethodOnSubscriber =
Class.new(StandardError)
WrongExpectedEventVersion =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

rubocop:enable Metrics/CyclomaticComplexity



77
78
79
# File 'lib/event_store_client/client.rb', line 77

def connection
  @connection
end

#service_nameObject

rubocop:enable Metrics/CyclomaticComplexity



77
78
79
# File 'lib/event_store_client/client.rb', line 77

def service_name
  @service_name
end

Instance Method Details

rubocop:disable Metrics/CyclomaticComplexity



68
69
70
71
72
73
74
# File 'lib/event_store_client/client.rb', line 68

def link_to(stream:, events:, expected_version: nil)
  raise ArgumentError if !stream || stream == ''
  raise ArgumentError if events.nil? || (events.is_a?(Array) && events.empty?)
  connection.link_to(stream, events, expected_version: expected_version)
rescue StoreAdapter::Api::Client::WrongExpectedEventVersion => e
  raise WrongExpectedEventVersion.new(e.message)
end

#poll(interval: 5) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/event_store_client/client.rb', line 26

def poll(interval: 5)
  return if @polling_started
  @polling_started = true
  thread1 = Thread.new do
    loop do
      create_pid_file
      Thread.handle_interrupt(Interrupt => :never) do
        begin # rubocop:disable Style/RedundantBegin
          Thread.handle_interrupt(Interrupt => :immediate) do
            broker.call(subscriptions)
          end
        rescue Exception => e # rubocop:disable Lint/RescueException
          # When the thread had been interrupted or broker.call returned an error
          sleep(interval) # wait for events to be processed
          delete_pid_file
          error_handler&.call(e)
        ensure
          # this code is run always
          Thread.stop
        end
      end
    end
  end
  thread2 = Thread.new do
    loop do
      sleep 1
      break unless thread1.alive?
      thread1.run
    end
  end
  @threads = [thread1, thread2]
  nil
end

#publish(stream:, events:, expected_version: nil) ⇒ Object



10
11
12
13
14
# File 'lib/event_store_client/client.rb', line 10

def publish(stream:, events:, expected_version: nil)
  connection.publish(stream: stream, events: events, expected_version: expected_version)
rescue StoreAdapter::Api::Client::WrongExpectedEventVersion => e
  raise WrongExpectedEventVersion.new(e.message)
end

#read(stream, direction: 'forward', start: 0, all: false) ⇒ Object



16
17
18
# File 'lib/event_store_client/client.rb', line 16

def read(stream, direction: 'forward', start: 0, all: false)
  connection.read(stream, direction: direction, start: start, all: all)
end

#stop_pollingObject



60
61
62
63
64
65
# File 'lib/event_store_client/client.rb', line 60

def stop_polling
  return if @threads.none?
  @threads.each(&:kill)
  @polling_started = false
  nil
end

#subscribe(subscriber, to: [], polling: true) ⇒ Object



20
21
22
23
24
# File 'lib/event_store_client/client.rb', line 20

def subscribe(subscriber, to: [], polling: true)
  raise NoCallMethodOnSubscriber unless subscriber.respond_to?(:call)
  @subscriptions.create(subscriber, to)
  poll if polling
end