Module: Serially::ClassMethods

Defined in:
lib/serially/serially.rb

Instance Method Summary collapse

Instance Method Details

#create_instance(*args) ⇒ Object

override this to provide a custom way of creating instances of your class



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/serially/serially.rb', line 50

def create_instance(*args)
  instance = nil
  args = args.flatten
  if self.is_active_record?
    if args.count == 1
      instance = args[0].is_a?(Fixnum) ? self.where(id: args[0]).first : self.where(args[0]).first
      raise Serially::ArgumentError.new("Serially: couldn't create ActiveRecord instance with provided arguments: #{args[0]}") if instance.blank?
    else
      raise Serially::ArgumentError.new("Serially: default implementation of ::create_instance expects to receive either id or hash")
    end
  else
    begin
      instance = args.blank? ? new : new(*args)
    rescue StandardError => exc
      raise Serially::ArgumentError.new("Serially: since no implementation of ::create_instance is provided in #{self}, tried to call new, but failed with provided arguments: #{args}")
    end
  end
  instance
end

#inherited(subclass) ⇒ Object

make sure inheritance works with Serially



12
13
14
15
# File 'lib/serially/serially.rb', line 12

def inherited(subclass)
  Serially::TaskManager[subclass] = Serially::TaskManager[self].clone_for(subclass)
  super
end

#is_active_record?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/serially/serially.rb', line 45

def is_active_record?
  self < ActiveRecord::Base
end

#serially(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/serially/serially.rb', line 17

def serially(*args, &block)
  options = args[0] || {}
  invalid_options = Serially::GlobalOptions.validate(options)
  raise Serially::ConfigurationError.new("Serially received the following invalid options: #{invalid_options}") if invalid_options.present?

  # If TaskManager for current including class doesn't exist, create it
  Serially::TaskManager[self] ||= Serially::TaskManager.new(self, options)
  task_manager = Serially::TaskManager[self]

  # create a new base, and resolve DSL
  @serially = Serially::Base.new(task_manager)
  if block
    @serially.instance_eval(&block)
  else
    raise Serially::ConfigurationError.new("Serially is defined without a block of tasks definitions in class #{self}")
  end

  # return Serially::Base
  @serially
end

#start_batch!(instance_ids) ⇒ Object



38
39
40
41
42
43
# File 'lib/serially/serially.rb', line 38

def start_batch!(instance_ids)
  queue = Serially::TaskManager[self].queue
  instance_ids.each do |instance_id|
    Serially::Job.enqueue(self, instance_id, queue)
  end
end