ruby-clock 0.2.0 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 380a0d3214236bb95f84bcb5aa3a04fc74e6ef916e4e064a71c13bcfb76a6f15
4
- data.tar.gz: ca43eca30e42447cdcfc563e0768ee0326f62679754996e0e6007774a62a9055
3
+ metadata.gz: e0fec8d8c66e1783260e73fad69f39572677871d722d1c62437c94741452340e
4
+ data.tar.gz: 03e5b84ef4cd5277da5623173ca70887dc3aac0296445e573459050341376b78
5
5
  SHA512:
6
- metadata.gz: 6bbf359d4a3523904d1e81c816cb89a11267a0fac2f4f891deb4bc27cb89834d4f70f34cda3a7849bfaa0b1184a8a8541e3e895f93d107402170e6a7f09816f3
7
- data.tar.gz: 2896961f63395b0eb4fa22d4fe47a4df5466dc7f3577afc5c1feac7fb3e7bb4315609ae6e4f14a56d223fe8ac3d70f20704d23f2fd01867501de874c75e01d0f
6
+ metadata.gz: 70134452766d69a5dbebc795cc31babb56ac14b6363aa6416379600fb6fa0546ccc9b131a17342b1376a101acc89ae746a1f9f19795274224a72e9291f9f6eba
7
+ data.tar.gz: 4aeb5e68d8108f40e10c11b4ea9fe4a3ad390ba4652afcc4e71cbc11c8c49b36be3f4c8b762f17eda1cffc488f1d5b803388bfb5f3e219962c289dd581549f81
data/Gemfile.lock CHANGED
@@ -1,21 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-clock (0.1.0)
5
- rufus-scheduler (>= 3.7.0)
4
+ ruby-clock (0.6.0)
5
+ method_source
6
+ rufus-scheduler (~> 3.8)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
- concurrent-ruby (1.1.7)
11
+ concurrent-ruby (1.1.9)
11
12
  et-orbi (1.2.4)
12
13
  tzinfo
13
- fugit (1.4.1)
14
+ fugit (1.5.0)
14
15
  et-orbi (~> 1.1, >= 1.1.8)
15
16
  raabro (~> 1.4)
17
+ method_source (1.0.0)
16
18
  raabro (1.4.0)
17
19
  rake (12.3.3)
18
- rufus-scheduler (3.7.0)
20
+ rufus-scheduler (3.8.0)
19
21
  fugit (~> 1.1, >= 1.1.6)
20
22
  tzinfo (2.0.4)
21
23
  concurrent-ruby (~> 1.0)
data/README.md CHANGED
@@ -57,9 +57,13 @@ To start your clock process:
57
57
 
58
58
  ### Rails
59
59
 
60
+ Install the `clock` binstub and commit to your repo.
61
+
62
+ bundle binstubs ruby-clock
63
+
60
64
  To run your clock process in your app's environment:
61
65
 
62
- bundle exec rails runner clock
66
+ bundle exec rails runner bin/clock
63
67
 
64
68
  ### Non-Rails
65
69
 
@@ -76,7 +80,7 @@ schedule.every('5 minutes') do
76
80
  Add this line to your Procfile
77
81
 
78
82
  ```
79
- clock: bundle exec rails runer clock
83
+ clock: bundle exec rails runner bin/clock
80
84
  ```
81
85
 
82
86
  ## More Config and Capabilities
