crono 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0045e60df85bab889a28dd9de6563cbb4f9797d0
4
+ data.tar.gz: c282718940378a5d41fe75e73780149b46f75a62
5
+ SHA512:
6
+ metadata.gz: 9b983b15c72eb8f1f813eb0744d51ca286ac8b35f9a35d1bdffb89b297358f4ecc5916a816410609ae0c6c132122c67c546cefc61092c3e0e309614449a33f8e
7
+ data.tar.gz: f5fd72d6addae0d7d800ef7b266d01fa47ee1eb28aae9e24add2a1a9b6fcef95ec5fe9961d74b6040e406afbaed10f083db9ec7336c4a680f9e84b881c3b4f40
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ os:
3
+ - linux
4
+ - osx
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1
8
+ - 2.2
9
+ notifications:
10
+ webhooks:
11
+ urls:
12
+ - https://webhooks.gitter.im/e/907e95dada362be2a13c
13
+ on_success: change # options: [always|never|change] default: always
14
+ on_failure: always # options: [always|never|change] default: always
15
+ on_start: false # default: false
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crono (0.0.1)
5
+ activejob (~> 4.0)
6
+ activesupport (~> 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activejob (4.2.0)
12
+ activesupport (= 4.2.0)
13
+ globalid (>= 0.3.0)
14
+ activesupport (4.2.0)
15
+ i18n (~> 0.7)
16
+ json (~> 1.7, >= 1.7.7)
17
+ minitest (~> 5.1)
18
+ thread_safe (~> 0.3, >= 0.3.4)
19
+ tzinfo (~> 1.1)
20
+ diff-lcs (1.2.5)
21
+ globalid (0.3.3)
22
+ activesupport (>= 4.1.0)
23
+ i18n (0.7.0)
24
+ json (1.8.2)
25
+ minitest (5.5.1)
26
+ rake (10.4.2)
27
+ rspec (3.2.0)
28
+ rspec-core (~> 3.2.0)
29
+ rspec-expectations (~> 3.2.0)
30
+ rspec-mocks (~> 3.2.0)
31
+ rspec-core (3.2.1)
32
+ rspec-support (~> 3.2.0)
33
+ rspec-expectations (3.2.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.2.0)
36
+ rspec-mocks (3.2.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.2.0)
39
+ rspec-support (3.2.2)
40
+ thread_safe (0.3.4)
41
+ timecop (0.7.3)
42
+ tzinfo (1.2.2)
43
+ thread_safe (~> 0.1)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (>= 1.0.0)
50
+ crono!
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+ timecop (~> 0.7)
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2015 Dzmitry Plashchynski <plashchynski@gmail.com>
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ Copyright 2015 Dzmitry Plashchynski <plashchynski@gmail.com>
2
+ Licensed under the Apache License, Version 2.0
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ Crono — Job scheduler for Rails
2
+ ------------------------
3
+ [![Build Status](https://travis-ci.org/plashchynski/crono.svg?branch=master)](https://travis-ci.org/plashchynski/crono)
4
+ [![Code Climate](https://codeclimate.com/github/plashchynski/crono/badges/gpa.svg)](https://codeclimate.com/github/plashchynski/crono)
5
+ [![security](https://hakiri.io/github/plashchynski/crono/master.svg)](https://hakiri.io/github/plashchynski/crono/master)
6
+
7
+ Crono is a time-based background job scheduler daemon (just like Cron) for Ruby on Rails. It's pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it everywhere.
8
+
9
+
10
+ ## Requirements
11
+
12
+ Tested with latest MRI Ruby (2.2, 2.1 and 2.0) and Rails 3.2+
13
+ Other versions are untested but might work fine.
14
+
15
+
16
+ ## Installation
17
+
18
+ Add the following line to your application's Gemfile:
19
+
20
+ gem 'crono'
21
+
22
+ Run the bundle command to install it.
23
+ After you install Crono, you can run the generator:
24
+
25
+ rails generate crono:install
26
+
27
+ It will create a configuration file `config/cronotab.rb`
28
+ Now you are ready to move forward to create a job and schedule it.
29
+
30
+
31
+ ## Usage
32
+
33
+ #### Create Job
34
+
35
+ Crono can use Active Job jobs in `app/jobs/`. The only requirements is that the `perform` method should take no arguments.
36
+
37
+ Here's an example of a test job:
38
+ app/jobs/test_job.rb
39
+
40
+ class TestJob < ActiveJob::Base
41
+ def perform
42
+ # put you scheduled code here
43
+ # Comments.deleted.clean_up...
44
+ end
45
+ end
46
+
47
+ The Active Job jobs is convenient because you can use one class in both periodic and enqueued ways. But it doesn't necessarily. Any class can be used as Job if it has a method `perform` without arguments:
48
+
49
+ class TestJob # this is not active job class
50
+ def perform
51
+ # put you scheduled code here
52
+ # Comments.deleted.clean_up...
53
+ end
54
+ end
55
+
56
+
57
+ #### Schedule Jobs
58
+
59
+ The schedule described in the configuration file `config/cronotab.rb`, that we created using `rails generate crono:install` or manually. The semantic is pretty straightforward:
60
+
61
+ Crono.perform(TestJob).every 2.days, at: "15:30"
62
+
63
+ You can schedule one job a few times if you want the job to be performed a few times a day:
64
+
65
+ Crono.perform(TestJob).every 1.days, at: "00:00"
66
+ Crono.perform(TestJob).every 1.days, at: "12:00"
67
+
68
+ The `at` can be a Hash:
69
+
70
+ Crono.perform(TestJob).every 1.days, at: {hour: 12, min: 15}
71
+
72
+
73
+ #### Run daemon
74
+
75
+ To run Crono daemon, in your Rails project root directory:
76
+
77
+ bundle exec crono RAILS_ENV=development
78
+
79
+
80
+ ## License
81
+
82
+ Copyright 2015 Dzmitry Plashchynski <plashchynski@gmail.com>
83
+ Licensed under the Apache License, Version 2.0
84
+ Please see [LICENSE](https://github.com/plashchynski/crono/blob/master/LICENSE) for licensing details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ task default: :spec
data/bin/crono ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+
5
+ require "crono/cli"
6
+
7
+ begin
8
+ cli = Crono::CLI.instance
9
+ cli.run
10
+ rescue => e
11
+ raise e if $DEBUG
12
+ STDERR.puts e.message
13
+ STDERR.puts e.backtrace.join("\n")
14
+ exit 1
15
+ end
data/crono.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/crono/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "crono"
6
+ s.version = Crono::VERSION
7
+ s.authors = ["Dzmitry Plashchynski"]
8
+ s.email = ["plashchynski@gmail.com"]
9
+ s.homepage = "https://github.com/plashchynski/crono"
10
+ s.description = s.summary = "Job scheduler for Rails"
11
+ s.license = "Apache-2.0"
12
+
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.rubyforge_project = "crono"
15
+
16
+ s.add_runtime_dependency "activejob", "~> 4.0"
17
+ s.add_runtime_dependency "activesupport", "~> 4.0"
18
+ s.add_development_dependency "rake", "~> 10.0"
19
+ s.add_development_dependency "bundler", ">= 1.0.0"
20
+ s.add_development_dependency "rspec", "~> 3.0"
21
+ s.add_development_dependency "timecop", "~> 0.7"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
25
+ s.require_path = 'lib'
26
+ end
data/lib/crono.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Crono
2
+ end
3
+
4
+ require "active_support/all"
5
+ require "crono/version.rb"
6
+ require "crono/period.rb"
7
+ require "crono/schedule.rb"
8
+ require "crono/config.rb"
9
+ require "crono/performer_proxy.rb"
data/lib/crono/cli.rb ADDED
@@ -0,0 +1,98 @@
1
+ require 'crono'
2
+ require 'optparse'
3
+
4
+ module Crono
5
+ mattr_accessor :schedule
6
+
7
+ class CLI
8
+ include Singleton
9
+ attr_accessor :config
10
+ attr_accessor :schedule
11
+ attr_accessor :logger
12
+
13
+ def initialize
14
+ self.config = Config.new
15
+ self.schedule = Schedule.new
16
+ Crono.schedule = schedule
17
+ end
18
+
19
+ def run
20
+ parse_options(ARGV)
21
+ init_logger
22
+ daemonize if config.daemonize
23
+ write_pid
24
+ load_rails
25
+ print_banner
26
+ start_working_loop
27
+ end
28
+
29
+ private
30
+ def daemonize
31
+ ::Process.daemon(true, true)
32
+
33
+ [$stdout, $stderr].each do |io|
34
+ File.open(config.logfile, 'ab') { |f| io.reopen(f) }
35
+ io.sync = true
36
+ end
37
+ $stdin.reopen("/dev/null")
38
+ end
39
+
40
+ def write_pid
41
+ pidfile = File.expand_path(config.pidfile)
42
+ File.write(pidfile, ::Process.pid)
43
+ end
44
+
45
+ def init_logger
46
+ logfile = config.daemonize ? config.logfile : STDOUT
47
+ self.logger = Logger.new(logfile)
48
+ end
49
+
50
+ def print_banner
51
+ logger.info "Loading Crono #{Crono::VERSION}"
52
+ logger.info "Running in #{RUBY_DESCRIPTION}"
53
+ end
54
+
55
+ def load_rails
56
+ require 'rails'
57
+ require File.expand_path("config/environment.rb")
58
+ ::Rails.application.eager_load!
59
+ require File.expand_path(config.cronotab)
60
+ end
61
+
62
+ def run_job(klass)
63
+ logger.info "Perform #{klass}"
64
+ Thread.new { klass.new.perform }
65
+ end
66
+
67
+ def start_working_loop
68
+ loop do
69
+ klass, time = schedule.next
70
+ sleep(time - Time.now)
71
+ run_job(klass)
72
+ end
73
+ end
74
+
75
+ def parse_options(argv)
76
+ OptionParser.new do |opts|
77
+ opts.banner = "Usage: crono [options]"
78
+
79
+ opts.on("-C", "--cronotab PATH", "Path to cronotab file (Default: #{config.cronotab})") do |cronotab|
80
+ config.cronotab = cronotab
81
+ end
82
+
83
+ opts.on("-L", "--logfile PATH", "Path to writable logfile (Default: #{config.logfile})") do |logfile|
84
+ config.logfile = logfile
85
+ end
86
+
87
+ opts.on("-P", "--pidfile PATH", "Path to pidfile (Default: #{config.pidfile})") do |pidfile|
88
+ config.pidfile = pidfile
89
+ end
90
+
91
+ opts.on("-d", "--[no-]daemonize", "Daemonize process (Default: #{config.daemonize})") do |daemonize|
92
+ config.daemonize = daemonize
93
+ end
94
+
95
+ end.parse!(argv)
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,19 @@
1
+ module Crono
2
+ class Config
3
+ CRONOTAB = "config/cronotab.rb"
4
+ LOGFILE = "log/crono.log"
5
+ PIDFILE = "tmp/pids/crono.pid"
6
+
7
+ attr_accessor :cronotab
8
+ attr_accessor :logfile
9
+ attr_accessor :pidfile
10
+ attr_accessor :daemonize
11
+
12
+ def initialize
13
+ self.cronotab = CRONOTAB
14
+ self.logfile = LOGFILE
15
+ self.pidfile = PIDFILE
16
+ self.daemonize = false
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module Crono
2
+ class PerformerProxy
3
+ def initialize(performer, schedule)
4
+ @performer = performer
5
+ @schedule = schedule
6
+ end
7
+
8
+ def every(period, *args)
9
+ @schedule.add(@performer, Period.new(period, *args))
10
+ end
11
+ end
12
+
13
+ def self.perform(performer)
14
+ PerformerProxy.new(performer, Crono.schedule)
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ module Crono
2
+ class Period
3
+ def initialize(period, at: nil)
4
+ @period = period
5
+ @at_hour, @at_min = parse_at(at) if at
6
+ end
7
+
8
+ def next
9
+ @period.from_now.change({hour: @at_hour, min: @at_min}.compact)
10
+ end
11
+
12
+ def parse_at(at)
13
+ case at
14
+ when String
15
+ time = Time.parse(at)
16
+ return time.hour, time.min
17
+ when Hash
18
+ return at[:hour], at[:min]
19
+ else
20
+ raise "Unknown 'at' format"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ module Crono
2
+ class Schedule
3
+ def initialize
4
+ @schedule = []
5
+ end
6
+
7
+ def add(peformer, period)
8
+ @schedule << [peformer, period]
9
+ end
10
+
11
+ def next
12
+ [queue.first[0], queue.first[1].next]
13
+ end
14
+
15
+ private
16
+ def queue
17
+ @schedule.sort { |a,b| a[1].next <=> b[1].next }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Crono
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ module Crono
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "Installs crono and generates the necessary configuration files"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ def copy_config
8
+ template 'cronotab.rb.erb', 'config/cronotab.rb'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ # cronotab.rb — Crono configuration file
2
+ #
3
+ # Here you can specify periodic jobs and their schedule.
4
+ # You can specify a periodic job as a ActiveJob class in `app/jobs/`
5
+ # Actually you can use any class. The only requirement is that
6
+ # the class should implement a method `perform` without arguments.
7
+ #
8
+ # Crono.perform(TestJob).every 2.days, at: "15:30"
9
+ #
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+ require 'crono/cli'
3
+
4
+ class TestJob
5
+ def perform;end
6
+ end
7
+
8
+ describe Crono::CLI do
9
+ let(:cli) { Crono::CLI.instance }
10
+
11
+ describe "#run" do
12
+ it "should try to initialize rails with #load_rails and start working loop" do
13
+ expect(cli).to receive(:load_rails)
14
+ expect(cli).to receive(:start_working_loop)
15
+ expect(cli).to receive(:parse_options)
16
+ expect(cli).to receive(:write_pid)
17
+ cli.run
18
+ end
19
+ end
20
+
21
+ describe "#run_job" do
22
+ it "should run job in separate thread" do
23
+ thread = cli.send(:run_job, TestJob).join
24
+ expect(thread).to be_stop
25
+ end
26
+ end
27
+
28
+ describe "#start_working_loop" do
29
+ it "should start working loop"
30
+ end
31
+
32
+ describe "#parse_options" do
33
+ it "should set cronotab" do
34
+ cli.send(:parse_options, ["--cronotab", "/tmp/cronotab.rb"])
35
+ expect(cli.config.cronotab).to be_eql "/tmp/cronotab.rb"
36
+ end
37
+
38
+ it "should set logfile" do
39
+ cli.send(:parse_options, ["--logfile", "log/crono.log"])
40
+ expect(cli.config.logfile).to be_eql "log/crono.log"
41
+ end
42
+
43
+ it "should set pidfile" do
44
+ cli.send(:parse_options, ["--pidfile", "tmp/pids/crono.0.log"])
45
+ expect(cli.config.pidfile).to be_eql "tmp/pids/crono.0.log"
46
+ end
47
+
48
+ it "should set daemonize" do
49
+ cli.send(:parse_options, ["--daemonize"])
50
+ expect(cli.config.daemonize).to be true
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe Crono::Config do
4
+ describe "#initialize" do
5
+ it "should initialize with default configuration options" do
6
+ @config = Crono::Config.new
7
+ expect(@config.cronotab).to be Crono::Config::CRONOTAB
8
+ expect(@config.logfile).to be Crono::Config::LOGFILE
9
+ expect(@config.pidfile).to be Crono::Config::PIDFILE
10
+ expect(@config.daemonize).to be false
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ class TestJob
4
+ def perform;end
5
+ end
6
+
7
+ describe Crono::PerformerProxy do
8
+ it "should add job and period to schedule" do
9
+ expect(Crono.schedule).to receive(:add).with(TestJob, kind_of(Crono::Period))
10
+ Crono.perform(TestJob).every(2.days, at: "15:30")
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Crono::Period do
4
+ around(:each) do |example|
5
+ Timecop.freeze do
6
+ example.run
7
+ end
8
+ end
9
+
10
+ describe "#next" do
11
+ context "in daily basis" do
12
+ it "should return the time 2 days from now" do
13
+ @period = Crono::Period.new(2.day)
14
+ expect(@period.next).to be_eql(2.day.from_now)
15
+ end
16
+
17
+ it "should set time to 'at' time as a string" do
18
+ @period = Crono::Period.new(2.day, at: "15:20")
19
+ expect(@period.next).to be_eql(2.day.from_now.change(hour: 15, min: 20))
20
+ end
21
+
22
+ it "should set time to 'at' time as a hash" do
23
+ at = {hour: 18, min: 45}
24
+ @period = Crono::Period.new(2.day, at: at)
25
+ expect(@period.next).to be_eql(2.day.from_now.change(at))
26
+ end
27
+
28
+ it "should raise error when 'at' is wrong" do
29
+ expect {
30
+ @period = Crono::Period.new(2.day, at: 1)
31
+ }.to raise_error("Unknown 'at' format")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ class TestJob
4
+ def perform;end
5
+ end
6
+
7
+ describe Crono::Schedule do
8
+ describe "#next" do
9
+ it "should return next job in schedule" do
10
+ @schedule = Crono::Schedule.new
11
+ [
12
+ Crono::Period.new(3.day, at: "18:55"),
13
+ Crono::Period.new(1.day, at: "15:30"),
14
+ Crono::Period.new(7.day, at: "06:05")
15
+ ].each { |period| @schedule.add(TestJob, period) }
16
+
17
+ expect(@schedule.next).to be_eql([TestJob, 1.day.from_now.change(hour: 15, min: 30)])
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'timecop'
5
+ require 'crono'
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crono
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Dzmitry Plashchynski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activejob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: timecop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.7'
97
+ description: Job scheduler for Rails
98
+ email:
99
+ - plashchynski@gmail.com
100
+ executables:
101
+ - crono
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE
111
+ - NOTICE
112
+ - README.md
113
+ - Rakefile
114
+ - bin/crono
115
+ - crono.gemspec
116
+ - lib/crono.rb
117
+ - lib/crono/cli.rb
118
+ - lib/crono/config.rb
119
+ - lib/crono/performer_proxy.rb
120
+ - lib/crono/period.rb
121
+ - lib/crono/schedule.rb
122
+ - lib/crono/version.rb
123
+ - lib/generators/crono/install/install_generator.rb
124
+ - lib/generators/crono/install/templates/cronotab.rb.erb
125
+ - spec/cli_spec.rb
126
+ - spec/config_spec.rb
127
+ - spec/performer_proxy_spec.rb
128
+ - spec/period_spec.rb
129
+ - spec/schedule_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/plashchynski/crono
132
+ licenses:
133
+ - Apache-2.0
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 1.3.6
149
+ requirements: []
150
+ rubyforge_project: crono
151
+ rubygems_version: 2.4.5
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Job scheduler for Rails
155
+ test_files: []