Class: Kronos::Storage::MongoDb

Inherits:
Object
  • Object
show all
Defined in:
lib/kronos/storage/mongo_db.rb

Overview

:reek:UtilityFunction:

Constant Summary collapse

SHEDULED_TASK_MODEL =
Mongo::Model::ScheduledTaskModel
REPORT_MODEL =
Mongo::Model::ReportModel

Instance Method Summary collapse

Instance Method Details

#pending?(task) ⇒ Boolean



50
51
52
53
54
# File 'lib/kronos/storage/mongo_db.rb', line 50

def pending?(task)
  # Checks if task has any pending scheduled task (where scheduled_task.next_run > Time.now)
  query = SHEDULED_TASK_MODEL.where(task_id: task.id)
  query.exists? && query.first.next_run > Time.now
end

#register_report(report) ⇒ Object



39
40
41
42
43
# File 'lib/kronos/storage/mongo_db.rb', line 39

def register_report(report)
  # Removes any Kronos::Report with same task ID and saves the one in parameter
  remove_reports_for(report.task_id)
  REPORT_MODEL.create(report_params(report))
end

#remove(task_id) ⇒ Object



29
30
31
32
# File 'lib/kronos/storage/mongo_db.rb', line 29

def remove(task_id)
  # Removes scheduled tasks with task_id
  SHEDULED_TASK_MODEL.where(task_id: task_id).destroy_all
end

#remove_reports_for(task_id) ⇒ Object



45
46
47
48
# File 'lib/kronos/storage/mongo_db.rb', line 45

def remove_reports_for(task_id)
  # Removes reports with task_id
  REPORT_MODEL.where(task_id: task_id).destroy_all
end

#reportsObject



34
35
36
37
# File 'lib/kronos/storage/mongo_db.rb', line 34

def reports
  # Returns all previous Kronos::Report that were saved using #register_report
  REPORT_MODEL.all
end

#resolved_tasksObject



24
25
26
27
# File 'lib/kronos/storage/mongo_db.rb', line 24

def resolved_tasks
  # Returns a list of task ids that where resolved (where scheduled_task.next_run <= Time.now)
  SHEDULED_TASK_MODEL.where(:next_run.lte => Time.now).pluck(:task_id)
end

#schedule(scheduled_task) ⇒ Object



18
19
20
21
22
# File 'lib/kronos/storage/mongo_db.rb', line 18

def schedule(scheduled_task)
  # Removes any Kronos::ScheduledTask with same task ID and saves the one in parameter
  remove(scheduled_task.task_id)
  SHEDULED_TASK_MODEL.create(scheduled_task_params(scheduled_task))
end

#scheduled_tasksObject



13
14
15
16
# File 'lib/kronos/storage/mongo_db.rb', line 13

def scheduled_tasks
  # Returns all current Kronos::ScheduledTask, resolved or pending
  SHEDULED_TASK_MODEL.all
end