Class: Rubino::Jobs::CronJobRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/jobs/cron_job_repository.rb

Overview

Repository for cron job definitions. Plain CRUD on the cron_jobs table; execution is orchestrated by Jobs::Scheduler.

The DELIVERS constant documents the accepted values for the deliver column, but it is NOT enforced here: validation of the local/webhook enum lives in the dry-schema at the HTTP boundary (see Api::Schemas). Callers that bypass the HTTP layer can insert any string; Scheduler#deliver_if_needed only acts on the exact match “webhook”, treating anything else as no-op delivery.

Constant Summary collapse

DELIVERS =
%w[local webhook].freeze

Instance Method Summary collapse

Constructor Details

#initialize(db: nil) ⇒ CronJobRepository

Returns a new instance of CronJobRepository.



20
21
22
# File 'lib/rubino/jobs/cron_job_repository.rb', line 20

def initialize(db: nil)
  @db = db || Rubino.database.db
end

Instance Method Details

#create(name:, schedule:, prompt:, skills: [], model: nil, provider: nil, deliver: "local", enabled: true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rubino/jobs/cron_job_repository.rb', line 24

def create(name:, schedule:, prompt:, skills: [], model: nil, provider: nil, deliver: "local", enabled: true)
  now = Time.now.utc.iso8601
  id = SecureRandom.uuid
  @db[:cron_jobs].insert(
    id: id, name: name, schedule: schedule, prompt: prompt,
    skills_json: JSON.generate(skills), model: model, provider: provider,
    deliver: deliver, enabled: enabled, created_at: now, updated_at: now
  )
  find(id)
end

#destroy!(id) ⇒ Object



70
71
72
# File 'lib/rubino/jobs/cron_job_repository.rb', line 70

def destroy!(id)
  @db[:cron_jobs].where(id: id).delete
end

#find(id) ⇒ Object



35
36
37
# File 'lib/rubino/jobs/cron_job_repository.rb', line 35

def find(id)
  @db[:cron_jobs].where(id: id).first
end

#list(include_disabled: true) ⇒ Object



39
40
41
42
43
# File 'lib/rubino/jobs/cron_job_repository.rb', line 39

def list(include_disabled: true)
  ds = @db[:cron_jobs].order(:name)
  ds = ds.where(enabled: true) unless include_disabled
  ds.all
end

#record_run(id, run_id:) ⇒ Object

Stamps last_run_at/last_run_id after Scheduler#fire creates the run.



65
66
67
68
# File 'lib/rubino/jobs/cron_job_repository.rb', line 65

def record_run(id, run_id:)
  now = Time.now.utc.iso8601
  @db[:cron_jobs].where(id: id).update(last_run_at: now, last_run_id: run_id, updated_at: now)
end

#set_enabled(id, enabled:) ⇒ Object



60
61
62
# File 'lib/rubino/jobs/cron_job_repository.rb', line 60

def set_enabled(id, enabled:)
  update(id, enabled: enabled)
end

#update(id, attrs) ⇒ Hash?

Partial update. Unknown keys are silently dropped (whitelist via slice); :skills accepts an Array of strings and is JSON-encoded into the skills_json column.

Returns:

  • (Hash, nil)

    the refreshed row, or nil if the id does not exist.



49
50
51
52
53
54
55
56
57
58
# File 'lib/rubino/jobs/cron_job_repository.rb', line 49

def update(id, attrs)
  return nil unless find(id)

  attrs = attrs.transform_keys(&:to_sym).slice(:name, :schedule, :prompt, :skills, :model, :provider, :deliver,
                                               :enabled)
  attrs[:skills_json] = JSON.generate(attrs.delete(:skills) || []) if attrs.key?(:skills)
  attrs[:updated_at] = Time.now.utc.iso8601
  @db[:cron_jobs].where(id: id).update(attrs)
  find(id)
end