@@ -98,6 +102,63 @@ You can define before, after, and around callbacks which will run for all jobs.
98
102
  Read [the rufus-scheduler documentation](https://github.com/jmettraux/rufus-scheduler/#callbacks)
99
103
  to learn how to do this. Where the documentation references `s`, you should use `schedule`.
100
104
 
105
+ ### Job Identifier
106
+
107
+ ruby-clock adds the `identifier` method to `Rufus::Scheduler::Job`. This method will return the job's
108
+ [name](https://github.com/jmettraux/rufus-scheduler/#name--string) if one was given.
109
+ If a name is not given, the last non-comment code line in the job's block
110
+ will be used instead. If for some reason an error is encountered while calculating this, the next
111
+ fallback is the line number of the job in Clockfile.
112
+
113
+ Some examples of jobs and their identifiers:
114
+
115
+ ```ruby
116
+ schedule.every '1 second', name: 'my job' do |variable|
117
+ Foo.bar
118
+ end
119
+ # => my job
120
+
121
+ schedule.every '1 day', name: 'my job' do |variable|
122
+ daily_things = Foo.setup_daily
123
+ daily_things.process
124
+ # TODO: figure out best time of day
125
+ end
126
+ # => daily_things.process
127
+
128
+ # n.b. ruby-clock isn't yet smart enough to remove trailing comments
129
+ schedule.every '1 week', name: 'my job' do |variable|
130
+ weekly_things = Foo.setup_weekly
131
+ weekly_things.process # does this work???!1~
132
+ end
133
+ # => weekly_things.process # does this work???!1~
134
+ ```
135
+
136
+ This can be used for keeping track of job behavior in logs or a
137
+ stats tracker. For example:
138
+
139
+ ```ruby
140
+ def schedule.on_post_trigger(job, trigger_time)
141
+ duration = Time.now-trigger_time.to_t
142
+ StatsTracker.value('Clock: Job Execution Time', duration.round(2))
143
+ StatsTracker.value("Clock: Job #{job.identifier} Execution Time", duration.round(2))
144
+ StatsTracker.increment('Clock: Job Executions')
145
+ end
146
+
147
+ schedule.every '10 seconds', name: 'thread stats' do
148
+ thread_usage = Hash.new(0)
149
+ schedule.work_threads(:active).each do |t|
150
+ thread_usage[t[:rufus_scheduler_job].identifier] += 1
151
+ end
152
+ thread_usage.each do |job, count|
153
+ StatsTracker.value("Clock: Job #{job} Active Threads", count)
154
+ end
155
+
156
+ StatsTracker.value("Clock: Active Threads", schedule.work_threads(:active).size)
157
+ StatsTracker.value("Clock: Vacant Threads", schedule.work_threads(:vacant).size)
158
+ StatsTracker.value("Clock: DB Pool Size", ActiveRecord::Base.connection_pool.connections.size)
159
+ end
160
+ ```
161
+
101
162
  ### other rufus-scheduler Options
102
163
 
103
164
  All rufus-scheduler options are set to defaults. The `schedule` variable
data/exe/clock CHANGED
@@ -1,6 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'ruby-clock'
4
+ require 'method_source'
5
+
6
+ class Rufus::Scheduler::Job
7
+ def identifier
8
+ @identifier ||= begin
9
+ name || handler.source.split("\n").reject(&:empty?).grep_v(/#.*/)[-2].strip
10
+ rescue
11
+ source_location.join('-')
12
+ rescue
13
+ 'error-calculating-job-identifier'
14
+ end
15
+ end
16
+ end
17
+
4
18
  include RubyClock
5
19
 
6
20
  listen_to_signals
data/lib/ruby-clock.rb CHANGED
@@ -3,14 +3,22 @@ require 'rufus-scheduler'
3
3
 
4
4
  module RubyClock
5
5
  def shutdown
6
- puts "Shutting down 🐈️ 👋"
7
- puts Rufus::Scheduler.singleton.shutdown(wait: 29)
8
- exit
6
+ puts "Shutting down ruby-clock 🐈️ 👋"
7
+ Rufus::Scheduler.singleton.shutdown(wait: 29)
9
8
  end
10
9
 
11
10
  def listen_to_signals
12
- Signal.trap('INT') { shutdown }
13
- Signal.trap('TERM') { shutdown }
11
+ signals = %w[INT TERM]
12
+ signals.each do |signal|
13
+ old_handler = Signal.trap(signal) do
14
+ shutdown
15
+ if old_handler.respond_to?(:call)
16
+ old_handler.call
17
+ else
18
+ exit
19
+ end
20
+ end
21
+ end
14
22
  end
15
23
 
16
24
  def schedule
@@ -18,6 +26,7 @@ module RubyClock
18
26
  end
19
27
 
20
28
  def run_jobs
29
+ puts "Starting ruby-clock with #{schedule.jobs.size} jobs"
21
30
  Rufus::Scheduler.singleton.join
22
31
  end
23
32
  end
@@ -1,3 +1,3 @@
1
1
  module RubyClock
2
- VERSION = "0.2.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/ruby-clock.gemspec CHANGED
@@ -29,5 +29,6 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- spec.add_dependency "rufus-scheduler", '>= 3.7.0'
32
+ spec.add_dependency "rufus-scheduler", '~> 3.8'
33
+ spec.add_dependency "method_source"
33
34
  end
metadata CHANGED
@@ -1,30 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-clock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bachir
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-31 00:00:00.000000000 Z
11
+ date: 2021-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rufus-scheduler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: method_source
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: 3.7.0
33
+ version: '0'
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: 3.7.0
27
- description:
40
+ version: '0'
41
+ description:
28
42
  email:
29
43
  - j@jjb.cc
30
44
  executables:
@@ -50,7 +64,7 @@ licenses:
50
64
  metadata:
51
65
  homepage_uri: https://github.com/jjb/ruby-clock
52
66
  source_code_uri: https://github.com/jjb/ruby-clock
53
- post_install_message:
67
+ post_install_message:
54
68
  rdoc_options: []
55
69
  require_paths:
56
70
  - lib
@@ -65,8 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
79
  - !ruby/object:Gem::Version
66
80
  version: '0'
67
81
  requirements: []
68
- rubygems_version: 3.1.2
69
- signing_key:
82
+ rubygems_version: 3.1.6
83
+ signing_key:
70
84
  specification_version: 4
71
85
  summary: A "clock" process for invoking ruby code within a persistent runtime
72
86
  test_files: []