Class: PerfectSched::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/perfectsched/client.rb', line 21

def initialize(config)
  @config = {}
  config.each_pair {|k,v| @config[k.to_sym] = v }

  @backend = Backend.new_backend(self, @config)

  @timezone = @config[:timezone] || 'UTC'
  @max_acquire = @config[:max_acquire] || 1
  @alive_time = @config[:alive_time] || 300
  @retry_wait = @config[:retry_wait] || 300  # TODO retry wait algorithm
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



33
34
35
# File 'lib/perfectsched/client.rb', line 33

def backend
  @backend
end

#configObject (readonly)

Returns the value of attribute config.



34
35
36
# File 'lib/perfectsched/client.rb', line 34

def config
  @config
end

Instance Method Details

#acquire(options = {}) ⇒ Object

:now => Time.now :max_acquire



94
95
96
97
98
99
# File 'lib/perfectsched/client.rb', line 94

def acquire(options={})
  alive_time = options[:alive_time] || @alive_time
  max_acquire = options[:max_acquire] || 1

  @backend.acquire(alive_time, max_acquire, options)
end

#add(key, type, options = {}) ⇒ Object

:next_time => Time.now :next_run_time => Time.now :cron :data :delay => 0 :timezone => UTC

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/perfectsched/client.rb', line 50

def add(key, type, options={})
  cron = options[:cron]

  raise ArgumentError, ":cron option is required" unless cron

  delay = options[:delay] || 0
  timezone = options[:timezone] || @timezone
  data = options[:data] || {}

  next_time = options[:next_time] || Time.now.to_i
  next_time = PerfectSched.cron_time(cron, next_time.to_i, timezone)

  next_run_time = options[:next_run_time]
  if next_run_time
    next_run_time = next_run_time.to_i
  else
    next_run_time = next_time + delay
  end

  @backend.add(key, type, cron, delay, timezone, data, next_time, next_run_time, options)

  # TODO return value
  return next_time, next_run_time
end

#closeObject



124
125
126
# File 'lib/perfectsched/client.rb', line 124

def close
  @backend.close
end

#delete(key, options = {}) ⇒ Object



75
76
77
# File 'lib/perfectsched/client.rb', line 75

def delete(key, options={})
  @backend.delete(key, options)
end

#finish(task_token, options = {}) ⇒ Object



120
121
122
# File 'lib/perfectsched/client.rb', line 120

def finish(task_token, options={})
  @backend.finish(task_token, options)
end

#get_schedule_metadata(key, options = {}) ⇒ Object



40
41
42
# File 'lib/perfectsched/client.rb', line 40

def (key, options={})
  @backend.(key, options)
end

#heartbeat(task_token, options = {}) ⇒ Object

:alive_time => nil



108
109
110
111
112
# File 'lib/perfectsched/client.rb', line 108

def heartbeat(task_token, options={})
  alive_time = options[:alive_time] || @alive_time

  @backend.heartbeat(task_token, alive_time, options)
end

#init_database(options = {}) ⇒ Object



36
37
38
# File 'lib/perfectsched/client.rb', line 36

def init_database(options={})
  @backend.init_database(options)
end

#list(options = {}, &block) ⇒ Object



88
89
90
# File 'lib/perfectsched/client.rb', line 88

def list(options={}, &block)
  @backend.list(options, &block)
end

#modify(key, options = {}) ⇒ Object

:next_time => nil :next_run_time => nil :cron => nil :delay => nil :timezone => nil



84
85
86
# File 'lib/perfectsched/client.rb', line 84

def modify(key, options={})
  @backend.modify(key, options)
end

#release(task_token, options = {}) ⇒ Object



101
102
103
104
105
# File 'lib/perfectsched/client.rb', line 101

def release(task_token, options={})
  alive_time = options[:alive_time] || @alive_time

  @backend.release(task_token, alive_time, options)
end

#retry(task_token, options = {}) ⇒ Object



114
115
116
117
118
# File 'lib/perfectsched/client.rb', line 114

def retry(task_token, options={})
  alive_time = options[:retry_wait] || @retry_wait

  @backend.heartbeat(task_token, alive_time, options)
end