refinery 0.12.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/lib/refinery.rb +1 -99
  2. data/refinery.gemspec +16 -117
  3. metadata +39 -118
  4. data/.gitignore +0 -6
  5. data/CHANGELOG +0 -2
  6. data/LICENSE +0 -21
  7. data/README.rdoc +0 -58
  8. data/README.textile +0 -58
  9. data/Rakefile +0 -43
  10. data/VERSION +0 -1
  11. data/bin/epub +0 -64
  12. data/bin/monitor +0 -47
  13. data/bin/pubnow +0 -61
  14. data/bin/refinery +0 -64
  15. data/config/config.example.yml +0 -21
  16. data/lib/refinery/beanstalk_queue.rb +0 -36
  17. data/lib/refinery/beanstalk_queue_provider.rb +0 -18
  18. data/lib/refinery/config.rb +0 -48
  19. data/lib/refinery/configurable.rb +0 -15
  20. data/lib/refinery/daemon.rb +0 -148
  21. data/lib/refinery/event_publisher.rb +0 -131
  22. data/lib/refinery/heartbeat.rb +0 -33
  23. data/lib/refinery/loggable.rb +0 -9
  24. data/lib/refinery/monitor.rb +0 -113
  25. data/lib/refinery/processor.rb +0 -55
  26. data/lib/refinery/publisher.rb +0 -42
  27. data/lib/refinery/queueable.rb +0 -48
  28. data/lib/refinery/server.rb +0 -88
  29. data/lib/refinery/statistics.rb +0 -61
  30. data/lib/refinery/stats_server.rb +0 -135
  31. data/lib/refinery/utilities.rb +0 -33
  32. data/lib/refinery/validations.rb +0 -48
  33. data/lib/refinery/worker.rb +0 -65
  34. data/logs/README +0 -1
  35. data/publishers/error.rb +0 -6
  36. data/publishers/sample.rb +0 -6
  37. data/publishers/sleep.rb +0 -5
  38. data/test/config.yml +0 -10
  39. data/test/test_helper.rb +0 -21
  40. data/test/unit/config_test.rb +0 -42
  41. data/test/unit/configurable_test.rb +0 -13
  42. data/test/unit/daemon_test.rb +0 -63
  43. data/test/unit/event_publisher_test.rb +0 -12
  44. data/test/unit/heartbeat_test.rb +0 -25
  45. data/test/unit/loggable_test.rb +0 -12
  46. data/test/unit/processor_test.rb +0 -34
  47. data/test/unit/publisher_test.rb +0 -13
  48. data/test/unit/queueable_test.rb +0 -26
  49. data/test/unit/server_test.rb +0 -34
  50. data/test/unit/statistics_test.rb +0 -44
  51. data/test/unit/utilities_test.rb +0 -25
  52. data/test/unit/validations_test.rb +0 -37
  53. data/test/unit/worker_test.rb +0 -44
  54. data/workers/error.rb +0 -8
  55. data/workers/sample.rb +0 -8
  56. data/workers/sleep.rb +0 -7
