Class: TaskManager::Plan

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Enumerize
Includes:
DeadlineCalculator
Defined in:
app/models/task_manager/plan.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeadlineCalculator

#calculate_deadline

Class Method Details

.activeObject

返回所有需要创建新任务的计划

判断条件: 1) ‘enabled_at`在当前时间之前; 2) `last_task_created_at`在周期内。

2.1) 日计划:`Time.now.beginning_of_day <= last_task_created_at <= Time.now.end_of_day`
2.2) 周计划:`Time.now.beginning_of_week <= last_task_created_at <= Time.now.end_of_week`
2.3) 月计划:`Time.now.beginning_of_month <= last_task_created_at <= Time.now.end_of_month`
2.4) 季计划:`Time.now.beginning_of_quarter <= last_task_created_at <= Time.now.end_of_quarter`
2.5) 年计划:`Time.now.beginning_of_year <= last_task_created_at <= Time.now.end_of_year`


100
101
102
103
104
105
106
107
108
109
# File 'app/models/task_manager/plan.rb', line 100

def active
  where{
    sift(:active_by_type, 'daily') |
    sift(:active_by_type, 'weekly') |
    sift(:active_by_type, 'monthly') |
    sift(:active_by_type, 'quarterly') |
    sift(:active_by_type, 'yearly')

  }
end

.active_by_type(type) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/task_manager/plan.rb', line 111

def active_by_type(type)
  now = Time.now
  beginning_at = case type.to_sym
                 when :daily then now.beginning_of_day
                 when :weekly then now.beginning_of_week
                 when :monthly then now.beginning_of_month
                 when :quarterly then now.beginning_of_quarter
                 when :yearly then now.beginning_of_year
                 end

  squeel{ (plan_type == type) &
          ((last_task_created_at <= beginning_at) | (last_task_created_at == nil)) &
          (enabled_at <= now) }
end

Instance Method Details

#assigneesObject



28
29
30
# File 'app/models/task_manager/plan.rb', line 28

def assignees
  assignables.collect(&:assignee)
end

#callbacksObject



32
33
34
# File 'app/models/task_manager/plan.rb', line 32

def callbacks
  callables.collect(&:callback)
end

#default_deadlineObject



78
79
80
81
82
83
84
85
86
87
# File 'app/models/task_manager/plan.rb', line 78

def default_deadline
  now = Time.now
  case plan_type.to_sym
  when :daily then now.end_of_day
  when :weekly then now.end_of_week
  when :monthly then now.end_of_month
  when :quarterly then now.end_of_quarter
  when :yearly then now.end_of_year
  end
end

#generate_task_for_assignable(a) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/task_manager/plan.rb', line 52

def generate_task_for_assignable(a)
  data.symbolize_keys!

  reminding_at = default_deadline.ago(begin_to_remind * 60)

  Task.create! do |t|
    t.name = name
    t.data = data.select{ |k, v| !(k.to_s.start_with?('deadline_')) }
    t.task_type = plan_type
    t.deadline = calculate_deadline(plan_type, data)
    t.reminding_at = reminding_at
    t.status = :new
    t.finished_at = nil
    t.autocompletable = autocompletable
    t.create_assignable(
      assignee_id: a.assignee_id,
      assignee_type: a.assignee_type,
    )
    #t.callables = callables
    t.callables = []
    callables.each do |c|
      t.callables << Callable.create!(callback_id: c.callback_id, callback_type: c.callback_type)
    end
  end
end

#generate_tasksObject

生成计划任务

为每一个计划的执行者创建一个计划任务。



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/task_manager/plan.rb', line 39

def generate_tasks
  tasks = []
  Plan.transaction do
    assignables.each do |a|
      tasks << generate_task_for_assignable(a)
    end
  end

  update_attributes(last_task_created_at: Time.now)

  tasks
end