Class: PgCron::Adapters::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_cron/adapters/postgres.rb,
lib/pg_cron/adapters/postgres/jobs.rb,
lib/pg_cron/adapters/postgres/connection.rb,
lib/pg_cron/adapters/postgres/query_executor.rb

Overview

The Postgres adapter.

Examples:

PgCron.configure do |config|
  config.adapter = PgCron::Adapters::Postgres.new
end

Defined Under Namespace

Classes: Connection, Jobs, QueryExecutor

Instance Method Summary collapse

Constructor Details

#initialize(connectable = ActiveRecord::Base) ⇒ Postgres

Returns a new instance of Postgres.



23
24
25
# File 'lib/pg_cron/adapters/postgres.rb', line 23

def initialize(connectable = ActiveRecord::Base)
  @connectable = connectable
end

Instance Method Details

#create_job(sql_definition) ⇒ void

This method returns an undefined value.

Creates a job in the database.

The definition is executed AS GIVEN — it is the cron.schedule() call from db/cron/_v01.sql, so the schedule, the command and any quoting are that file's business. This neither parses nor rewrites it, which is the substantive difference from assembling SQL by interpolating values: a name or command containing a quote produced broken SQL that way.

Parameters:

  • sql_definition (String) —

    The SQL for the job.



61
62
63
# File 'lib/pg_cron/adapters/postgres.rb', line 61

def create_job(sql_definition)
  execute(sql_definition)
end

#drop_job(name) ⇒ void

This method returns an undefined value.

Drops the job from the database.

Parameters:

  • name (String, Symbol) —

    The name of the job to drop.



93
94
95
# File 'lib/pg_cron/adapters/postgres.rb', line 93

def drop_job(name)
  execute("SELECT cron.unschedule(#{quoted(name)});")
end

#jobs ⇒ Array<PgCron::Job>

Every scheduled job in the database.

Used by SchemaDumper to populate schema.rb. Returns nothing when pg_cron is not installed, because cron.job does not exist then and a schema dump must not fail on a database that simply has no cron.

Returns:



45
46
47
48
49
# File 'lib/pg_cron/adapters/postgres.rb', line 45

def jobs
  return [] unless connection.pg_cron_enabled?

  PgCron::Adapters::Postgres::Jobs.all(connection)
end

#pg_cron_enabled? ⇒ Boolean

Whether pg_cron is installed in this database.

Every statement checks this first, so a migration that schedules a job still runs against a database without the extension — a test database, or an environment where cron is not wanted — instead of every such migration needing its own guard.

Returns:

  • (Boolean)


33
34
35
# File 'lib/pg_cron/adapters/postgres.rb', line 33

def pg_cron_enabled?
  connection.pg_cron_enabled?
end

#update_job(name, sql_definition) ⇒ void

This method returns an undefined value.

Updates a job in the database.

NOT drop-then-create, which is what F(x) does for a function: pg_cron's cron.schedule() against an existing jobname REPLACES that job, so the update is a single statement. Dropping first would leave a window with no schedule, and on a frequently-firing job that window is a missed run.

The existence check is deliberate — cron.schedule() would otherwise silently CREATE a job that was expected to be there, hiding a migration applied out of order.

Parameters:

  • name (String, Symbol) —

    The name of the job.

  • sql_definition (String) —

    The SQL for the job.



79
80
81
82
83
84
85
86
87
# File 'lib/pg_cron/adapters/postgres.rb', line 79

def update_job(name, sql_definition)
  connection.transaction do
    unless job_exists?(name)
      raise PG::InternalError, "ERROR: could not find valid entry for job '#{name}'"
    end

    execute(sql_definition)
  end
end