Class: Sidekiq::Superworker::Subjob

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Naming, ActiveModel::Validations
Defined in:
lib/sidekiq/superworker/subjob.rb

Constant Summary collapse

ATTRIBUTES =
[:subjob_id, :superjob_id, :parent_id, :children_ids, :next_id, :children_ids,
:subworker_class, :superworker_class, :arg_keys, :arg_values, :status, :descendants_are_complete,
:meta]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Subjob

Returns a new instance of Subjob.



92
93
94
95
96
97
98
99
100
101
# File 'lib/sidekiq/superworker/subjob.rb', line 92

def initialize(params={})
  if params.present?
    params.each do |attribute, value|
      public_send("#{attribute}=", value)
    end
    Sidekiq.redis do |conn|
      conn.sadd("#{self.class.redis_prefix}:#{superjob_id}:subjob_keys", "#{self.class.redis_prefix}:#{superjob_id}:#{subjob_id}")
    end
  end
end

Class Method Details

.allObject



50
51
52
# File 'lib/sidekiq/superworker/subjob.rb', line 50

def all
  keys.collect { |key| find_by_key(key) }
end

.countObject



54
55
56
# File 'lib/sidekiq/superworker/subjob.rb', line 54

def count
  keys.length
end

.create(attributes = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/sidekiq/superworker/subjob.rb', line 16

def create(attributes={})
  if attributes.is_a?(Array)
    attributes.collect { |attribute| create(attribute) }
  else
    object = new(attributes)
    object.save
    object
  end
end

.delete_subjobs_for(superjob_id) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/sidekiq/superworker/subjob.rb', line 65

def delete_subjobs_for(superjob_id)
  Sidekiq.redis do |conn|
    keys = conn.smembers("#{redis_prefix}:#{superjob_id}:subjob_keys")
    conn.del(keys) if keys.any?
    conn.del("#{redis_prefix}:#{superjob_id}:subjob_keys")
  end
end

.find_by_jid(jid) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/sidekiq/superworker/subjob.rb', line 26

def find_by_jid(jid)
  hash = Sidekiq.redis do |conn|
    conn.hgetall("#{redis_prefix}:#{jid}")
  end
  return nil if hash.blank?
  hash.collect do |key, value|
    hash[key] = ActiveSupport::JSON.decode(value)
  end
  new(hash)
end

.find_by_key(key) ⇒ Object



37
38
39
40
41
# File 'lib/sidekiq/superworker/subjob.rb', line 37

def find_by_key(key)
  return nil if key.blank?
  jid = key.split(':', 2).last
  find_by_jid(jid)
end

.find_by_superjob_jid(jid) ⇒ Object



43
44
45
46
47
48
# File 'lib/sidekiq/superworker/subjob.rb', line 43

def find_by_superjob_jid(jid)
  keys = Sidekiq.redis do |conn|
    conn.smembers("#{redis_prefix}:#{jid}:subjob_keys")
  end
  keys.collect { |key| find_by_key(key) }
end

.jid(superjob_id, subjob_id) ⇒ Object



83
84
85
# File 'lib/sidekiq/superworker/subjob.rb', line 83

def jid(superjob_id, subjob_id)
  "#{superjob_id}:#{subjob_id}"
end

.keysObject



58
59
60
61
62
63
# File 'lib/sidekiq/superworker/subjob.rb', line 58

def keys
  Sidekiq.redis do |conn|
    keys = conn.keys("#{redis_prefix}:*:subjob_keys")
    keys.collect { |key| conn.smembers(key) }.flatten
  end
end

.redis_prefixObject



87
88
89
# File 'lib/sidekiq/superworker/subjob.rb', line 87

def redis_prefix
  Superworker.options[:subjob_redis_prefix]
end

.transaction(&block) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/sidekiq/superworker/subjob.rb', line 73

def transaction(&block)
  result = nil
  Sidekiq.redis do |conn|
    conn.multi do
      result = yield(conn)
    end
  end
  result
end

Instance Method Details

#==(other) ⇒ Object



153
154
155
# File 'lib/sidekiq/superworker/subjob.rb', line 153

def ==(other)
  self.jid == other.jid
end

#childrenObject



143
144
145
146
147
# File 'lib/sidekiq/superworker/subjob.rb', line 143

def children
  return [] if children_ids.blank?
  children = children_ids.collect { |id| self.class.find_by_jid(self.class.jid(superjob_id, id)) }
  children.reject(&:nil?)
end

#descendants_are_completeObject



135
136
137
# File 'lib/sidekiq/superworker/subjob.rb', line 135

def descendants_are_complete
  @descendants_are_complete || false
end

#jidObject



127
128
129
# File 'lib/sidekiq/superworker/subjob.rb', line 127

def jid
  self.class.jid(superjob_id, subjob_id)
end

#keyObject



131
132
133
# File 'lib/sidekiq/superworker/subjob.rb', line 131

def key
  "#{self.class.redis_prefix}:#{jid}"
end

#nextObject



149
150
151
# File 'lib/sidekiq/superworker/subjob.rb', line 149

def next
  self.class.find_by_jid(self.class.jid(superjob_id, next_id))
end

#parentObject



139
140
141
# File 'lib/sidekiq/superworker/subjob.rb', line 139

def parent
  self.class.find_by_jid(self.class.jid(superjob_id, parent_id))
end

#saveObject



103
104
105
106
107
108
109
110
111
# File 'lib/sidekiq/superworker/subjob.rb', line 103

def save
  return false unless self.valid?

  self.class.transaction do |conn|
    conn.mapped_hmset(key, to_param)
    conn.expire(key,Superworker.options[:superjob_expiration]) if Superworker.options[:superjob_expiration]
  end
  true
end

#to_infoObject



157
158
159
# File 'lib/sidekiq/superworker/subjob.rb', line 157

def to_info
  "Subjob ##{jid} (#{superworker_class} > #{subworker_class})"
end

#to_paramObject



161
162
163
164
165
166
167
# File 'lib/sidekiq/superworker/subjob.rb', line 161

def to_param
  param = {}
  ATTRIBUTES.each do |attribute|
    param["#{attribute.to_s}".to_sym] = public_send(attribute).to_json
  end
  param
end

#update_attribute(attribute, value) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/sidekiq/superworker/subjob.rb', line 118

def update_attribute(attribute, value)
  public_send("#{attribute.to_s}=", value)
  return false unless self.valid?
  Sidekiq.redis do |conn|
    conn.hset(key, attribute.to_s, value.to_json)
  end
  true
end

#update_attributes(pairs = {}) ⇒ Object



113
114
115
116
# File 'lib/sidekiq/superworker/subjob.rb', line 113

def update_attributes(pairs = {})
  pairs.each_pair { |attribute, value| public_send("#{attribute}=", value) }
  self.save
end