Class: Recurrent::TaskCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/recurrent/task_collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTaskCollection

Returns a new instance of TaskCollection.



4
5
6
7
# File 'lib/recurrent/task_collection.rb', line 4

def initialize
  @mutex = Mutex.new
  @tasks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



66
67
68
69
70
# File 'lib/recurrent/task_collection.rb', line 66

def method_missing(id, *args, &block)
  @mutex.synchronize do
    @tasks.send(id, *args, &block)
  end
end

Class Method Details

.sort_by_frequency(task_list) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/recurrent/task_collection.rb', line 57

def self.sort_by_frequency(task_list)
  task_list.sort_by do |task|
    task.schedule.rrules.sort_by do |rule|
      rule.frequency_in_seconds
    end.first.frequency_in_seconds
  end
end

Instance Method Details

#add_or_update(new_task) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/recurrent/task_collection.rb', line 9

def add_or_update(new_task)
  @mutex.synchronize do
    old_task_index = @tasks.index {|task| task.name == new_task.name }
    if old_task_index
      @tasks[old_task_index].schedule = new_task.schedule
      @tasks[old_task_index].action = new_task.action
    else
      @tasks << new_task
    end
  end
end

#next_execution_timeObject



28
29
30
31
32
# File 'lib/recurrent/task_collection.rb', line 28

def next_execution_time
  @mutex.synchronize do
    @tasks.map { |task| task.next_occurrence }.sort.first
  end
end

#next_for_execution_at_time(time) ⇒ Object



21
22
23
24
25
26
# File 'lib/recurrent/task_collection.rb', line 21

def next_for_execution_at_time(time)
  @mutex.synchronize do
    tasks_running_at_time = @tasks.select {|task| task.running? && task.thread['execution_time'] == time }
    TaskCollection.sort_by_frequency(tasks_running_at_time).first
  end
end

#remove(name) ⇒ Object



34
35
36
37
38
# File 'lib/recurrent/task_collection.rb', line 34

def remove(name)
  @mutex.synchronize do
    @tasks.reject! {|task| task.name == name }
  end
end

#runningObject



40
41
42
43
44
# File 'lib/recurrent/task_collection.rb', line 40

def running
  @mutex.synchronize do
    @tasks.select {|task| task.running? }
  end
end

#scheduled_to_execute_at(time, opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/recurrent/task_collection.rb', line 46

def scheduled_to_execute_at(time, opts={})
  @mutex.synchronize do
    current_tasks = @tasks.select {|task| task.next_occurrence == time }
    if opts[:sort_by_frequency]
      TaskCollection.sort_by_frequency(current_tasks)
    else
      current_tasks
    end
  end
end