cronwatch 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,8 +11,11 @@ module Cronwatch
11
11
  # Writes a migration for the three tables the ActiveRecord store uses
12
12
  # (created with the SDK's own DDL, so a Node process can share them) and
13
13
  # config/initializers/cronwatch.rb, then prints how to schedule
14
- # Cronwatch::CheckJob and mount the dashboard. The templates live in this
15
- # file so the gem ships nothing but Ruby.
14
+ # Cronwatch::CheckJob and mount the dashboard. With --database, the
15
+ # migration goes to that database and app/models/cronwatch_record.rb, an
16
+ # abstract class connected to it, is the store's connection_class, so the
17
+ # store reads the tables where the migration made them. The templates
18
+ # live in this file so the gem ships nothing but Ruby.
16
19
  class InstallGenerator < ::Rails::Generators::Base
17
20
  include ::ActiveRecord::Generators::Migration
18
21
 
@@ -21,7 +24,10 @@ module Cronwatch
21
24
  class_option :prefix, type: :string, default: Cronwatch::Stores::ActiveRecord::DEFAULT_PREFIX,
22
25
  desc: "Table name prefix: lowercase letters, digits and underscores"
23
26
  class_option :database, type: :string, aliases: %i[--db],
24
- desc: "The database for the migration, in an app with several"
27
+ desc: "The database for the tables, in an app with several (from config/database.yml)"
28
+
29
+ # The abstract class --database writes, which the store connects through.
30
+ RECORD_CLASS = "CronwatchRecord"
25
31
 
26
32
  def check_prefix
27
33
  Cronwatch::Stores::ActiveRecord.table_prefix(options[:prefix])
@@ -41,6 +47,12 @@ module Cronwatch
41
47
  create_file File.join(dir, "#{number}_#{migration_name}.rb"), migration
42
48
  end
43
49
 
50
+ def create_connection_class
51
+ return unless database_name
52
+
53
+ create_file "app/models/cronwatch_record.rb", connection_class
54
+ end
55
+
44
56
  def create_initializer
45
57
  create_file "config/initializers/cronwatch.rb", initializer
46
58
  end
@@ -60,9 +72,33 @@ module Cronwatch
60
72
  "create_cronwatch_#{prefix.squeeze("_").delete_prefix("_").delete_suffix("_")}_tables"
61
73
  end
62
74
 
75
+ # The --database name, or nil.
76
+ def database_name
77
+ name = options[:database].to_s
78
+ name.empty? ? nil : name
79
+ end
80
+
63
81
  def store_args
64
82
  prefix = options[:prefix]
65
- prefix == Cronwatch::Stores::ActiveRecord::DEFAULT_PREFIX ? "" : "(prefix: #{prefix.inspect})"
83
+ args = []
84
+ args << "prefix: #{prefix.inspect}" unless prefix == Cronwatch::Stores::ActiveRecord::DEFAULT_PREFIX
85
+ # Named, not referenced, so the initializer does not load an app class at boot.
86
+ args << "connection_class: #{RECORD_CLASS.inspect}" if database_name
87
+ args.empty? ? "" : "(#{args.join(", ")})"
88
+ end
89
+
90
+ def connection_class
91
+ <<~RUBY
92
+ # frozen_string_literal: true
93
+
94
+ # The #{database_name} database, where CronWatch keeps its tables. The store in
95
+ # config/initializers/cronwatch.rb connects through this class.
96
+ class #{RECORD_CLASS} < ActiveRecord::Base
97
+ self.abstract_class = true
98
+
99
+ connects_to database: { writing: :#{database_name} }
100
+ end
101
+ RUBY
66
102
  end
67
103
 
68
104
  def migration
@@ -102,7 +138,7 @@ module Cronwatch
102
138
  #
103
139
  # and run Cronwatch::CheckJob every few minutes to catch the runs that never happen.
104
140
  Cronwatch.configure do |c|
105
- # Jobs, runs and alert state, in this app's database.
141
+ # Jobs, runs and alert state, in #{database_name ? "the #{database_name} database (see app/models/cronwatch_record.rb)" : "this app's database"}.
106
142
  c.store = Cronwatch::Stores::ActiveRecord.new#{store_args}
107
143
 
108
144
  # Where alerts go. With none set, they are written to standard error.
@@ -127,14 +163,22 @@ module Cronwatch
127
163
  RUBY
128
164
  end
129
165
 
166
+ # What the next steps say about --database after the migrate line, or "".
167
+ def database_step
168
+ return "" unless database_name
169
+
170
+ "\n\n The store connects through #{RECORD_CLASS} (app/models/cronwatch_record.rb),\n " \
171
+ "which connects_to the #{database_name} database named in config/database.yml."
172
+ end
173
+
130
174
  def next_steps
131
175
  <<~TEXT
132
176
 
133
177
  CronWatch is installed. Next:
134
178
 
135
- 1. Create the tables:
179
+ 1. Create the tables#{database_name ? " in the #{database_name} database" : ""}:
136
180
 
137
- bin/rails db:migrate
181
+ bin/rails db:migrate#{database_step}
138
182
 
139
183
  2. Monitor a job:
140
184
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cronwatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon C. Phillips