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



85
86
87
# File 'lib/event_store_client/client.rb', line 85

def connection
  @connection
end

#service_nameObject

rubocop:enable Metrics/CyclomaticComplexity



85
86
87
# File 'lib/event_store_client/client.rb', line 85

def service_name
  @service_name
end

Instance Method Details

rubocop:disable Metrics/CyclomaticComplexity



76
77
78
79
80
81
82
# File 'lib/event_store_client/client.rb', line 76

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



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
59
60
61
62
63
64
65
66
# File 'lib/event_store_client/client.rb', line 34

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.append_to_stream(stream, 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, resolve_links: true) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/event_store_client/client.rb', line 16

def read(stream, direction: 'forward', start: 0, all: false, resolve_links: true)
  if all
    connection.read_all_from_stream(
      stream, start: start, direction: direction, resolve_links: resolve_links
    )
  else
    connection.read(
      stream, start: start, direction: direction, resolve_links: resolve_links
    )
  end
end

#stop_pollingObject



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

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

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

Raises:



28
29
30
31
32
# File 'lib/event_store_client/client.rb', line 28

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