Class: PgCron::Adapters::Postgres
- Inherits:
-
Object
- Object
- PgCron::Adapters::Postgres
- 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.
Defined Under Namespace
Classes: Connection, Jobs, QueryExecutor
Instance Method Summary collapse
-
#create_job(sql_definition) ⇒ void
Creates a job in the database.
-
#drop_job(name) ⇒ void
Drops the job from the database.
-
#initialize(connectable = ActiveRecord::Base) ⇒ Postgres
constructor
A new instance of Postgres.
-
#jobs ⇒ Array<PgCron::Job>
Every scheduled job in the database.
-
#pg_cron_enabled? ⇒ Boolean
Whether pg_cron is installed in this database.
-
#update_job(name, sql_definition) ⇒ void
Updates a job in the database.
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/
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.
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.
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.
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.
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 |