Class: Schked::Adapters::Sequel
- Inherits:
-
Object
- Object
- Schked::Adapters::Sequel
- Includes:
- JobRunStore
- Defined in:
- lib/schked/adapters/sequel.rb
Constant Summary collapse
- SUPPORTED_DATABASE_TYPES =
%w[postgres mysql].freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #adapter_name ⇒ Object
- #claim(job_name, window_start) ⇒ Object
- #cleanup(older_than) ⇒ Object
-
#initialize(database, logger: Logger.new($stdout)) ⇒ Sequel
constructor
A new instance of Sequel.
Constructor Details
#initialize(database, logger: Logger.new($stdout)) ⇒ Sequel
Returns a new instance of Sequel.
18 19 20 21 22 23 24 25 26 |
# File 'lib/schked/adapters/sequel.rb', line 18 def initialize(database, logger: Logger.new($stdout)) @database = database @logger = logger type = database.database_type.to_s unless SUPPORTED_DATABASE_TYPES.include?(type) raise ArgumentError, "Schked::Adapters::Sequel supports Postgres and MySQL databases only, got: #{type.inspect}" end end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
16 17 18 |
# File 'lib/schked/adapters/sequel.rb', line 16 def logger @logger end |
Instance Method Details
#adapter_name ⇒ Object
62 63 64 |
# File 'lib/schked/adapters/sequel.rb', line 62 def adapter_name @database.adapter_scheme.to_s end |
#claim(job_name, window_start) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/schked/adapters/sequel.rb', line 28 def claim(job_name, window_start) validate!(job_name, window_start) ts = window_start.is_a?(Time) ? window_start.to_i : Integer(window_start) run_at = Time.now.to_f claimer = SecureRandom.uuid if postgres? # Sequel's +insert_conflict+ is a PostgreSQL-only dataset method; # it returns the new PK on success and +nil+ on conflict, so the # winner is decided atomically in a single statement. id = dataset .insert_conflict(target: UNIQUE_COLUMNS) .insert(job_name: job_name, window_start: ts, run_at: run_at, claimer: claimer) !id.nil? else mysql_claim(job_name, ts, run_at, claimer) end rescue ArgumentError raise rescue => e logger.error("Failed to claim sequel job run with error: #{e.}") raise end |
#cleanup(older_than) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/schked/adapters/sequel.rb', line 53 def cleanup(older_than) cutoff = older_than.is_a?(Time) ? older_than.to_i : Integer(older_than) dataset.where { window_start < cutoff }.delete nil rescue => e logger.error("Failed to clean up sequel job runs with error: #{e.}") raise end |