Class: Kronos::Storage::MongoDb

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

Overview

:reek:UtilityFunction: :reek:TooManyMethods:

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#check_lock(task_id, lock_id) ⇒ Object



69
70
71
# File 'lib/kronos/storage/mongo_db.rb', line 69

def check_lock(task_id, lock_id)
  LOCK_MODEL.where(task_id: task_id, value: lock_id).exists?
end

#lock_task(task_id) ⇒ Object



63
64
65
66
67
# File 'lib/kronos/storage/mongo_db.rb', line 63

def lock_task(task_id)
  SecureRandom.uuid.tap do |lock_id|
    LOCK_MODEL.create(task_id: task_id, value: lock_id)
  end
end

#locked_task?(task_id) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/kronos/storage/mongo_db.rb', line 59

def locked_task?(task_id)
  LOCK_MODEL.where(task_id: task_id).exists?
end

#pending?(task) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/kronos/storage/mongo_db.rb', line 53

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



42
43
44
45
46
# File 'lib/kronos/storage/mongo_db.rb', line 42

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

#release_lock(task_id) ⇒ Object



73
74
75
# File 'lib/kronos/storage/mongo_db.rb', line 73

def release_lock(task_id)
  LOCK_MODEL.where(task_id: task_id).destroy_all
end

#remove(task_id) ⇒ Object



32
33
34
35
# File 'lib/kronos/storage/mongo_db.rb', line 32

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



48
49
50
51
# File 'lib/kronos/storage/mongo_db.rb', line 48

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

#reportsObject



37
38
39
40
# File 'lib/kronos/storage/mongo_db.rb', line 37

def reports
  # Returns all previous Kronos::Report that were saved using #register_report
  REPORT_MODEL.all.map(&method(:mount_report))
end

#resolved_tasksObject



27
28
29
30
# File 'lib/kronos/storage/mongo_db.rb', line 27

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



21
22
23
24
25
# File 'lib/kronos/storage/mongo_db.rb', line 21

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



16
17
18
19
# File 'lib/kronos/storage/mongo_db.rb', line 16

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