Module: QueueDispatcher::ActsAsTaskQueue::SingletonMethods

Defined in:
lib/queue_dispatcher/acts_as_task_queue.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_task_queue_configObject



52
53
54
# File 'lib/queue_dispatcher/acts_as_task_queue.rb', line 52

def acts_as_task_queue_config
  @acts_as_task_queue_config
end

#any_running?Boolean

Are there any running task_queues?

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/queue_dispatcher/acts_as_task_queue.rb', line 58

def any_running?
  running = false
  all.each{ |tq| running = true if tq.running? || tq.brand_new? }
  running
end

#find_or_create_by_name(name, options = {}) ⇒ Object

Find or create a task_queue by its name which is not in state ‘error’. Create one, if there does not exists one



82
83
84
85
86
# File 'lib/queue_dispatcher/acts_as_task_queue.rb', line 82

def find_or_create_by_name(name, options = {})
  transaction do
    self.where(:name => name).where("state != 'error'").first || self.create(:name => name, :state => 'new', terminate_immediately: options[:terminate_immediately])
  end
end

#get_next_pendingObject

Get next pending task_queue



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/queue_dispatcher/acts_as_task_queue.rb', line 66

def get_next_pending
  task_queue = nil

  transaction do
    # Find next task_queue which is not running and not in state error
    order(:id).lock(true).all.each { |tq| task_queue = tq unless task_queue || tq.pid_running? || tq.state == 'error' }

    # Update pid inside the atomic transaction to be sure, the next call of this method will not give the same queue a second time
    task_queue.update_attribute :pid, $$ if task_queue
  end

  task_queue
end

#reset_immediately!Object

Kill all running TaskQueues immediately and destroy them.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/queue_dispatcher/acts_as_task_queue.rb', line 90

def reset_immediately!
  all.each do |tq|
    tq.update_attributes state: 'aborted'

    # Kill the TaskQueue with SIGKILL
    Process.kill 'KILL', tq.pid if tq.pid_running?

    # Update task_state to aborted and release all its locks
    tq.tasks.each do |task|
      task.update_attributes state: 'aborted' unless task.state == 'successful' || task.state == 'finished'
      tq.send(:release_lock_for, task)
    end
    tq.destroy
  end
end