aaron 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1bac6b442799706965c1e2b13fc0bd1bf3808485
4
+ data.tar.gz: e2bb52f3214b1bdc35f0fba1b46497202c6de0ba
5
+ SHA512:
6
+ metadata.gz: 88e9da757093ff31cd6ca3619040bf9dc002d7e00ad75a86528003447c4b16edb82f5d48de1f5a8260f7661413f057e6c88de2856e408d06e33a3822374a903c
7
+ data.tar.gz: 187029d9dbf321e6d219e9db5532eb2aec824c55b831adc2f98080ecc9a69ab309b58011184048c2b669eb23abee4fbfdef22c21cb37e16fb965c2314105a41a
@@ -0,0 +1,8 @@
1
+ /.bundle
2
+ /vendor
3
+ .DS_Store
4
+ Gemfile.lock
5
+ /daemon.rb.pid
6
+ /bin/test
7
+ /bin/out
8
+ /pkg
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,11 @@
1
+ script: 'bundle exec rspec'
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
7
+
8
+ addons:
9
+ code_climate:
10
+ repo_token:
11
+ secure: 2828a2cfcfe595896162c943429a3f5cc69c1c0edee9995677160993a7943d99
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "codeclimate-test-reporter", group: :test, require: nil
4
+
5
+ gemspec
@@ -0,0 +1,9 @@
1
+ guard :rspec, cmd: "rspec" do
2
+ watch(%r{^lib/(.+).rb$}) do |m|
3
+ "spec/#{m[1]}_spec.rb"
4
+ end
5
+
6
+ watch(%r{^spec/(.+).rb$}) do |m|
7
+ "spec/#{m[1]}.rb"
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 William Yaworsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,14 @@
1
+ # Aaron (random cron)
2
+
3
+ [![Code Climate](https://codeclimate.com/github/legion-of-bots/aaron/badges/gpa.svg)](https://codeclimate.com/github/legion-of-bots/aaron) [![Test Coverage](https://codeclimate.com/github/legion-of-bots/aaron/badges/coverage.svg)](https://codeclimate.com/github/legion-of-bots/aaron) [![Build Status](https://travis-ci.org/legion-of-bots/aaron.svg?branch=master)](https://travis-ci.org/legion-of-bots/aaron) [![Dependency Status](https://gemnasium.com/legion-of-bots/aaron.svg)](https://gemnasium.com/legion-of-bots/aaron)
4
+
5
+ Randomize the execution of tasks
6
+
7
+ ## TODO
8
+
9
+ - [x] daemonize Aaron
10
+ - [x] add seconds / minutes / hours methods
11
+ - [x] better support for running shell commands
12
+ - [ ] more unit tests
13
+ - [x] integrate with Travis
14
+ - [ ] documentation
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
8
+ rescue LoadError
9
+ end
10
+
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aaron/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'aaron'
8
+ spec.version = Aaron::VERSION
9
+ spec.authors = ['William Yaworsky']
10
+ spec.email = ['wj.px01@gmail.com']
11
+ spec.summary = %q{Randomized cron.}
12
+ spec.homepage = 'https://github.com/legion-of-bots/aaron'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ spec.add_dependency 'daemons', '1.2.2 '
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.5'
25
+ spec.add_development_dependency 'rake'
26
+
27
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
28
+ spec.add_development_dependency 'memfs'
29
+ spec.add_development_dependency 'guard'
30
+ spec.add_development_dependency 'guard-rspec'
31
+ spec.add_development_dependency 'pry'
32
+ spec.add_development_dependency 'pry-rescue'
33
+ spec.add_development_dependency 'pry-stack_explorer'
34
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'daemons'
4
+
5
+ Daemons.run(File.expand_path("#{File.dirname(__FILE__)}/../daemon.rb"))
@@ -0,0 +1,3 @@
1
+ require_relative 'lib/aaron'
2
+
3
+ Aaron::Aaron.run
@@ -0,0 +1,58 @@
1
+ %w[
2
+
3
+ dsl
4
+ errors
5
+ extensions
6
+ scheduler
7
+ task
8
+ version
9
+
10
+ ].each { |f| require_relative "aaron/#{f}" }
11
+
12
+ module Aaron
13
+
14
+ class Aaron
15
+
16
+ attr_reader :config, :file
17
+
18
+ @@possible_config_file_locations = %w[
19
+ ~/aaron.rb
20
+ ].map { |f| File.expand_path(f) }
21
+
22
+ def self.locate_config_file
23
+ @@possible_config_file_locations.find { |p| File.exists?(p) }
24
+ end
25
+
26
+ def self.run
27
+ config_file = Aaron.locate_config_file
28
+ if config_file
29
+ Aaron.from_file(config_file).run
30
+ else
31
+ raise Errors::ConfigFileNotFound.new %Q(
32
+ Aaron was unable to locate his config file.
33
+ He looked in
34
+ #{@@possible_config_file_locations.join("\n")}
35
+ )
36
+ end
37
+ end
38
+
39
+ def self.from_file file
40
+ Aaron.new(File.read(file), file)
41
+ end
42
+
43
+ def initialize config, file = nil
44
+ @config = config
45
+ @file = file
46
+ end
47
+
48
+ def get_binding
49
+ DSL.new.instance_eval { binding }
50
+ end
51
+
52
+ def run
53
+ eval(config, get_binding, file).run
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,23 @@
1
+ module Aaron
2
+
3
+ class DSL
4
+
5
+ attr_reader :scheduler
6
+
7
+ def initialize
8
+ @scheduler = Scheduler.new
9
+ end
10
+
11
+ def every range, proc = nil, &block
12
+ scheduler << Task.new(range, proc, &block)
13
+ scheduler
14
+ end
15
+
16
+ def exec &block
17
+ block.type = :exec
18
+ block
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,10 @@
1
+ module Aaron
2
+
3
+ module Errors
4
+
5
+ class ConfigFileNotFound < Exception
6
+ end
7
+
8
+ end
9
+
10
+ end
@@ -0,0 +1,41 @@
1
+ class Fixnum
2
+
3
+ def to other
4
+ (self..other)
5
+ end
6
+
7
+ def second
8
+ self
9
+ end
10
+
11
+ def minute
12
+ second * 60
13
+ end
14
+
15
+ def hour
16
+ minute * 60
17
+ end
18
+
19
+ def day
20
+ hour * 24
21
+ end
22
+
23
+ def week
24
+ day * 7
25
+ end
26
+
27
+ def method_missing m, *args, &block
28
+ if /(.+)s$/.match(m.to_s) and self.respond_to?($1) # support for seconds, minutes etc
29
+ self.send($1)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ class Proc
38
+
39
+ attr_accessor :type
40
+
41
+ end
@@ -0,0 +1,34 @@
1
+ module Aaron
2
+
3
+ class Scheduler
4
+
5
+ attr_reader :tasks
6
+
7
+ def initialize
8
+ @tasks = []
9
+ end
10
+
11
+ def run
12
+ while true do
13
+ n = next_sleep
14
+ t = tasks.each { |task| task.apply_time!(n) }.select &:ready_to_trigger?
15
+ sleep(n)
16
+ t.each &:trigger
17
+ end
18
+ end
19
+
20
+ def next_sleep
21
+ tasks.map { |task| task.next_sleep }.sort.first
22
+ end
23
+
24
+ def add_task task
25
+ self << task
26
+ end
27
+
28
+ def << task
29
+ @tasks << task
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,54 @@
1
+ module Aaron
2
+
3
+ class Task
4
+
5
+ attr_reader :range, :block
6
+
7
+ def initialize range, proc = nil, &block
8
+ @range = range
9
+ @block = proc or block
10
+ end
11
+
12
+ def next_sleep
13
+ @next_sleep ||= get_next_sleep
14
+ end
15
+
16
+ def apply_time! time
17
+ @next_sleep -= time
18
+ if @next_sleep == 0
19
+ @next_sleep = nil
20
+ end
21
+ end
22
+
23
+ def ready_to_trigger?
24
+ @next_sleep.nil?
25
+ end
26
+
27
+ def trigger
28
+ if block.type then self.send(block.type) else block.call end
29
+ end
30
+
31
+ protected
32
+
33
+ def get_next_sleep
34
+ if range.instance_of?(Range)
35
+ rand(range)
36
+ else
37
+ range
38
+ end
39
+ end
40
+
41
+ def exec
42
+ cmd = block.call.split(' ')
43
+ bin = File.expand_path(cmd[0])
44
+ if not File.exists?(bin)
45
+ bin = cmd[0]
46
+ end
47
+ rest = cmd.drop(1).join(' ')
48
+ bin += ' ' + rest
49
+ `#{bin}`
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ module Aaron
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,24 @@
1
+ describe Aaron::Aaron do
2
+ subject { Aaron::Aaron }
3
+
4
+ describe ".locate_config_file" do
5
+
6
+ context "a config file does not exist", config_file_exists: false do
7
+ it "should return nil" do
8
+ expect(subject.locate_config_file).to be_nil
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+ describe ".run" do
15
+
16
+ context "a config file does not exist", config_file_exists: false do
17
+ it "should throw an exception" do
18
+ expect { subject.run }.to raise_error(Aaron::Errors::ConfigFileNotFound)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,11 @@
1
+ describe Aaron::DSL do
2
+
3
+ describe ".every" do
4
+
5
+ it "should return a scheduler" do
6
+ expect(subject.every(1)).to be_instance_of(Aaron::Scheduler)
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,23 @@
1
+ shared_context "a config file exists", config_file_exists: true do
2
+
3
+ let(:config_file_path) { "~/aaron.rb" }
4
+
5
+ around :each do |example|
6
+ MemFs.activate do
7
+ FileUtils.mkdir_p(File.dirname(config_file_path))
8
+ FileUtils.touch(config_file_path)
9
+ example.run
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ shared_context "a config file does not exist", config_file_exists: false do
16
+
17
+ around :each do |example|
18
+ MemFs.activate do
19
+ example.run
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,20 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
5
+ require 'aaron'
6
+ require 'memfs'
7
+ require 'fileutils'
8
+
9
+ RSpec.configure do |config|
10
+ config.expect_with :rspec do |expectations|
11
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
12
+ end
13
+
14
+ config.mock_with :rspec do |mocks|
15
+ mocks.verify_partial_doubles = true
16
+ end
17
+
18
+ end
19
+
20
+ require 'contexts'
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aaron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - William Yaworsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: daemons
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: memfs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-rescue
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-stack_explorer
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description:
154
+ email:
155
+ - wj.px01@gmail.com
156
+ executables:
157
+ - aaron
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - .gitignore
162
+ - .rspec
163
+ - .travis.yml
164
+ - Gemfile
165
+ - Guardfile
166
+ - LICENSE.txt
167
+ - README.md
168
+ - Rakefile
169
+ - aaron.gemspec
170
+ - bin/aaron
171
+ - daemon.rb
172
+ - lib/aaron.rb
173
+ - lib/aaron/dsl.rb
174
+ - lib/aaron/errors.rb
175
+ - lib/aaron/extensions.rb
176
+ - lib/aaron/scheduler.rb
177
+ - lib/aaron/task.rb
178
+ - lib/aaron/version.rb
179
+ - spec/aaron/aaron_spec.rb
180
+ - spec/aaron/dsl_spec.rb
181
+ - spec/contexts.rb
182
+ - spec/spec_helper.rb
183
+ homepage: https://github.com/legion-of-bots/aaron
184
+ licenses:
185
+ - MIT
186
+ metadata: {}
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - '>='
194
+ - !ruby/object:Gem::Version
195
+ version: 1.9.3
196
+ required_rubygems_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - '>='
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ requirements: []
202
+ rubyforge_project:
203
+ rubygems_version: 2.0.14
204
+ signing_key:
205
+ specification_version: 4
206
+ summary: Randomized cron.
207
+ test_files:
208
+ - spec/aaron/aaron_spec.rb
209
+ - spec/aaron/dsl_spec.rb
210
+ - spec/contexts.rb
211
+ - spec/spec_helper.rb