ruby-clock 2.0.0.beta8 → 2.0.0.beta9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 315bcc30c6fc32c1676e452750d76e839b0123f19fe7512bbe6662ae72ee0ecc
4
- data.tar.gz: a5831b721e9d99af3082cb31dd472f4e282d5665616f40694ba41def7250b907
3
+ metadata.gz: cc7625ea594c3028d9503ad7c1013b7888c776a51bf5853ada639cef1b9d754c
4
+ data.tar.gz: 6c3d8841a45cd336a108c334b94e35441200885b2329ac6dbf39751dd32b8451
5
5
  SHA512:
6
- metadata.gz: '0983e40393e4d273feddbcb53d67c0a8b2ccbb194439a16df9ddb1edaeedcf89f36338bffc80d8ffb44809500b3553cbc601c55275fcd4dba86d18b67ad57bf5'
7
- data.tar.gz: 563039f0ea34927b4d6e1abd64db0f2ae086c44f37d03852d8ca510223e1ce0f57ebe275ce7059d704de3e29a1e7f33d1412b92ad4b7a5a7e4e6d5ec661e9605
6
+ metadata.gz: f8067434425e00d84371aa7242d78ebf9a24f355e2f13ec1cba79e7140301d2902b4bf2902237023b277ebfd876c88761b4cdc15de33df8225837ab074bb1781
7
+ data.tar.gz: 93eb07e47f6a8ec2cad4f7916090bb2f310657cca5bae3adef090e7a08f2de7d9b4af62f9e6649e1ab35eb5b2fc81d4a2063971bea6b37e3759473cd722ea47a
data/CHANGELOG.md CHANGED
@@ -5,30 +5,32 @@
5
5
  is implemented is now compatible with both rails 6 and 7
6
6
  * RUBY_CLOCK_SHUTDOWN_WAIT_SECONDS value is logged when starting
7
7
  * DSL methods are now at the top-level namespace (`schedule.every` → `every`, `schedule.cron` → `cron`)
8
- * Error handler definition is now at the top-level namespace (`def schedule.on_error` → `on_error do`)
9
8
  * Around callbacks now have a top-level namespace method. `def schedule.around_trigger` → `around_action do`
10
9
  * Multiple around callbacks can be consecutively assigned - no need to put all behavior into one method
10
+ * Error handler definition is now at the top-level namespace (`def schedule.on_error` → `on_error do`)
11
11
  * Errors encountered when loading Clockfile (such as incorrect cron syntax)
12
12
  will be reported to the error handler
13
13
  * The automatic identifier generator will now ignore `}` and `end` lines
