Class: Rapid::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid/task.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, content, attr) ⇒ Task

Returns a new instance of Task.



7
8
9
10
11
12
13
14
# File 'lib/rapid/task.rb', line 7

def initialize klass, content, attr
  @klass = klass
  @content = content
  @message_ttl = attr[:message_ttl]
  @routing_key = attr[:routing_key] 
  @max_attempts = attr[:max_attempts] || 1
  @attempt_counter = 0
end

Instance Method Details

#args=(args) ⇒ Object



16
17
18
# File 'lib/rapid/task.rb', line 16

def args= args
  @args = args
end

#enqueueObject



45
46
47
48
# File 'lib/rapid/task.rb', line 45

def enqueue
  q = Rapid::get_channel.queue(underscore('rapid_'+@klass.to_s), durable: true)
  q.publish(to_json, persisted: true)# TODO: message_ttl, routing_key
end

#metaclassObject



3
4
5
# File 'lib/rapid/task.rb', line 3

def metaclass
  class << self; self; end
end

#method_name=(method_name) ⇒ Object



20
21
22
# File 'lib/rapid/task.rb', line 20

def method_name= method_name
  @method_name = method_name
end

#to_jsonObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rapid/task.rb', line 24

def to_json
  {
    klass: @klass,
    content: @content,
    message_ttl: @message_ttl,
    routing_key: @routing_key,
    max_attempts: @max_attempts,
    attempt_counter: @attempt_counter,
    method_name: @method_name,
    args: @args,
  }.to_json
end

#underscore(camel_cased_word) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rapid/task.rb', line 37

def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr('-', '_').
  downcase
end