whirl 0.0.3

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.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .autotest
20
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rake'
6
+ gem 'autotest'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jesse Trimble
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.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Whirl
2
+
3
+ A watered-down gem for looping over jobs. Pre-wired with Redis and HTTParty. Built with Heroku usage in mind.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'whirl'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install whirl
18
+
19
+ ## Usage
20
+
21
+ A contrived example, but this is all you need:
22
+
23
+ ```ruby
24
+ require 'whirl'
25
+
26
+ class Whirl::Job::DoBusiness < Whirl::Job::Base
27
+ action do
28
+ say "HA HA! _BUSINESS_: http://i.imgur.com/EFcX1.jpg", :red
29
+ end
30
+ end
31
+
32
+ class Whirl::Job::MonitorSomeStuff < Whirl::Job::Base
33
+ action do
34
+ # Monitor a website, make api calls, send emails...
35
+ say "Do ALL THE MONITORING!"
36
+ end
37
+ end
38
+
39
+ Whirl.run # <= continuously loops over the DoBusiness and MonitorSomeStuff jobs
40
+ ```
41
+
42
+ ## TODO
43
+
44
+ - Finish documentation
45
+ - Detailed usage examples
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require File.expand_path("../lib/whirl", __FILE__)
4
+
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc "Run all examples"
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_opts = %w[--color]
11
+ end
12
+
13
+ task :default => [:spec]
14
+ rescue LoadError; end
@@ -0,0 +1,49 @@
1
+ module Whirl
2
+
3
+ module Job
4
+
5
+ class Base
6
+
7
+ class << self
8
+ attr_accessor :job_action
9
+ end
10
+
11
+ def self.action(&block)
12
+ self.job_action = block
13
+ end
14
+
15
+ def self.perform
16
+ say "starting...", :blue
17
+ begin
18
+ job_action.call
19
+ rescue Exception => e
20
+ say "Job #{self.name} failed!", :red
21
+ say ".. Reason: #{e.message}", :red
22
+ if ENV['DEBUG']
23
+ say ".. Backtrace:"
24
+ puts e.backtrace
25
+ end
26
+ ensure
27
+ end
28
+ end
29
+
30
+ def self.newline
31
+ puts "\n"
32
+ end
33
+
34
+ def self.say(*args)
35
+ args.push :cyan if args.size == 1
36
+ @shell ||= Thor::Shell::Color.new
37
+ message = "#{self.name.ljust(28, " ")} === #{args.shift}"
38
+ @shell.say *[message, *args]
39
+ end
40
+
41
+ def self.redis_key(val)
42
+ "#{self.name}:#{val}"
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,24 @@
1
+ require 'forwardable'
2
+
3
+ module Whirl
4
+
5
+ class Redis
6
+ extend Forwardable
7
+ include Singleton
8
+
9
+ def_delegators :@client, :get, :set
10
+
11
+ attr_accessor :client
12
+
13
+ def initialize
14
+ self.client = if redis_url = ENV['REDISTOGO_URL']
15
+ u = URI.parse(redis_url)
16
+ ::Redis.new host: u.host, port: u.port, password: u.password
17
+ else
18
+ ::Redis.new
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module Whirl
2
+ VERSION = '0.0.3'
3
+ end
data/lib/whirl.rb ADDED
@@ -0,0 +1,50 @@
1
+ $:.unshift File.expand_path("..", __FILE__) unless $:.include? File.expand_path("..", __FILE__)
2
+
3
+ require 'rake'
4
+ require 'thor'
5
+ require 'httparty'
6
+ require 'redis'
7
+ require 'json'
8
+
9
+ require 'whirl/version'
10
+ require 'whirl/redis'
11
+ require 'whirl/job/base'
12
+
13
+ module Whirl
14
+
15
+ def self.jobs
16
+ jobs = Whirl::Job.constants.
17
+ map { |d| Whirl::Job.const_get(d) }.
18
+ select { |d| d.respond_to? :perform }
19
+
20
+ jobs -= [Whirl::Job::Base]
21
+ end
22
+
23
+ def self.run
24
+ trap("SIGINT") do
25
+ Whirl::Job::Base.newline
26
+ Whirl::Job::Base.say "Whirled.", :green
27
+ Whirl::Job::Base.newline
28
+ exit 0
29
+ end
30
+
31
+ begin
32
+ jobs.each do |j|
33
+ fork do
34
+ j.perform
35
+ end
36
+ end
37
+
38
+ sleep whirl_interval.to_i
39
+ end while loop?
40
+ end
41
+
42
+ def self.loop?
43
+ true
44
+ end
45
+
46
+ def self.whirl_interval
47
+ ENV['WHIRL_LOOP_INTERVAL'] || 10
48
+ end
49
+
50
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Whirl do
4
+
5
+ module Whirl::Job
6
+ class FakeJob
7
+ def self.perform(*); end
8
+ end
9
+
10
+ class Nope; end
11
+ end
12
+
13
+ describe '.jobs' do
14
+
15
+ it "returns an array of all constants under Whirl::Job that respond to #perform, less Base" do
16
+ Whirl.jobs.should == [Whirl::Job::FakeJob]
17
+ end
18
+
19
+ end
20
+
21
+ describe '.run' do
22
+
23
+ # Don't want Whirl to fork, keep spinning or have delays.
24
+ #
25
+ before do
26
+ Whirl.stub(:fork) do |&block|
27
+ block.call
28
+ end
29
+ Whirl.stub(:loop?).and_return(false)
30
+ Whirl.stub(:whirl_interval).and_return(0)
31
+ end
32
+
33
+ it 'knows its jobs' do
34
+ job_mock = mock
35
+ job_mock.should_receive(:perform)
36
+
37
+ Whirl.stub(:jobs).and_return [job_mock]
38
+ Whirl.run
39
+ end
40
+
41
+ it 'runs each job' do
42
+ class Whirl::Job::FakeAgain
43
+ def perform(*); end
44
+ end
45
+
46
+ Whirl::Job::FakeJob.should_receive(:perform).once
47
+ Whirl::Job::FakeAgain.should_receive(:perform).once
48
+
49
+ Whirl.run
50
+ end
51
+
52
+ end
53
+
54
+ end
@@ -0,0 +1,3 @@
1
+ File.expand_path("../../lib", __FILE__).tap { |p| $:.unshift p unless $:.include? p }
2
+
3
+ require 'whirl'
data/whirl.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/whirl/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jesse Trimble"]
6
+ gem.email = ["jlowelltrim@gmail.com"]
7
+ gem.description = %q{A watered-down task runner pre-wired with Redis. Built with Heroku Cedar in mind.}
8
+ gem.summary = %q{A watered-down task runner pre-wired with Redis. Built with Heroku Cedar in mind.}
9
+ gem.homepage = ""
10
+
11
+ gem.add_dependency 'thor'
12
+ gem.add_dependency 'httparty'
13
+ gem.add_dependency 'redis'
14
+
15
+ gem.add_development_dependency 'rspec'
16
+
17
+ gem.files = `git ls-files`.split($\)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.name = "whirl"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = Whirl::VERSION
23
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whirl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jesse Trimble
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &70311718593320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70311718593320
25
+ - !ruby/object:Gem::Dependency
26
+ name: httparty
27
+ requirement: &70311718592820 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70311718592820
36
+ - !ruby/object:Gem::Dependency
37
+ name: redis
38
+ requirement: &70311718592340 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70311718592340
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70311718591840 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70311718591840
58
+ description: A watered-down task runner pre-wired with Redis. Built with Heroku Cedar
59
+ in mind.
60
+ email:
61
+ - jlowelltrim@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .travis.yml
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - lib/whirl.rb
73
+ - lib/whirl/job/base.rb
74
+ - lib/whirl/redis.rb
75
+ - lib/whirl/version.rb
76
+ - spec/lib/whirl_spec.rb
77
+ - spec/spec_helper.rb
78
+ - whirl.gemspec
79
+ homepage: ''
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.10
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A watered-down task runner pre-wired with Redis. Built with Heroku Cedar
103
+ in mind.
104
+ test_files:
105
+ - spec/lib/whirl_spec.rb
106
+ - spec/spec_helper.rb