Class: Onetimer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/onetimer/runner.rb

Overview

Runs one-off data tasks exactly once, the way db:migrate runs schema migrations exactly once. Task files live in Onetimer.tasks_dir (default lib/one_timers/), named _.rb, each defining OneTimers:: with a #run method. Generate one with rake onetimer:new NAME=....

Class Method Summary collapse

Class Method Details

.generate!(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/onetimer/runner.rb', line 14

def self.generate!(name)
  timestamp = Time.current.strftime("%Y%m%d%H%M%S")
  class_name = name.camelize
  path = Onetimer.tasks_dir.join("#{timestamp}_#{name.underscore}.rb")

  FileUtils.mkdir_p(Onetimer.tasks_dir)
  File.write(path, <<~RUBY)
    # frozen_string_literal: true

    module OneTimers
      class #{class_name}
        def run
          # One-time task logic goes here.
        end
      end
    end
  RUBY

  path
end

.run_pending!Object



10
11
12
# File 'lib/onetimer/runner.rb', line 10

def self.run_pending!
  task_files.each { |file| run_task(file) }
end