14
- * posix-spawn is no longer used. In ruby 3, native `Process.spawn` is more performant. See
14
+ * posix-spawn is no longer used. In ruby ~3~ 2.2, native `Process.spawn` is more performant. See
15
15
  [posix-spawn#90](https://github.com/rtomayko/posix-spawn/issues/90)
16
16
  and
17
17
  [terrapin#19](https://github.com/thoughtbot/terrapin/pull/19)
18
18
  for more info.
19
- * `--environment-and-syntax-check` capability
19
+ * ability to load multiple clockfiles with one invocation
20
+ * job slugs based on job identifier, e.g. "Widget Co. Weekly Reports" -> "widget-co-weekly-reports"
21
+ * `--check-slug-uniqueness`
22
+ * `--environment-and-syntax-check`
20
23
 
21
24
  ### Anti-Features
22
25
  * ruby 3.0 is now the minimum version
23
26
 
24
27
  ### Code Improvements
25
28
  * The code which implements the rails reloader/executor is now less complicated
26
- * Code reorganization so there are no unnecessary methods in top-level Kernel namespace
29
+ * Code reorganization so there are no unnecessary methods in top-level `Kernel` namespace
27
30
  * top-level DSL methods are now implemented with refinements, so they don't polute other code
28
31
 
29
32
 
30
33
  ### Migrating from ruby-clock version 1 to version 2
31
-
32
34
  * The minimum ruby version is 3.0
33
35
  * The top of every Clockfile must begin with `using RubyClock::DSL`
34
36
  * If you have an existing `def schedule.around_trigger`, you will need to change it to use the new
@@ -37,6 +39,7 @@
37
39
  `every`, `cron`, and `on_error` at the top-level, without referencing `schedule`.
38
40
  * You now have the option of catching and reporting errors encountered when parsing the Clockfile.
39
41
  * There is no longer a need to have a binstub in rails. You can delete bin/clock from your app.
42
+ * remove the `posix-spawn` gem from your project
40
43
  * The invocations (in Procfile, or wherever else you start ruby-clock) should change from
41
44
 
42
45
  bundle exec rails runner bin/clock
data/README.md CHANGED
@@ -33,7 +33,7 @@ ruby >= 3.0 is required.
33
33
  Add these lines to your application's Gemfile:
34
34
 
35
35
  ```ruby
36
- gem 'ruby-clock'
36
+ gem 'ruby-clock', '2.0.0.beta9'
37
37
  ```
38
38
 
39
39
  And then execute:
@@ -71,6 +71,11 @@ This will ignore Clockfile and only read jobs from clocks/MyClockfile:
71
71
 
72
72
  bundle exec clock clocks/MyClockfile
73
73
 
74
+ You can also load multiple files with one invocation
75
+ (although a better approach might be to load your subfiles within a top-level Clockfile):
76
+
77
+ bundle exec clock clocks/daily.rb clocks/weekly.rb
78
+
74
79
  ### Rails
75
80
 
76
81
  To run your clock process in your app's environment:
@@ -150,6 +155,16 @@ is valid during dev, or in automate tests.
150
155
  assert(system("bundle exec --environment-and-syntax-check clock/my_clockfile.rb"))
151
156
  ```
152
157
 
158
+ You can use `--check-slug-uniqueness` to check if all the auto-generated slugs are unique. If you have
159
+ multiple files with jobs, you need to pass them all in with one invocation in order to check global uniqueness.
160
+
161
+ ```ruby
162
+ # system returns true/false depending on 0/1 exit status of process
163
+ assert(system("bundle exec --check-slug-uniqueness")) # loads Clockfile
164
+ assert(system("bundle exec --check-slug-uniqueness clock/weekly.rb clock/daily.rb")) # load specific files
165
+ ```
166
+
167
+
153
168
  ## More Config and Capabilities
154
169
 
155
170
  ### Error Handling
@@ -297,7 +312,7 @@ There are also `rake_execute` and `rake_async`.
297
312
  See [the code](https://github.com/jjb/ruby-clock/blob/main/lib/ruby-clock/rake.rb)
298
313
  and [this article](https://code.jjb.cc/running-rake-tasks-from-within-ruby-on-rails-code) for more info.
299
314
 
300
- ### Job Identifier
315
+ ### Job Identifier & Slug
301
316
 
302
317
  ruby-clock adds the `identifier` method to `Rufus::Scheduler::Job`. This method will return the job's
303
318
  [name](https://github.com/jmettraux/rufus-scheduler/#name--string) if one was given.
@@ -305,30 +320,35 @@ If a name is not given, the last non-comment code line in the job's block
305
320
  will be used instead. If for some reason an error is encountered while calculating this, the next
306
321
  fallback is the line number of the job in Clockfile.
307
322
 
308
- Some examples of jobs and their identifiers:
323
+ There is also the `slug` method, which produces a slug using
324
+ [ActiveSupport parameterize](https://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize),
325
+ and with underscores changed to hyphens.
326
+ If the `activesupport` gem is not in your Gemfile and you attempt to use `slug`, it will fail.
327
+
328
+ Some examples of identifiers and slugs:
309
329
 
310
330
  ```ruby
311
331
  every '1 second', name: 'my job' do
312
332
  Foo.bar
313
333
  end
314
- # => my job
334
+ # my job, my-job
315
335
 
316
336
  every '1 day' do
317
337
  daily_things = Foo.setup_daily
318
338
  daily_things.process
319
339
  # TODO: figure out best time of day
320
340
  end
321
- # => daily_things.process
341
+ # daily_things.process, daily-things-process
322
342
 
323
343
  # n.b. ruby-clock isn't yet smart enough to remove trailing comments
324
344
  every '1 week' do
325
345
  weekly_things = Foo.setup_weekly
326
346
  weekly_things.process # does this work???!1~
327
347
  end
328
- # => weekly_things.process # does this work???!1~
348
+ # weekly_things.process # does this work???!1~, weekly-things-process-does-this-work-1
329
349
  ```
330
350
 
331
- This can be used for keeping track of job behavior in logs or a
351
+ The identifier can be used for keeping track of job behavior in logs or a
332
352
  stats tracker. For example:
333
353
 
334
354
  ```ruby
@@ -356,6 +376,18 @@ every '10 seconds', name: 'thread stats' do
356
376
  end
357
377
  ```
358
378
 
379
+ The slug can be used for similar purposes where a slug-style string is needed. Here you can report
380
+ your job to a scheduled job monitor:
381
+
382
+ ```ruby
383
+ # TODO proper example for healthcheks
384
+ around_action do |job_proc, job_info|
385
+ Net::HTTP.get("https://mymonitor.example.com/APIKEY/start/#{job_info.slug}")
386
+ job_proc.call
387
+ Net::HTTP.get("https://mymonitor.example.com/APIKEY/stop/#{job_info.slug}")
388
+ end
389
+ ```
390
+
359
391
  ### Other rufus-scheduler Options
360
392
 
361
393
  All [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler/) options are set to defaults.
@@ -14,7 +14,7 @@ end
14
14
  # end
15
15
 
16
16
  around_action do |job_proc, job_info|
17
- puts "before1 #{job_info.class} #{job_info.identifier}"
17
+ puts "before1 #{job_info.class} #{job_info.identifier} #{job_info.slug}"
18
18
  job_proc.call
19
19
  puts "after1"
20
20
  end
data/example-app/Gemfile CHANGED
@@ -5,4 +5,5 @@ source "https://rubygems.org"
5
5
  ruby '>= 3.0'
6
6
 
7
7
  gem 'ruby-clock', path: '../'
8
+ gem 'activesupport'
8
9
  # gem 'terrapin'
data/exe/clock CHANGED
@@ -9,27 +9,40 @@ RubyClock.instance.prepare_rake
9
9
  RubyClock.instance.schedule.pause
10
10
  RubyClock.instance.add_rails_executor_to_around_actions
11
11
 
12
- clockfile = 'Clockfile'
13
12
  check_syntax = false
14
- case ARGV[0]
15
- when '--environment-and-syntax-check'
16
- check_syntax = true
17
- case ARGV[1]
18
- when String
19
- clockfile = ARGV[1]
13
+ check_slug_uniqueness = false
14
+ if '--' == ARGV[0][0..1]
15
+ case ARGV[0]
16
+ when '--environment-and-syntax-check'
17
+ check_syntax = true
18
+ when '--check-slug-uniqueness'
19
+ check_slug_uniqueness = true
20
+ else
21
+ raise 'unknown option'
22
+ end
23
+ if ARGV[1]
24
+ clockfiles = ARGV[1..]
25
+ else
26
+ clockfiles = ['Clockfile']
27
+ end
28
+ else
29
+ if ARGV[0]
30
+ clockfiles = ARGV[0..]
31
+ else
32
+ clockfiles = ['Clockfile']
20
33
  end
21
- when String
22
- clockfile = ARGV[0]
23
34
  end
24
35
 
25
- begin
26
- load clockfile
27
- rescue => clockfile_error
28
- if RubyClock.instance.on_error
29
- RubyClock.instance.on_error.call("An error has occured while parsing the clockfile", clockfile_error)
30
- end
36
+ clockfiles.each do |clockfile|
37
+ begin
38
+ load clockfile
39
+ rescue => clockfile_error
40
+ if RubyClock.instance.on_error
41
+ RubyClock.instance.on_error.call("An error has occured while parsing the clockfile #{clockfile}", clockfile_error)
42
+ end
31
43
 
32
- raise
44
+ raise
45
+ end
33
46
  end
34
47
 
35
48
  RubyClock.instance.ensure_around_trigger_has_not_been_redefined
@@ -37,6 +50,23 @@ RubyClock.instance.freeze_around_actions
37
50
 
38
51
  if check_syntax
39
52
  puts "✨ Environment & Syntax OK ✨"
53
+ elsif check_slug_uniqueness
54
+ slugs = {}
55
+ RubyClock.instance.schedule.jobs.each do |j|
56
+ if slugs[j.slug]
57
+ slugs[j.slug] +=1
58
+ else
59
+ slugs[j.slug] = 1
60
+ end
61
+ end
62
+ slugs_with_duplicates = []
63
+ slugs.each{|s,count| slugs_with_duplicates << s if count > 1 }
64
+ if slugs_with_duplicates.any?
65
+ puts "The following slugs have duplicates: #{slugs_with_duplicates}"
66
+ exit(false)
67
+ else
68
+ puts "✨ All Slugs Are Unique ✨"
69
+ end
40
70
  else
41
71
  RubyClock.instance.run_jobs
42
72
  end
@@ -1,3 +1,3 @@
1
1
  class RubyClock
2
- VERSION = "2.0.0.beta8"
2
+ VERSION = "2.0.0.beta9"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require 'method_source'
2
+
2
3
  class Rufus::Scheduler::Job
3
4
  UNDESIRED_ELEMENTS = ['', '}', 'end']
5
+
4
6
  def identifier
5
7
  @identifier ||= begin
6
8
  return name if name
@@ -18,4 +20,9 @@ class Rufus::Scheduler::Job
18
20
  end
19
21
  end
20
22
  end
23
+
24
+ def slug
25
+ require 'active_support/inflector'
26
+ identifier.gsub('_', '-').parameterize
27
+ end
21
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-clock
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta8
4
+ version: 2.0.0.beta9
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bachir
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufus-scheduler