Class: Sidekiq::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/sidekiq_pro/batches.rb

Defined Under Namespace

Classes: Status

Instance Method Summary collapse

Constructor Details

#initialize(bid = nil) ⇒ Batch

Returns a new instance of Batch.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec/sidekiq_pro/batches.rb', line 85

def initialize(bid = nil)
  if Sidekiq::Testing.disabled?
    super
  else
    @bid = bid || SecureRandom.urlsafe_base64(10)
    props = RSpec::SidekiqPro::Batches::Props.fetch(bid, {})

    @created_f = @created_at = props.fetch("created_at", Time.now.utc).to_f
    @description = props["description"]
    @parent_bid = props["parent"]
    @callbacks = props.fetch("callbacks", {})
    @jids = props.fetch("jids", [])
    @mutable = props.empty?
  end
end

Instance Method Details

#include?(jid) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/rspec/sidekiq_pro/batches.rb', line 113

def include?(jid)
  return super if Sidekiq::Testing.disabled?

  @jids.include?(jid)
end

#invalidate_allObject



119
120
121
122
123
124
125
# File 'lib/rspec/sidekiq_pro/batches.rb', line 119

def invalidate_all
  return super if Sidekiq::Testing.disabled?

  RSpec::SidekiqPro::Batches::Props[bid]["invalidated"] = true
  Sidekiq::Queues.jobs_by_queue.each_value { |jobs| jobs.delete_if { |job| include?(job["jid"]) } }
  Sidekiq::Queues.jobs_by_class.each_value { |jobs| jobs.delete_if { |job| include?(job["jid"]) } }
end

#invalidate_jids(*jids) ⇒ Object



127
128
129
130
131
# File 'lib/rspec/sidekiq_pro/batches.rb', line 127

def invalidate_jids(*jids)
  return super if Sidekiq::Testing.disabled?

  # TODO
end

#invalidated?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
# File 'lib/rspec/sidekiq_pro/batches.rb', line 133

def invalidated?
  return super if Sidekiq::Testing.disabled?

  !!RSpec::SidekiqPro::Batches::Props[bid]["invalidated"]
end

#jidsObject



107
108
109
110
111
# File 'lib/rspec/sidekiq_pro/batches.rb', line 107

def jids
  return super if Sidekiq::Testing.disabled?

  @jids
end

#jobs(&block) ⇒ Object

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rspec/sidekiq_pro/batches.rb', line 139

def jobs(&block)
  return super if Sidekiq::Testing.disabled?

  raise ArgumentError, "Must specify a block" unless block

  if mutable?
    @parent_bid = Thread.current[:sidekiq_batch]&.bid

    RSpec::SidekiqPro::Batches::Props[bid] = {
      "created_at" => created_at,
      "description" => description,
      "parent" => parent_bid,
      "callbacks" => callbacks,
      "jids" => jids
    }
  end

  @mutable = false

  begin
    parent = Thread.current[:sidekiq_batch]
    Thread.current[:sidekiq_batch] = self
    yield
  ensure
    Thread.current[:sidekiq_batch] = parent
  end

  RSpec::SidekiqPro::Batches::Props[bid]["jids"] = @jids
end

#perform_callback(event) ⇒ Object

Raises:

  • (NotImplementedError)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rspec/sidekiq_pro/batches.rb', line 175

def perform_callback(event)
  raise NotImplementedError if Sidekiq::Testing.disabled?

  callbacks[event.to_s]&.each do |callback|
    callback.each do |target, options|
      klass_name, method = target.to_s.split("#")
      klass = klass_name.constantize
      meth = method || "on_#{event}"
      inst = klass.new
      inst.jid = SecureRandom.hex(12) if inst.respond_to?(:jid)
      inst.send(meth, status, options)
    end
  end
end

#redis(bid, &block) ⇒ Object



101
102
103
104
105
# File 'lib/rspec/sidekiq_pro/batches.rb', line 101

def redis(bid, &block)
  return super if Sidekiq::Testing.disabled?

  raise "Redis unavailbale when Sidekiq::Testing is enable"
end

#register(jid) ⇒ Object



169
170
171
172
173
# File 'lib/rspec/sidekiq_pro/batches.rb', line 169

def register(jid)
  return super if Sidekiq::Testing.disabled?

  @jids << jid
end