From 20135b87aeee5f574e2da0a89925c52c0ff31241 Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Fri, 6 Mar 2015 22:08:48 +0200 Subject: [PATCH] Add migration to generator --- README.md | 2 +- bin/crono | 3 +-- lib/crono/job.rb | 4 ++++ lib/generators/crono/install/install_generator.rb | 14 ++++++++++++++ .../templates/migrations/create_crono_jobs.rb | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 lib/generators/crono/install/templates/migrations/create_crono_jobs.rb diff --git a/README.md b/README.md index 042e304..3b2c231 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ After you install Crono, you can run the generator: rails generate crono:install -It will create a configuration file `config/cronotab.rb` +It will create a configuration file `config/cronotab.rb` and migration Now you are ready to move forward to create a job and schedule it. diff --git a/bin/crono b/bin/crono index 5da4eff..f2fb30d 100755 --- a/bin/crono +++ b/bin/crono @@ -5,8 +5,7 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require "crono/cli" begin - cli = Crono::CLI.instance - cli.run + Crono::CLI.instance.run rescue => e raise e if $DEBUG STDERR.puts e.message diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 4d848b6..807c758 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -18,6 +18,10 @@ module Crono "Perform #{performer} #{period.description}" end + def job_id + description + end + def perform logger.info "Perform #{performer}" self.last_performed_at = Time.now diff --git a/lib/generators/crono/install/install_generator.rb b/lib/generators/crono/install/install_generator.rb index 622f36a..f103755 100644 --- a/lib/generators/crono/install/install_generator.rb +++ b/lib/generators/crono/install/install_generator.rb @@ -1,12 +1,26 @@ +require 'rails/generators' +require 'rails/generators/migration' +require 'rails/generators/active_record' + module Crono module Generators class InstallGenerator < ::Rails::Generators::Base + include Rails::Generators::Migration + + def self.next_migration_number(path) + ActiveRecord::Generators::Base.next_migration_number(path) + end + desc "Installs crono and generates the necessary configuration files" source_root File.expand_path("../templates", __FILE__) def copy_config template 'cronotab.rb.erb', 'config/cronotab.rb' end + + def create_migrations + migration_template 'migrations/create_crono_jobs.rb', 'db/migrate/create_crono_jobs.rb' + end end end end diff --git a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb new file mode 100644 index 0000000..ed889a0 --- /dev/null +++ b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb @@ -0,0 +1,15 @@ +class CreateActiveAdminComments < ActiveRecord::Migration + def self.up + create_table :crono_jobs do |t| + t.string :job_id, null: false + t.text :log + t.datetime :last_performed_at + t.timestamps + end + add_index :crono_jobs, [:job_id] + end + + def self.down + drop_table :crono_jobs + end +end