nezu 0.4.17 → 0.5.9
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/Gemfile.lock +8 -6
- data/VERSION +1 -1
- data/lib/nezu.rb +106 -16
- data/lib/nezu/cli.rb +5 -8
- data/lib/nezu/generators.rb +1 -1
- data/lib/nezu/generators/application/templates/Gemfile +2 -1
- data/lib/nezu/generators/application/templates/app/consumers/$app_name.rb.tt +4 -1
- data/lib/nezu/runner.rb +25 -74
- data/lib/nezu/runtime.rb +43 -0
- data/lib/nezu/runtime/consumer.rb +7 -5
- data/lib/nezu/runtime/worker.rb +3 -3
- data/lib/tasks/databases.rake +112 -0
- data/nezu.gemspec +1 -0
- metadata +20 -2
data/Gemfile.lock
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nezu (0.4.
|
4
|
+
nezu (0.4.18)
|
5
5
|
activerecord (~> 3.2.11)
|
6
6
|
activesupport (~> 3.2.11)
|
7
7
|
amqp
|
8
8
|
bunny (>= 0.9.0.pre6)
|
9
9
|
configatron
|
10
10
|
mysql2
|
11
|
+
term-ansicolor
|
11
12
|
|
12
13
|
GEM
|
13
14
|
remote: https://rubygems.org/
|
@@ -33,12 +34,12 @@ GEM
|
|
33
34
|
eventmachine
|
34
35
|
arel (3.0.2)
|
35
36
|
builder (3.0.4)
|
36
|
-
bunny (0.9.0.
|
37
|
+
bunny (0.9.0.pre8)
|
37
38
|
amq-protocol (>= 1.2.0)
|
38
39
|
columnize (0.3.6)
|
39
40
|
configatron (2.10.0)
|
40
41
|
yamler (>= 0.1.0)
|
41
|
-
debugger (1.
|
42
|
+
debugger (1.4.0)
|
42
43
|
columnize (>= 0.3.1)
|
43
44
|
debugger-linecache (~> 1.1.1)
|
44
45
|
debugger-ruby_core_source (~> 1.2.0)
|
@@ -46,7 +47,7 @@ GEM
|
|
46
47
|
debugger-ruby_core_source (>= 1.1.1)
|
47
48
|
debugger-ruby_core_source (1.2.0)
|
48
49
|
diff-lcs (1.2.1)
|
49
|
-
eventmachine (1.0.
|
50
|
+
eventmachine (1.0.3)
|
50
51
|
i18n (0.6.4)
|
51
52
|
json (1.7.7)
|
52
53
|
multi_json (1.6.1)
|
@@ -58,14 +59,15 @@ GEM
|
|
58
59
|
rspec-core (~> 2.13.0)
|
59
60
|
rspec-expectations (~> 2.13.0)
|
60
61
|
rspec-mocks (~> 2.13.0)
|
61
|
-
rspec-core (2.13.
|
62
|
+
rspec-core (2.13.1)
|
62
63
|
rspec-expectations (2.13.0)
|
63
64
|
diff-lcs (>= 1.1.3, < 2.0)
|
64
65
|
rspec-mocks (2.13.0)
|
65
66
|
sdoc (0.3.20)
|
66
67
|
json (>= 1.1.3)
|
67
68
|
rdoc (~> 3.10)
|
68
|
-
|
69
|
+
term-ansicolor (1.1.1)
|
70
|
+
tzinfo (0.3.37)
|
69
71
|
yamler (0.1.0)
|
70
72
|
|
71
73
|
PLATFORMS
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.9
|
data/lib/nezu.rb
CHANGED
@@ -5,41 +5,131 @@ require 'yaml'
|
|
5
5
|
require 'active_support/core_ext'
|
6
6
|
require 'active_record'
|
7
7
|
require 'configatron'
|
8
|
+
require 'term/ansicolor'
|
9
|
+
require 'nezu/runtime'
|
10
|
+
|
8
11
|
module Nezu
|
9
|
-
|
10
|
-
configatron.app_base_dir = File.expand_path(Dir.pwd)
|
12
|
+
mattr_accessor :logger
|
11
13
|
|
12
|
-
|
13
|
-
Env.new(ENV['NEZU_ENV']||'development')
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.root
|
17
|
-
Root::PATH
|
18
|
-
end
|
14
|
+
GEM_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
19
15
|
|
16
|
+
#used by Nezu.env and Nezu.env.developent? etc.
|
20
17
|
class Env < String
|
21
|
-
def method_missing(meth, params=nil)
|
22
|
-
meth.to_s.sub(/\?$/, '')
|
18
|
+
def method_missing(meth, params=nil) #:nodoc:
|
19
|
+
env = meth.to_s.sub(/\?$/, '')
|
20
|
+
super if env == meth.to_s # doesn't end on "?" ? try parents.
|
21
|
+
env == self
|
23
22
|
end
|
24
23
|
|
25
|
-
def respond_to?(meth, params=nil)
|
24
|
+
def respond_to?(meth, params=nil) #:nodoc:
|
26
25
|
!!meth.to_s.match(/\?$/)
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
30
29
|
class Root < String
|
31
|
-
|
30
|
+
APP_PATH = self.new(File.expand_path(Dir.pwd))
|
32
31
|
|
32
|
+
# you can do Nezu.root.join('path', 'to', 'your', 'stuff') and get the
|
33
|
+
# absolute path of your stuff
|
33
34
|
def join(*params)
|
34
|
-
File.join(
|
35
|
+
File.join(APP_PATH, params)
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
# the generic exception class
|
38
40
|
class Error < Exception
|
39
41
|
def new(e,msg)
|
40
|
-
Nezu
|
41
|
-
e.backtrace.each {|bt_line| Nezu
|
42
|
+
Nezu.logger.error(e.to_s)
|
43
|
+
e.backtrace.each {|bt_line| Nezu.logger.error(bt_line)}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# all these nice colorful log entries are created here
|
48
|
+
# if you don`t like them just override its #call method
|
49
|
+
class CustomLogFormatter
|
50
|
+
TIME_FORMAT = "%Y-%m-%d %H:%M:%S."
|
51
|
+
HOST = %x(hostname).chomp
|
52
|
+
APP = File.basename(Dir.pwd)
|
53
|
+
|
54
|
+
String.send(:include, Term::ANSIColor)
|
55
|
+
|
56
|
+
def call(severity, time, progname, msg)
|
57
|
+
@severity = severity
|
58
|
+
@time = time
|
59
|
+
@progname = progname
|
60
|
+
@msg = msg
|
61
|
+
formatted_msg
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def formatted_severity
|
67
|
+
{
|
68
|
+
'DEBUG' => 'DEBUG'.blue,
|
69
|
+
'INFO' => ' INFO'.green,
|
70
|
+
'WARN' => ' WARN'.yellow,
|
71
|
+
'ERROR' => 'ERROR'.intense_red,
|
72
|
+
'FATAL' => 'FATAL'.intense_white.on_red,
|
73
|
+
'ANY' => ' ANY'.black.on_white
|
74
|
+
}[@severity]
|
75
|
+
end
|
76
|
+
|
77
|
+
def formatted_progname
|
78
|
+
end
|
79
|
+
|
80
|
+
def formatted_msg
|
81
|
+
formatted_time = @time.strftime(TIME_FORMAT) << @time.usec.to_s[0..2].rjust(3)
|
82
|
+
if @msg.kind_of?(Exception)
|
83
|
+
"#{formatted_time} #{HOST} #{APP}[#{$$}][#{formatted_severity}] #{@msg.inspect}\n" +
|
84
|
+
@msg.backtrace.map do |bt_line|
|
85
|
+
"#{formatted_time} #{HOST} #{APP}[#{$$}][#{formatted_severity}] #{bt_line}\n"
|
86
|
+
end.join
|
87
|
+
elsif @msg.kind_of?(String)
|
88
|
+
"#{formatted_time} #{HOST} #{APP}[#{$$}][#{formatted_severity}] #{@msg.strip}\n"
|
89
|
+
else
|
90
|
+
"#{formatted_time} #{HOST} #{APP}[#{$$}][#{formatted_severity}] #{@msg.class}: #{@msg.inspect}\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Returns a String like object with the current name of the environment
|
96
|
+
def self.env
|
97
|
+
Env.new(ENV['NEZU_ENV']||'development')
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.app
|
101
|
+
configatron.app_name.classify
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns a String like object with the applications absolute root
|
105
|
+
def self.root
|
106
|
+
Root::APP_PATH
|
107
|
+
end
|
108
|
+
|
109
|
+
# turn errors into warnings if used in verbose mode (Nezu.try(true) { ... })
|
110
|
+
# useful if you find it acceptable that something isn't
|
111
|
+
# working
|
112
|
+
def self.try(verbose=false, &block)
|
113
|
+
yield
|
114
|
+
rescue Exception => e
|
115
|
+
Nezu.logger.warn("[Nezu Runner] Nezu.try failed") if verbose
|
116
|
+
Nezu.logger.warn(e) if verbose
|
117
|
+
end
|
118
|
+
|
119
|
+
# spew log messages
|
120
|
+
# just like any other ruby program
|
121
|
+
# e.g. Nezu.logger.info('foobar')
|
122
|
+
def self.logger
|
123
|
+
if @@logger.nil?
|
124
|
+
log_target = {
|
125
|
+
'development' => STDOUT,
|
126
|
+
'test' => nil,
|
127
|
+
'production' => File.expand_path(File.join('log/', 'nezu.log'))
|
128
|
+
}
|
129
|
+
@@logger = Logger.new(log_target[Nezu.env])
|
130
|
+
@@logger.formatter = Nezu::CustomLogFormatter.new
|
42
131
|
end
|
132
|
+
@@logger
|
43
133
|
end
|
44
134
|
end
|
45
135
|
|
data/lib/nezu/cli.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'nezu'
|
3
2
|
module Nezu
|
4
3
|
module CLI
|
5
4
|
def self.help(*params)
|
@@ -9,6 +8,7 @@ module Nezu
|
|
9
8
|
|
10
9
|
"#{script_name} new <appname>" for an app skeleton dir in <appname>
|
11
10
|
"#{script_name} run" inside an app dir to start it
|
11
|
+
"#{script_name} console" inside an app dir to get an irb shell with your current env loaded
|
12
12
|
|
13
13
|
).gsub(/^\s*/, '')
|
14
14
|
exit(1)
|
@@ -28,22 +28,19 @@ module Nezu
|
|
28
28
|
def self.run(*params)
|
29
29
|
puts %Q(Starting app...)
|
30
30
|
require 'nezu/runner'
|
31
|
-
Nezu::
|
31
|
+
Nezu::Runtime.load_config
|
32
|
+
Nezu::Runner.start
|
32
33
|
exit(0)
|
33
34
|
end
|
34
35
|
|
35
36
|
def self.console(*params)
|
36
37
|
puts %Q(Starting console...)
|
37
|
-
require 'nezu/runner'
|
38
38
|
ARGV.clear
|
39
|
+
Nezu::Runtime.load_config
|
39
40
|
IRB.start()
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
|
-
|
45
45
|
Nezu::CLI.send(ARGV.shift, ARGV)
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
data/lib/nezu/generators.rb
CHANGED
@@ -5,7 +5,7 @@ require 'nezu/generators/application/app_generator'
|
|
5
5
|
module Nezu
|
6
6
|
module Generators
|
7
7
|
|
8
|
-
configatron.template_paths = [File.join(
|
8
|
+
configatron.template_paths = [File.join(Nezu::GEM_DIR, 'lib/nezu/generators/application/templates')] + ENV['NEZU_TEMPLATES'].to_s.split(':')
|
9
9
|
configatron.file_suffixes = %w(tt)
|
10
10
|
|
11
11
|
def template_to(filename) # e.g. "config/amqp.yml"
|
data/lib/nezu/runner.rb
CHANGED
@@ -1,93 +1,44 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
#
|
4
|
-
#
|
5
|
-
require 'amqp'
|
6
|
-
require "bunny"
|
7
|
-
require 'json'
|
8
|
-
require 'nezu/runtime/worker'
|
9
|
-
require 'nezu/runtime/consumer'
|
10
|
-
require 'nezu/runtime/producer'
|
11
|
-
require 'nezu/runtime/recipient'
|
12
|
-
|
13
1
|
$: << File.expand_path('./lib')
|
14
2
|
$: << File.expand_path('./app')
|
15
3
|
$: << File.expand_path('.')
|
16
4
|
|
17
|
-
|
18
|
-
Signal.trap("INT") { connection.close { EventMachine.stop } ; exit}
|
19
|
-
|
20
|
-
module Nezu
|
21
|
-
class CustomLogFormatter
|
22
|
-
SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
|
23
|
-
TIME_FORMAT = "%Y-%m-%d %H:%M:%S."
|
24
|
-
HOST = %x(hostname).chomp
|
25
|
-
APP = File.basename(Dir.pwd)
|
26
|
-
|
27
|
-
def call(severity, time, progname, msg)
|
28
|
-
formatted_severity = sprintf("%-5s","#{severity}")
|
29
|
-
formatted_time = time.strftime(TIME_FORMAT) << time.usec.to_s[0..2].rjust(3)
|
30
|
-
color = SEVERITY_TO_COLOR_MAP[severity]
|
31
|
-
if msg.kind_of?(Exception)
|
32
|
-
"#{formatted_time} #{HOST} #{APP}[#{$$}][\033[#{color}m#{formatted_severity}\033[0m] #{msg.to_s}\n" +
|
33
|
-
msg.backtrace.map do |bt_line|
|
34
|
-
"#{formatted_time} #{HOST} #{APP}[#{$$}][\033[#{color}m#{formatted_severity}\033[0m] #{bt_line.strip}"
|
35
|
-
end.join("\n")
|
36
|
-
else
|
37
|
-
"#{formatted_time} #{HOST} #{APP}[#{$$}][\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip}\n"
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
log_target = {
|
42
|
-
'development' => STDOUT,
|
43
|
-
'test' => nil,
|
44
|
-
'production' => File.expand_path(File.join('log/', 'nezu.log'))
|
45
|
-
}
|
46
|
-
LOGGER = Logger.new(log_target[Nezu.env])
|
47
|
-
LOGGER.formatter = CustomLogFormatter.new
|
48
|
-
|
49
|
-
def self.try(&block)
|
50
|
-
yield
|
51
|
-
rescue Exception => e
|
52
|
-
Nezu::LOGGER.warn("[Nezu Runner] Nezu.try failed")
|
53
|
-
Nezu::LOGGER.warn(e)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
Dir.glob(File.join('config', '*.yml')).each do |yaml_file|
|
58
|
-
yaml = YAML.load_file(yaml_file)[Nezu.env]
|
59
|
-
configatron.configure_from_hash(File.basename(yaml_file.sub(/.yml/, '')) => yaml)
|
60
|
-
end
|
61
|
-
|
62
|
-
if configatron.database.present?
|
63
|
-
ActiveRecord::Base.establish_connection(configatron.database.to_hash)
|
64
|
-
ActiveRecord::Base.logger = Logger.new(File.expand_path(File.join('log/', 'database.log')))
|
65
|
-
end
|
66
|
-
|
67
|
-
|
68
|
-
Nezu::LOGGER.info("[Nezu Runner] initializing...")
|
5
|
+
Signal.trap("INT") { Nezu::Runner.stop}
|
69
6
|
|
70
7
|
module Nezu
|
71
8
|
class Runner
|
72
|
-
|
73
|
-
|
74
|
-
|
9
|
+
# this is the starting point for every application running with "$> nezu run"
|
10
|
+
# it get called from cli.rb
|
11
|
+
def self.start
|
75
12
|
AMQP.start(configatron.amqp.url) do |connection, open_ok|
|
76
|
-
Nezu
|
13
|
+
Nezu.logger.debug("[Nezu Runner] AMQP connection #{configatron.amqp.url}")
|
77
14
|
channel = AMQP::Channel.new(connection, :auto_recovery => true)
|
78
|
-
Nezu
|
15
|
+
Nezu.logger.debug("[Nezu Runner] AMQP channel #{channel}")
|
79
16
|
Nezu::Runtime::Consumer.descendants.each do |consumer|
|
80
|
-
Nezu
|
17
|
+
Nezu.logger.debug("[Nezu Runner] Consumer.descendants: ##{consumer.to_s}")
|
81
18
|
worker = Nezu::Runtime::Worker.new(channel, consumer.new)
|
82
19
|
worker.start
|
83
20
|
end
|
21
|
+
@@connection = connection
|
84
22
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
23
|
+
rescue => e
|
24
|
+
Nezu.logger.fatal("#{self.inspect} died restarting")
|
25
|
+
Nezu.logger.fatal(e)
|
26
|
+
sleep 0.5
|
27
|
+
self.start
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.stop(exit_code=0)
|
31
|
+
Nezu.logger.info("shutting down #{Nezu.app}")
|
32
|
+
Nezu::Runner.connection.close { EventMachine.stop }
|
33
|
+
Nezu.logger.info("done #{Nezu.app}")
|
34
|
+
exit(exit_code)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.connection
|
38
|
+
@@connection
|
88
39
|
end
|
89
40
|
end
|
90
41
|
end
|
91
42
|
|
92
|
-
Nezu
|
43
|
+
Nezu.logger.info("[Nezu Runner] ready")
|
93
44
|
|
data/lib/nezu/runtime.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'amqp'
|
2
|
+
require "bunny"
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Nezu
|
6
|
+
module Runtime
|
7
|
+
# load everything needed to run the app
|
8
|
+
def self.load_config
|
9
|
+
Nezu.try(true) { configure_from_yaml('database.yml') }
|
10
|
+
|
11
|
+
begin
|
12
|
+
configure_from_yaml('amqp.yml')
|
13
|
+
rescue
|
14
|
+
Nezu.logger.fatal("[Nezu Runner] no amqp config please create one in config/amqp.yml") unless configatron.amqp.present?
|
15
|
+
raise
|
16
|
+
end
|
17
|
+
|
18
|
+
if configatron.database.present?
|
19
|
+
ActiveRecord::Base.establish_connection(configatron.database.to_hash)
|
20
|
+
ActiveRecord::Base.logger = Logger.new(File.expand_path(File.join('log/', 'database.log')))
|
21
|
+
end
|
22
|
+
require 'nezu/runtime'
|
23
|
+
(Dir.glob(Nezu.root.join('app', '**', '*.rb')) + Dir.glob(Nezu.root.join('lib', '**', '*.rb'))).each do |file_name|
|
24
|
+
require file_name #Autoload is not thread-safe :(
|
25
|
+
end
|
26
|
+
Nezu.try {require "config/nezu"}
|
27
|
+
Nezu.logger.debug("[Nezu Runner] config loaded")
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def self.configure_from_yaml(yaml_file) #:nodoc:
|
33
|
+
yaml = YAML.load_file(Nezu.root.join('config', yaml_file))[Nezu.env]
|
34
|
+
configatron.configure_from_hash(File.basename(yaml_file.sub(/.yml/, '')) => yaml)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
require 'nezu/runtime/worker'
|
40
|
+
require 'nezu/runtime/consumer'
|
41
|
+
require 'nezu/runtime/producer'
|
42
|
+
require 'nezu/runtime/recipient'
|
43
|
+
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Nezu
|
2
2
|
module Runtime
|
3
3
|
class Consumer
|
4
|
-
def self.inherited(subclass)
|
4
|
+
def self.inherited(subclass) #:nodoc:
|
5
5
|
subclass.class_eval {cattr_accessor :queue_name}
|
6
6
|
subclass.queue_name = ''
|
7
7
|
subclass.queue_name << "#{configatron.amqp.queue_prefix}." unless configatron.amqp.queue_prefix.nil?
|
@@ -14,7 +14,7 @@ module Nezu
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def handle_message(metadata, payload)
|
17
|
-
Nezu
|
17
|
+
Nezu.logger.debug("NEZU Consumer[#{self.class}] payload: #{payload}")
|
18
18
|
params = JSON.parse(payload.to_s)
|
19
19
|
action = params.delete('__action')
|
20
20
|
reply_to = params.delete('__reply_to')
|
@@ -22,12 +22,14 @@ module Nezu
|
|
22
22
|
if reply_to
|
23
23
|
result.reverse_merge!('__action' => "#{action}_result")
|
24
24
|
recipient = Nezu::Runtime::Recipient.new(reply_to)
|
25
|
-
Nezu
|
25
|
+
Nezu.logger.debug("sending result #{result}of #{action} to #{recipient}")
|
26
26
|
recipient.push!(result)
|
27
27
|
end
|
28
|
+
rescue JSON::ParserError => e
|
29
|
+
Nezu.logger.error('Please send only json in the message body')
|
30
|
+
Nezu.logger.debug(e)
|
28
31
|
rescue NoMethodError => e
|
29
|
-
Nezu
|
30
|
-
e.backtrace.each {|bt_line| Nezu::LOGGER.error(bt_line)}
|
32
|
+
Nezu.logger.error(e)
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/lib/nezu/runtime/worker.rb
CHANGED
@@ -3,20 +3,20 @@ module Nezu
|
|
3
3
|
class Worker
|
4
4
|
def initialize(channel, consumer = Consumer.new)
|
5
5
|
@queue_name = consumer.class.queue_name
|
6
|
-
Nezu
|
6
|
+
Nezu.logger.info("queue name: #{@queue_name}")
|
7
7
|
@channel = channel
|
8
8
|
@channel.on_error(&method(:handle_channel_exception))
|
9
9
|
@consumer = consumer
|
10
10
|
end
|
11
11
|
|
12
12
|
def start
|
13
|
-
Nezu
|
13
|
+
Nezu.logger.info("[Nezu Worker] #{@queue_name}")
|
14
14
|
@queue = @channel.queue(@queue_name, :exclusive => false)
|
15
15
|
@queue.subscribe(&@consumer.method(:handle_message))
|
16
16
|
end
|
17
17
|
|
18
18
|
def handle_channel_exception(channel, channel_close)
|
19
|
-
Nezu
|
19
|
+
Nezu.logger.error("Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}")
|
20
20
|
end # handle_channel_exception(channel, channel_close)
|
21
21
|
end
|
22
22
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
#require 'nezu/runner'
|
2
|
+
require 'active_support/core_ext/object/inclusion'
|
3
|
+
require 'active_record'
|
4
|
+
require 'debugger'
|
5
|
+
|
6
|
+
#def load_config
|
7
|
+
#yaml = YAML.load_file(File.join(Nezu.root.join('config', 'database.yml')))
|
8
|
+
#configatron.databases.configure_from_hash(yaml)
|
9
|
+
#end
|
10
|
+
|
11
|
+
def connect_to(env, without_db=false)
|
12
|
+
config = configatron.databases.send(env).to_hash
|
13
|
+
config.delete(:database) if without_db
|
14
|
+
@connection = ActiveRecord::Base.establish_connection(config)
|
15
|
+
end
|
16
|
+
|
17
|
+
db_namespace = namespace :db do
|
18
|
+
task :load_config do
|
19
|
+
yaml = YAML.load_file(File.join(Nezu.root.join('config', 'database.yml')))
|
20
|
+
configatron.databases.configure_from_hash(yaml)
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Create all the local databases defined in config/database.yml'
|
24
|
+
task :create do
|
25
|
+
begin
|
26
|
+
load_config
|
27
|
+
configatron.databases.to_hash.each do |k,v|
|
28
|
+
print "creating #{k}`s db (#{v}) ... "
|
29
|
+
connect_to(k, true)
|
30
|
+
ActiveRecord::Base.connection.create_database(v[:database])
|
31
|
+
puts 'done'
|
32
|
+
end
|
33
|
+
rescue Mysql2::Error => e
|
34
|
+
puts "not done (#{e.to_s})"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
task :migrate => [:load_config] do
|
39
|
+
connect_to(Nezu.env)
|
40
|
+
if ENV['VERSION']
|
41
|
+
db_namespace['migrate:down'].invoke
|
42
|
+
db_namespace['migrate:up'].invoke
|
43
|
+
else
|
44
|
+
ActiveRecord::Migrator.migrate(Nezu.root.join('db', 'migrate')) do |migration|
|
45
|
+
migration.migrate(:up)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
|
51
|
+
task :rollback => [:environment, :load_config] do
|
52
|
+
connect_to(Nezu.env)
|
53
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
54
|
+
ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
|
55
|
+
db_namespace['schema:dump'].invoke
|
56
|
+
end
|
57
|
+
|
58
|
+
namespace :migrate do
|
59
|
+
# desc 'Resets your database using your migrations for the current environment'
|
60
|
+
#task :reset => ['db:drop', 'db:create', 'db:migrate']
|
61
|
+
|
62
|
+
desc 'Runs the "up" for a given migration VERSION.'
|
63
|
+
task :up => [:environment, :load_config] do
|
64
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
65
|
+
raise 'VERSION is required' unless version
|
66
|
+
ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, version)
|
67
|
+
db_namespace['dump'].invoke
|
68
|
+
end
|
69
|
+
|
70
|
+
desc 'Runs the "down" for a given migration VERSION.'
|
71
|
+
task :down => [:environment, :load_config] do
|
72
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
73
|
+
raise 'VERSION is required' unless version
|
74
|
+
ActiveRecord::Migrator.run(:down, ActiveRecord::Migrator.migrations_paths, version)
|
75
|
+
db_namespace['_dump'].invoke
|
76
|
+
end
|
77
|
+
|
78
|
+
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
|
79
|
+
task :redo => [:environment, :load_config] do
|
80
|
+
if ENV['VERSION']
|
81
|
+
db_namespace['migrate:down'].invoke
|
82
|
+
db_namespace['migrate:up'].invoke
|
83
|
+
else
|
84
|
+
db_namespace['rollback'].invoke
|
85
|
+
db_namespace['migrate'].invoke
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
namespace :schema do
|
91
|
+
desc 'Create a db/schema.rb file'
|
92
|
+
task :dump => [:environment, :load_config] do
|
93
|
+
require 'active_record/schema_dumper'
|
94
|
+
connect_to(Nezu.env)
|
95
|
+
filename = ENV['SCHEMA'] || "#{Nezu.root}/db/schema.rb"
|
96
|
+
File.open(filename, "w:utf-8") do |file|
|
97
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
desc 'Create a db from schema.rb file'
|
102
|
+
task :load => [:load_config] do
|
103
|
+
#require 'active_record/schema_dumper'
|
104
|
+
connect_to(Nezu.env)
|
105
|
+
#filename = ENV['SCHEMA'] || "#{Nezu.root}/db/schema.rb"
|
106
|
+
#File.open(filename, "w:utf-8") do |file|
|
107
|
+
#ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
108
|
+
#end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
data/nezu.gemspec
CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.add_dependency 'activerecord', '~>3.2.11'
|
15
15
|
s.add_dependency 'activesupport', '~>3.2.11'
|
16
16
|
s.add_dependency 'configatron'
|
17
|
+
s.add_dependency 'term-ansicolor'
|
17
18
|
s.add_development_dependency 'rake'
|
18
19
|
s.add_development_dependency 'sdoc'
|
19
20
|
s.add_development_dependency 'rspec'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nezu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: amqp
|
@@ -108,6 +108,22 @@ dependencies:
|
|
108
108
|
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: term-ansicolor
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :runtime
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
111
127
|
- !ruby/object:Gem::Dependency
|
112
128
|
name: rake
|
113
129
|
requirement: !ruby/object:Gem::Requirement
|
@@ -210,10 +226,12 @@ files:
|
|
210
226
|
- lib/nezu/generators/application/templates/log/.gitkeep
|
211
227
|
- lib/nezu/generators/application/templates/spec/.gitkeep
|
212
228
|
- lib/nezu/runner.rb
|
229
|
+
- lib/nezu/runtime.rb
|
213
230
|
- lib/nezu/runtime/consumer.rb
|
214
231
|
- lib/nezu/runtime/producer.rb
|
215
232
|
- lib/nezu/runtime/recipient.rb
|
216
233
|
- lib/nezu/runtime/worker.rb
|
234
|
+
- lib/tasks/databases.rake
|
217
235
|
- nezu.gemspec
|
218
236
|
- spec/functional/generators/app_generator_spec.rb
|
219
237
|
- spec/functional/generators_spec.rb
|