qron 0.9.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.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE.txt +24 -0
  4. data/README.md +46 -0
  5. data/lib/qron.rb +101 -0
  6. data/qron.gemspec +46 -0
  7. metadata +66 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8c930aec174d9935c00cd256977349028a6db517d2f3eeba15f2f4dadd01bbca
4
+ data.tar.gz: 21b4ce60d4cf77b01856cf624374cddc2764194bec2fb87fdc9c73815ad878fa
5
+ SHA512:
6
+ metadata.gz: bcfc555cd774e231ce73c8bdab71c0482eb2a1dcbb37f534a9bdccb4942345a20c28ee52e433cabe6718d608dbef1acc0842dcdb81715eb7b728fca9199a3064
7
+ data.tar.gz: a23f7152c51ab2e18f6e24370d1866ee608840a0190feef28813ccf6686fc5654ccc8aba57a0ef630043029d3acffdcffe8638da0a8b2c642108cfb703b9727a
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+
2
+ # CHANGELOG.md
3
+
4
+
5
+ ## stagnum 0.9.0 released 2025-03-23
6
+
7
+ * Initial release
8
+
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+
2
+ Copyright (c) 2025-2025, John Mettraux, jmettraux+flor@gmail.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+
22
+
23
+ Made in Japan
24
+
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+
2
+ # qron
3
+
4
+ [![tests](https://github.com/floraison/qron/workflows/test/badge.svg)](https://github.com/floraison/qron/actions)
5
+ [![Gem Version](https://badge.fury.io/rb/qron.svg)](http://badge.fury.io/rb/qron)
6
+
7
+ Queue and cron.
8
+
9
+ A stupid Ruby cron thread that wakes up from time to time to perform according
10
+ to what's written in a crontab.
11
+
12
+ Given `etc/qrontab_dev`:
13
+ ```
14
+ * * * * * p [ :hello, :min, Time.now ]
15
+ * * * * * * p [ :hello, :sec, Time.now ]
16
+ ```
17
+
18
+ and
19
+
20
+ ```ruby
21
+ require 'qron'
22
+
23
+ q = Qron.new(tab: 'etc/qrontab_dev')
24
+ q.join
25
+ ```
26
+
27
+ ```
28
+ [:hello, :sec, 2025-03-23 15:39:56.558783631 +0900]
29
+ [:hello, :sec, 2025-03-23 15:39:57.368985197 +0900]
30
+ [:hello, :sec, 2025-03-23 15:39:58.308865845 +0900]
31
+ [:hello, :sec, 2025-03-23 15:39:59.209102149 +0900]
32
+ [:hello, :min, 2025-03-23 15:40:00.149162785 +0900]
33
+ [:hello, :sec, 2025-03-23 15:40:00.149290935 +0900]
34
+ [:hello, :sec, 2025-03-23 15:40:01.039228675 +0900]
35
+ (...)
36
+ ```
37
+
38
+ Uses [fugit](https://github.com/floraison/fugit) for cron parsing and
39
+ [stagnum](https://github.com/floraison/stagnum) as its worker pool.
40
+
41
+ A little brother to [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler).
42
+
43
+ ## LICENSE
44
+
45
+ MIT, see [LICENSE.txt](LICENSE.txt)
46
+
data/lib/qron.rb ADDED
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fugit'
4
+ require 'stagnum'
5
+
6
+
7
+ class Qron
8
+
9
+ VERSION = '0.9.0'.freeze
10
+
11
+ attr_reader :options
12
+ attr_reader :tab, :thread, :started, :last_sec, :work_pool
13
+
14
+ def initialize(opts={})
15
+
16
+ @options = opts
17
+
18
+ start unless opts[:start] == false
19
+ end
20
+
21
+ def start
22
+
23
+ @started = Time.now
24
+ @last_sec = @started.to_i
25
+
26
+ @work_pool ||=
27
+ Stagnum::Pool.new("Qron-#{Qron::VERSION}-pool", @options[:workers] || 3)
28
+
29
+ @thread =
30
+ Thread.new do
31
+ loop do
32
+ break if @started == nil
33
+ now = Time.now
34
+ next if now.to_i == @last_sec
35
+ perform(now)
36
+ sleep 0.7 + (0.5 * rand)
37
+ end
38
+ end
39
+
40
+ # TODO rescue perform...
41
+ end
42
+
43
+ def stop
44
+
45
+ @started = nil
46
+ end
47
+
48
+ def join
49
+
50
+ @thread && @thread.join
51
+ end
52
+
53
+ protected
54
+
55
+ def read_tab
56
+
57
+ t = @options[:crontab] || @options[:tab] || 'qrontab'
58
+
59
+ return t if t.is_a?(Array)
60
+
61
+ # TODO timezones
62
+
63
+ File.readlines(t)
64
+ .inject([]) { |a, l|
65
+ l = l.strip
66
+ next a if l == ''
67
+ next a if l.start_with?('#')
68
+ ll5 = l.split(/\s+/, 6)
69
+ ll6 = ll5.pop.split(/\s+/, 2)
70
+ ll5 = ll5.join(' ')
71
+ ll6, r = *ll6
72
+ c = Fugit::Cron.parse("#{ll5} #{ll6}")
73
+ unless c
74
+ c = Fugit::Cron.parse(ll5)
75
+ r = "#{ll6} #{r}"
76
+ end
77
+ a << [ c, r ]
78
+ a }
79
+ end
80
+
81
+ def perform(now)
82
+
83
+ @tab ||= read_tab
84
+
85
+ @tab.each do |cron, command|
86
+
87
+ do_perform(now, cron, command) if cron.match?(now)
88
+ end
89
+
90
+ @last_sec = now.to_i
91
+ end
92
+
93
+ def do_perform(now, cron, command)
94
+
95
+ @work_pool.enqueue({ time: now, cron: cron, command: command }) do
96
+
97
+ ::Kernel.eval(command)
98
+ end
99
+ end
100
+ end
101
+
data/qron.gemspec ADDED
@@ -0,0 +1,46 @@
1
+
2
+ Gem::Specification.new do |s|
3
+
4
+ s.name = 'qron'
5
+
6
+ s.version = File.read(
7
+ File.expand_path('../lib/qron.rb', __FILE__)
8
+ ).match(/ VERSION *= *['"]([^'"]+)/)[1]
9
+
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = [ 'John Mettraux' ]
12
+ s.email = [ 'jmettraux+flor@gmail.com' ]
13
+ s.homepage = 'https://github.com/floraison/qron'
14
+ s.license = 'MIT'
15
+ s.summary = 'queue and cron'
16
+
17
+ s.description = %{
18
+ A Ruby thread that wakes up in time to perform what's ordered in its crontab
19
+ }.strip
20
+
21
+ s.metadata = {
22
+ 'changelog_uri' => s.homepage + '/blob/master/CHANGELOG.md',
23
+ 'bug_tracker_uri' => s.homepage + '/issues',
24
+ 'homepage_uri' => s.homepage,
25
+ 'documentation_uri' => s.homepage,
26
+ 'source_code_uri' => s.homepage,
27
+ #'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/floraison',
28
+ #'wiki_uri' => s.homepage + '/wiki',
29
+ 'rubygems_mfa_required' => 'true',
30
+ }
31
+
32
+ #s.files = `git ls-files`.split("\n")
33
+ s.files = Dir[
34
+ '{README,CHANGELOG,CREDITS,LICENSE}.{md,txt}',
35
+ #'Makefile',
36
+ 'lib/**/*.rb', #'spec/**/*.rb', 'test/**/*.rb',
37
+ "#{s.name}.gemspec",
38
+ ]
39
+
40
+ #s.add_runtime_dependency 'raabro', '~> 1.4'
41
+
42
+ s.add_development_dependency 'probatio', '~> 1.0'
43
+
44
+ s.require_path = 'lib'
45
+ end
46
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - John Mettraux
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-03-23 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: probatio
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
26
+ description: A Ruby thread that wakes up in time to perform what's ordered in its
27
+ crontab
28
+ email:
29
+ - jmettraux+flor@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/qron.rb
38
+ - qron.gemspec
39
+ homepage: https://github.com/floraison/qron
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ changelog_uri: https://github.com/floraison/qron/blob/master/CHANGELOG.md
44
+ bug_tracker_uri: https://github.com/floraison/qron/issues
45
+ homepage_uri: https://github.com/floraison/qron
46
+ documentation_uri: https://github.com/floraison/qron
47
+ source_code_uri: https://github.com/floraison/qron
48
+ rubygems_mfa_required: 'true'
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.6.2
64
+ specification_version: 4
65
+ summary: queue and cron
66
+ test_files: []