@@ -1,48 +0,0 @@
1
- module Refinery #:nodoc:
2
- # Error that is raised when a message is invalid.
3
- class InvalidMessageError < RuntimeError
4
- end
5
-
6
- # Module containing all validations.
7
- module Validations
8
- def self.included(base) # :nodoc:
9
- base.extend(ClassMethods)
10
- end
11
-
12
- # Class methods that are added to the worker.
13
- module ClassMethods
14
- # A list of all of the validators. Validators are lambdas
15
- # that will be called with the message as its only arg.
16
- # Note that the order of validators is retained.
17
- def validators
18
- @validators ||= []
19
- end
20
-
21
- # Validate with the given block. The block must receive a single
22
- # argument that is the message
23
- def validate_with(&block)
24
- validators << block
25
- end
26
- alias :validate :validate_with
27
-
28
- # Validate that each of the keys exists in the message.
29
- def validate_key_exists(*args)
30
- args.each do |key|
31
- validators << lambda do |message|
32
- raise Refinery::InvalidMessageError, "Key does not exist in message: #{key}" unless message[key]
33
- end
34
- end
35
- end
36
- alias :validates_key_exists :validate_key_exists
37
- alias :validates_presence_of :validate_key_exists
38
- end
39
-
40
- # Validate the given message
41
- protected
42
- def validate(message)
43
- self.class.validators.each do |validator|
44
- validator.call(message)
45
- end
46
- end
47
- end
48
- end
@@ -1,65 +0,0 @@
1
- module Refinery #:nodoc:
2
- # Base class for workers. Place subclasses of this in the workers
3
- # directory.
4
- #
5
- # Workers may include validation logic to verify that the message
6
- # has the correct keys and values before processing.
7
- class Worker
8
- include Refinery::Loggable
9
- include Refinery::Configurable
10
- include Refinery::Utilities
11
- include Refinery::Validations
12
- include Refinery::Queueable
13
-
14
- # Initialize the worker with the given daemon.
15
- def initialize(daemon)
16
- @daemon = daemon
17
- end
18
-
19
- # Run the worker with the given message. The result from the worker's
20
- # <code>execute</code> method is returned along with the run time.
21
- #
22
- # Validation will occur prior to calling execute.
23
- def run(message)
24
- result = false
25
-
26
- validate(message)
27
-
28
- logger.debug "Executing worker #{self.class.name}"
29
- time = Benchmark.realtime do
30
- begin
31
- result = execute(message)
32
- rescue Exception => e
33
- logger.error "Error executing worker #{self.class.name}: #{e.message}"
34
- raise e
35
- end
36
- end
37
- logger.debug "Completed worker #{self.class.name} in #{time} seconds"
38
- return result, time
39
- end
40
-
41
- # Get the data store for the worker.
42
- #
43
- # The data store is provided through the Moneta interface.
44
- #
45
- # If the configuration providers a data_store:class option then that class
46
- # will be used (the class must be in the Moneta module), otherwise
47
- # Moneta::S3 will be used.
48
- def data_store(options)
49
- class_name = processor_config['workers']['data_store']['class'] rescue 'S3'
50
- ds_class = Moneta.const_get(camelize(class_name))
51
- (@data_store ||= {})[options] ||= ds_class.new(
52
- :access_key_id => config['aws']['credentials']['access_key_id'],
53
- :secret_access_key => config['aws']['credentials']['secret_access_key'],
54
- :bucket => options[:bucket],
55
- :multi_thread => true
56
- )
57
- end
58
-
59
- protected
60
- # Get's the config element starting at the processer
61
- def processor_config
62
- config['processors'][daemon.name]
63
- end
64
- end
65
- end
@@ -1 +0,0 @@
1
- Log files from Refinery will be written here. Do not remove this directory.
@@ -1,6 +0,0 @@
1
- # An example publisher that posts a message to the queue that should raise an error.
2
- class ErrorPublisher < Refinery::Publisher
3
- def execute
4
- publish_if_empty({'text' => 'fire an error, please'})
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- # A sample publisher that posts a message to the queue.
2
- class SamplePublisher < Refinery::Publisher
3
- def execute
4
- publish_if_empty({'text' => 'hey there!'})
5
- end
6
- end
@@ -1,5 +0,0 @@
1
- class SleepPublisher < Refinery::Publisher
2
- def execute
3
- publish({'seconds' => rand(5) + 0.5})
4
- end
5
- end
@@ -1,10 +0,0 @@
1
- aws:
2
- credentials:
3
- access_key_id: "aaa"
4
- secret_access_key: "bbb"
5
- processors:
6
- sample:
7
- publishers:
8
- delay: 10
9
- workers:
10
- initial: 3
@@ -1,21 +0,0 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- require 'shoulda'
4
- require 'mocha'
5
- require 'refinery'
6
-
7
- class Test::Unit::TestCase
8
- def setup_default_config
9
- Refinery::Config.stubs(:default).returns(Refinery::Config.new(
10
- {
11
- 'aws' => {
12
- 'credentials' => {
13
- 'access_key_id' => 'aki',
14
- 'secret_access_key' => 'sak'
15
- }
16
- },
17
- 'processors' => []
18
- }
19
- ))
20
- end
21
- end
@@ -1,42 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ConfigTest < Test::Unit::TestCase
4
- context "the config class" do
5
- should "provide a default configuration" do
6
- assert_not_nil Refinery::Config.default
7
- end
8
-
9
- context "default configuration" do
10
- setup do
11
- @config = Refinery::Config.default
12
- end
13
- should "provide an empty aws credentials hash" do
14
- assert_equal Hash.new, @config['aws']['credentials']
15
- end
16
- should "provide an empty processors hash" do
17
- assert_equal Hash.new, @config['processors']
18
- end
19
- end
20
-
21
- context "after loading configuration from a YAML file" do
22
- setup do
23
- @config_file = File.dirname(__FILE__) + '/../config.yml'
24
- @config = Refinery::Config.new
25
- @config.load_file(@config_file)
26
- end
27
- should "have aws credentials" do
28
- assert_equal 'aaa', @config['aws']['credentials']['access_key_id']
29
- assert_equal 'bbb', @config['aws']['credentials']['secret_access_key']
30
- end
31
- should "reload the file when changed" do
32
- `touch #{@config_file}`
33
- YAML.expects(:load_file).once
34
- @config.refresh
35
- end
36
- should "not reload the file when not changed" do
37
- YAML.expects(:load_file).never
38
- @config.refresh
39
- end
40
- end
41
- end
42
- end
@@ -1,13 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ConfigureMe
4
- include Refinery::Configurable
5
- end
6
-
7
- class ConfigurableTest < Test::Unit::TestCase
8
- context "a class with the configurable module" do
9
- should "provide a config" do
10
- assert_not_nil ConfigureMe.new.config
11
- end
12
- end
13
- end
@@ -1,63 +0,0 @@
1
- require 'test_helper'
2
-
3
- class DaemonTest < Test::Unit::TestCase
4
- context "a daemon" do
5
- setup do
6
- @server = stub('Server')
7
- @processor = stub('Processor', :server => @server)
8
-
9
- @waiting_queue = stub('Queue(waiting)')
10
- @error_queue = stub('Queue(error)')
11
- @done_queue = stub('Queue(done)')
12
-
13
- @provider = stub('QueueProvider')
14
- if defined?(Typica)
15
- Typica::Sqs::QueueService.stubs(:new).returns(@provider)
16
- else
17
- RightAws::SqsGen2.stubs(:new).returns(@provider)
18
- end
19
- @provider.stubs(:queue).with('sample_waiting').returns(@waiting_queue)
20
- @provider.stubs(:queue).with('sample_error').returns(@error_queue)
21
- @provider.stubs(:queue).with('sample_done').returns(@done_queue)
22
- end
23
- should "be startable" do
24
- @waiting_queue.stubs(:receive)
25
- assert_nothing_raised do
26
- daemon = Refinery::Daemon.new(@processor, 'sample')
27
- end
28
- end
29
- should "have logging" do
30
- @waiting_queue.stubs(:receive)
31
- daemon = Refinery::Daemon.new(@processor, 'sample')
32
- assert_not_nil daemon.logger
33
- end
34
- should "allow visibility setting" do
35
- @waiting_queue.stubs(:receive).with(600)
36
- daemon = Refinery::Daemon.new(@processor, 'sample', '', {'visibility' => 600})
37
- end
38
- should "have a queue name" do
39
- @waiting_queue.stubs(:receive)
40
- @provider.stubs(:queue).with(
41
- 'prefix_sample_waiting').returns(@waiting_queue)
42
- daemon = Refinery::Daemon.new(@processor, 'sample', 'prefix_')
43
- assert_equal 'prefix_sample', daemon.queue_name
44
- end
45
- context "that is started" do
46
- setup do
47
- @waiting_queue.stubs(:receive)
48
- @daemon = Refinery::Daemon.new(@processor, 'sample')
49
- end
50
- should "have a state of running" do
51
- assert @daemon.running?
52
- end
53
- # context "after calling stop" do
54
- # setup do
55
- # @daemon.stop
56
- # end
57
- # should "not be running" do
58
- # assert !@daemon.running?
59
- # end
60
- # end
61
- end
62
- end
63
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- class EventPublisherTest < Test::Unit::TestCase
4
- context "an event publisher" do
5
- should "raise an error if credentials are not set" do
6
- publishing_settings = {'sample' => {'delay' => 10}}
7
- Refinery::Config.any_instance.stubs(:publishing).returns(publishing_settings)
8
- event_publisher = Refinery::EventPublisher.new
9
- event_publisher.run
10
- end
11
- end
12
- end
@@ -1,25 +0,0 @@
1
- require 'test_helper'
2
-
3
- class HeartbeatTest < Test::Unit::TestCase
4
- context "a heartbeat" do
5
- setup do
6
- setup_default_config
7
-
8
- @server = stub('server')
9
- @server.stubs(:daemons).returns([])
10
-
11
- heartbeat_queue = stub('heartbeat queue')
12
- heartbeat_queue.stubs(:send_message)
13
- provider = stub('queue provider')
14
- provider.stubs(:queue).with('heartbeat').returns(heartbeat_queue)
15
- if defined?(Typica)
16
- Typica::Sqs::QueueService.stubs(:new).returns(provider)
17
- else
18
- RightAws::SqsGen2.stubs(:new).returns(provider)
19
- end
20
- end
21
- should "be initializable" do
22
- Refinery::Heartbeat.new(@server)
23
- end
24
- end
25
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- class LogMe
4
- include Refinery::Loggable
5
- end
6
- class LoggableTest < Test::Unit::TestCase
7
- context "a class with Loggable mixed in" do
8
- should "have a logger" do
9
- assert_not_nil LogMe.new.logger
10
- end
11
- end
12
- end
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ProcessorTest < Test::Unit::TestCase
4
- context "a processor" do
5
- setup do
6
- @server = stub('Server')
7
- @settings = {
8
- 'workers' => {
9
- 'initial' => 1
10
- }
11
- }
12
-
13
- @waiting_queue = stub('Queue(waiting)')
14
- @error_queue = stub('Queue(error)')
15
- @done_queue = stub('Queue(done)')
16
-
17
- provider = stub('QueueProvider')
18
- if defined?(Typica)
19
- Typica::Sqs::QueueService.stubs(:new).returns(provider)
20
- else
21
- RightAws::SqsGen2.stubs(:new).returns(provider)
22
- end
23
- provider.stubs(:queue).with('sample_waiting').returns(@waiting_queue)
24
- provider.stubs(:queue).with('sample_error').returns(@error_queue)
25
- provider.stubs(:queue).with('sample_done').returns(@done_queue)
26
- end
27
- should "initialize" do
28
- assert_nothing_raised do
29
- @waiting_queue.stubs(:receive)
30
- Refinery::Processor.new(@server, 'sample', @settings)
31
- end
32
- end
33
- end
34
- end
@@ -1,13 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SamplePublisher < Refinery::Publisher
4
- end
5
-
6
- class PublisherTest < Test::Unit::TestCase
7
- context "a publisher" do
8
- should "be instantiable" do
9
- waiting_queue = stub('waiting queue')
10
- SamplePublisher.new(waiting_queue)
11
- end
12
- end
13
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
-
3
- class QueueMe
4
- include Refinery::Configurable
5
- include Refinery::Queueable
6
- end
7
-
8
- class QueueableTest < Test::Unit::TestCase
9
- context "a class with the Queuable module" do
10
- should "provide a queue" do
11
- setup_default_config
12
-
13
- queue = stub('queue')
14
- provider = stub('queue provider')
15
- provider.expects(:queue).with('a_queue').returns(queue)
16
- if defined?(Typica)
17
- Typica::Sqs::QueueService.expects(:new).returns(provider)
18
- else
19
- RightAws::SqsGen2.expects(:new).with('aki', 'sak', {:multi_thread => true}).returns(provider)
20
- end
21
-
22
- queueable = QueueMe.new
23
- assert_not_nil queueable.queue('a_queue')
24
- end
25
- end
26
- end
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ServerTest < Test::Unit::TestCase
4
- context "the server class" do
5
- should "provide a logger" do
6
- assert_not_nil Refinery::Server.logger
7
- end
8
- context "logger" do
9
- should "default to WARN level" do
10
- assert_equal Logger::WARN, Refinery::Server.logger.level
11
- end
12
- end
13
- end
14
- context "a server instance" do
15
- setup do
16
- @server = Refinery::Server.new
17
- end
18
- should "have a config" do
19
- assert_not_nil @server.config
20
- end
21
- should "be runnable" do
22
- setup_default_config
23
-
24
- #Refinery::Heartbeat.expects(:new)
25
-
26
- assert_nothing_raised do
27
- thread = Thread.new do
28
- @server.run
29
- end
30
- @server.stop
31
- end
32
- end
33
- end
34
- end