class Kronos::Storage::MongoDb

:reek: UtilityFunction: :reek: TooManyMethods:

Constants

LOCK_MODEL
REPORT_MODEL
SHEDULED_TASK_MODEL

Public Instance Methods

check_lock(task_id, lock_id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
reports() click to toggle source
# 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_tasks() click to toggle source
# 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) click to toggle source
# 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_tasks() click to toggle source
# 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

Private Instance Methods

mount_failure_report(report_model) click to toggle source
# File lib/kronos/storage/mongo_db.rb, line 92
def mount_failure_report(report_model)
  Kronos::Report.failure_from(report_model.task_id, report_model.exception, report_model.timestamp)
end
mount_report(report_model) click to toggle source
# File lib/kronos/storage/mongo_db.rb, line 79
def mount_report(report_model)
  case report_model.status
  when Kronos::Report::STATUSES[:success]
    mount_success_report(report_model)
  when Kronos::Report::STATUSES[:failure]
    mount_failure_report(report_model)
  end
end
mount_success_report(report_model) click to toggle source
# File lib/kronos/storage/mongo_db.rb, line 88
def mount_success_report(report_model)
  Kronos::Report.success_from(report_model.task_id, report_model.metadata, report_model.timestamp)
end
report_params(report) click to toggle source
# File lib/kronos/storage/mongo_db.rb, line 103
def report_params(report)
  {
    task_id: report.task_id,
    status: report.status,
    metadata: report.metadata,
    exception: report.exception,
    timestamp: report.timestamp
  }
end
scheduled_task_params(scheduled_task) click to toggle source
# File lib/kronos/storage/mongo_db.rb, line 96
def scheduled_task_params(scheduled_task)
  {
    task_id: scheduled_task.task_id,
    next_run: scheduled_task.next_run
  }
end