redis-repeater 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
data/bin/redis-repeater
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/redis-repeater/scheduler.rb'
|
2
|
+
require File.dirname(__FILE__) + '/redis-repeater/scheduler_job.rb'
|
3
|
+
require File.dirname(__FILE__) + '/redis-repeater/transfer_scheduler_job.rb'
|
4
|
+
|
5
|
+
require 'yaml'
|
6
|
+
require 'logger'
|
7
|
+
require 'fileutils'
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'redis'
|
11
|
+
require 'eventmachine'
|
12
|
+
|
13
|
+
module RedisRepeater
|
14
|
+
|
15
|
+
DefaultRedisHost = 'localhost'
|
16
|
+
DefaultRedisPort = 6380
|
17
|
+
LogDefaultFilename = File.dirname(__FILE__) + '/../log/redis-repeater.log'
|
18
|
+
|
19
|
+
def self.start(config_dir = File.dirname(__FILE__) + '/../config')
|
20
|
+
|
21
|
+
# Connect to redis
|
22
|
+
config = YAML::load File.open("#{config_dir}/config.yml")
|
23
|
+
redis_from = redis_configure(config, 'origin')
|
24
|
+
redis_to = redis_configure(config, 'destination')
|
25
|
+
|
26
|
+
# Load the queues from the config file
|
27
|
+
queues = YAML::load File.open("#{config_dir}/queues.yml")
|
28
|
+
|
29
|
+
# Logger
|
30
|
+
log_filename = config.has_key?('log') ? config['log'] : LogDefaultFilename
|
31
|
+
log_path = File.dirname(log_filename)
|
32
|
+
FileUtils.mkdir_p(log_path)
|
33
|
+
logger = Logger.new(log_filename)
|
34
|
+
|
35
|
+
# Load the queues into the scheduler
|
36
|
+
scheduler = Scheduler.new(logger)
|
37
|
+
queues.each do |name, timeout|
|
38
|
+
scheduler << TransferSchedulerJob.new(name, timeout, logger, redis_from, redis_to)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Perform forever
|
42
|
+
scheduler.perform # TODO make configurable
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.redis_configure(config, name)
|
47
|
+
host = DefaultRedisHost
|
48
|
+
port = DefaultRedisPort
|
49
|
+
if config.has_key?(name)
|
50
|
+
host = config[name]['host'] if config[name].has_key?('host')
|
51
|
+
port = config[name]['port'] if config[name].has_key?('port')
|
52
|
+
end
|
53
|
+
Redis.new(:host => host, :port => port)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RedisRepeater
|
2
|
+
|
3
|
+
class Scheduler
|
4
|
+
|
5
|
+
def initialize(logger = nil)
|
6
|
+
@logger = logger
|
7
|
+
end
|
8
|
+
|
9
|
+
# When adding, we move through the current queue, and
|
10
|
+
# find the first one that is performing after we want to
|
11
|
+
def <<(job)
|
12
|
+
scheduler = self
|
13
|
+
EventMachine.run {
|
14
|
+
EventMachine::add_timer(job.timeout) { job.perform; scheduler << job }
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
def perform
|
19
|
+
@logger.info 'Running on schedule forever...'
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RedisRepeater
|
2
|
+
|
3
|
+
class SchedulerJob
|
4
|
+
|
5
|
+
def initialize(name, timeout, logger)
|
6
|
+
@name = name
|
7
|
+
@timeout = timeout
|
8
|
+
@logger = logger
|
9
|
+
@next_allowed_performance = Time.now + timeout
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
puts "Performing #{self.name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def adjust_next_allowed_performance
|
17
|
+
@next_allowed_performance = Time.now + timeout
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :name, :timeout, :next_allowed_performance
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module RedisRepeater
|
2
|
+
|
3
|
+
# Move jobs from One Redis queue to the same queue on a different machine
|
4
|
+
|
5
|
+
class TransferSchedulerJob < SchedulerJob
|
6
|
+
|
7
|
+
def initialize(name, timeout, logger, redis_from, redis_to)
|
8
|
+
@redis_from = redis_from
|
9
|
+
@redis_to = redis_to
|
10
|
+
super(name, timeout, logger)
|
11
|
+
end
|
12
|
+
|
13
|
+
def perform
|
14
|
+
count = 0
|
15
|
+
while (item = @redis_from.lpop @name)
|
16
|
+
@redis_to.rpush(@name, item)
|
17
|
+
count += 1
|
18
|
+
end
|
19
|
+
@logger.debug "Transported #{count} items for #{@name}"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-repeater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- John Crepezzi
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-09 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: eventmachine
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Automatically move events from one redis queue to the same queue on another server
|
50
|
+
email: john@crepezzi.com
|
51
|
+
executables:
|
52
|
+
- redis-repeater
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- lib/redis-repeater/scheduler.rb
|
59
|
+
- lib/redis-repeater/scheduler_job.rb
|
60
|
+
- lib/redis-repeater/transfer_scheduler_job.rb
|
61
|
+
- lib/redis-repeater/version.rb
|
62
|
+
- lib/redis-repeater.rb
|
63
|
+
- bin/redis-repeater
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: http://seejohnrun.github.com/redis-repeater/
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project: redis-repeater
|
94
|
+
rubygems_version: 1.3.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Redis Repeater
|
98
|
+
test_files: []
|
99
|
+
|