Class: Restforce::DB::Worker

Inherits:
Object
  • Object
show all
Includes:
FileDaemon
Defined in:
lib/restforce/db/worker.rb

Overview

Restforce::DB::Worker represents the primary polling loop through which all record synchronization occurs.

Constant Summary collapse

DEFAULT_INTERVAL =
5
DEFAULT_DELAY =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileDaemon

included

Constructor Details

#initialize(options = {}) ⇒ Worker

Public: Initialize a new Restforce::DB::Worker.

options - A Hash of options to configure the worker’s run. Currently

supported options are:
interval - The maximum polling loop rest time.
delay    - The amount of time by which to offset queries.
config   - The path to a client configuration file.
verbose  - Display command line output? Defaults to false.


31
32
33
34
# File 'lib/restforce/db/worker.rb', line 31

def initialize(options = {})
  @options = options
  @interval = @options.fetch(:interval) { DEFAULT_INTERVAL }
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/restforce/db/worker.rb', line 21

def logger
  @logger
end

#trackerObject

Returns the value of attribute tracker.



21
22
23
# File 'lib/restforce/db/worker.rb', line 21

def tracker
  @tracker
end

Instance Method Details

#startObject

Public: Start the polling loop for this Worker. Synchronizes all registered record types between the database and Salesforce, looping indefinitely until processing is interrupted by a signal.

Returns nothing.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/restforce/db/worker.rb', line 41

def start
  DB.reset
  DB.configure do |config|
    config.parse(@options[:config])
    config.logger = logger
  end

  %w(TERM INT).each { |signal| trap(signal) { stop } }

  loop do
    runtime = Benchmark.realtime { perform }
    sleep(@interval - runtime) if runtime < @interval && !stop?

    break if stop?
  end
end

#stopObject

Public: Instruct the worker to stop running at the end of the current processing loop.

Returns nothing.



62
63
64
65
# File 'lib/restforce/db/worker.rb', line 62

def stop
  Thread.new { log "Exiting..." }
  @exit = true
end