Class: Hackle::PollingWorkspaceFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hackle/workspaces/polling_workspace_fetcher.rb

Constant Summary collapse

DEFAULT_POLLING_INTERVAL =
10

Instance Method Summary collapse

Constructor Details

#initialize(config:, http_fetcher:) ⇒ PollingWorkspaceFetcher

Returns a new instance of PollingWorkspaceFetcher.



10
11
12
13
14
15
16
# File 'lib/hackle/workspaces/polling_workspace_fetcher.rb', line 10

def initialize(config:, http_fetcher:)
  @logger = config.logger
  @http_fetcher = http_fetcher
  @current_workspace = Concurrent::AtomicReference.new
  @task = Concurrent::TimerTask.new(execution_interval: DEFAULT_POLLING_INTERVAL) { poll }
  @running = false
end

Instance Method Details

#fetchObject



18
19
20
# File 'lib/hackle/workspaces/polling_workspace_fetcher.rb', line 18

def fetch
  @current_workspace.get
end

#pollObject



37
38
39
40
41
42
# File 'lib/hackle/workspaces/polling_workspace_fetcher.rb', line 37

def poll
  workspace = @http_fetcher.fetch
  @current_workspace.set(workspace)
rescue => e
  @logger.error { "Failed to poll Workspace: #{e.inspect}" }
end

#start!Object



22
23
24
25
26
27
28
# File 'lib/hackle/workspaces/polling_workspace_fetcher.rb', line 22

def start!
  return if @running

  poll
  @task.execute
  @running = true
end

#stop!Object



30
31
32
33
34
35
# File 'lib/hackle/workspaces/polling_workspace_fetcher.rb', line 30

def stop!
  return unless @running

  @task.shutdown
  @running = false
end