qwirk 0.1.0 → 0.2.0
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/History.md +5 -0
- data/lib/qwirk.rb +51 -10
- data/lib/qwirk/adapter/base/expanding_worker_config.rb +31 -18
- data/lib/qwirk/adapter/base/worker_config.rb +34 -11
- data/lib/qwirk/adapter/in_memory/publisher.rb +1 -1
- data/lib/qwirk/adapter/in_memory/queue.rb +27 -29
- data/lib/qwirk/adapter/in_memory/topic.rb +10 -12
- data/lib/qwirk/adapter/in_memory/worker.rb +9 -0
- data/lib/qwirk/adapter/in_memory/worker_config.rb +4 -0
- data/lib/qwirk/adapter/inline/worker.rb +4 -0
- data/lib/qwirk/adapter/inline/worker_config.rb +4 -0
- data/lib/qwirk/adapter_factory.rb +5 -1
- data/lib/qwirk/base_worker.rb +1 -5
- data/lib/qwirk/engine.rb +2 -6
- data/lib/qwirk/manager.rb +18 -10
- data/lib/qwirk/marshal_strategy/json.rb +1 -1
- data/lib/qwirk/remote_exception.rb +9 -1
- data/lib/qwirk/task.rb +61 -44
- data/lib/qwirk/worker.rb +16 -7
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +1 -1
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/views/layouts/application.html.erb +3 -3
- data/test/dummy/config/application.rb +22 -8
- data/test/dummy/config/database.yml +3 -0
- data/test/dummy/config/environments/development.rb +15 -4
- data/test/dummy/config/environments/production.rb +31 -13
- data/test/dummy/config/environments/test.rb +9 -7
- data/test/dummy/config/initializers/inflections.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +1 -1
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +1 -1
- data/test/dummy/config/routes.rb +2 -56
- data/test/dummy/public/500.html +0 -1
- data/test/dummy/script/rails +1 -1
- data/test/fixtures/qwirk/jobs.yml +11 -0
- data/test/functional/qwirk/jobs_controller_test.rb +9 -0
- data/test/{base_test.rb → models/base_worker_test.rb} +1 -4
- data/test/models/marshal_strategy_test.rb +61 -0
- data/test/{unit → models}/qwirk/batch/acquire_file_strategy_test.rb +0 -0
- data/test/{unit → models}/qwirk/batch/active_record/batch_job_test.rb +0 -0
- data/test/{unit → models}/qwirk/batch/parse_file_strategy_test.rb +0 -0
- data/test/test_helper.rb +7 -4
- data/test/unit/helpers/qwirk/jobs_helper_test.rb +6 -0
- data/test/unit/qwirk/job_test.rb +9 -0
- metadata +179 -117
- data/lib/qwirk/task.rb.sav +0 -194
- data/test/dummy/log/development.log +0 -0
- data/test/dummy/log/production.log +0 -0
- data/test/dummy/log/server.log +0 -0
- data/test/dummy/log/test.log +0 -0
- data/test/dummy/public/javascripts/application.js +0 -2
- data/test/dummy/public/javascripts/controls.js +0 -965
- data/test/dummy/public/javascripts/dragdrop.js +0 -974
- data/test/dummy/public/javascripts/effects.js +0 -1123
- data/test/dummy/public/javascripts/prototype.js +0 -6001
- data/test/dummy/public/javascripts/rails.js +0 -191
- data/test/marshal_strategy_test.rb +0 -62
@@ -16,6 +16,10 @@ module Qwirk
|
|
16
16
|
super.merge(:active => false)
|
17
17
|
end
|
18
18
|
|
19
|
+
def self.in_process?(config)
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
19
23
|
# Hack - Steal attribute from expanding_worker_config so test config can share development config
|
20
24
|
def min_count=(min_count)
|
21
25
|
@active = (min_count > 0)
|
@@ -21,7 +21,7 @@ module Qwirk
|
|
21
21
|
@log_times = config.delete(:log_times)
|
22
22
|
|
23
23
|
key = config.delete(:adapter)
|
24
|
-
raise "No adapter definition" unless key
|
24
|
+
raise "No adapter definition config=#{config.inspect}" unless key
|
25
25
|
key = key.to_sym
|
26
26
|
@publisher_class, @worker_config_class, block = @@adapter_hash[key]
|
27
27
|
raise "No adapter matching #{key}" unless @publisher_class
|
@@ -44,5 +44,9 @@ module Qwirk
|
|
44
44
|
def create_publisher_impl(queue_name, topic_name, options, response_options)
|
45
45
|
@publisher_class.new(self, queue_name, topic_name, options, response_options)
|
46
46
|
end
|
47
|
+
|
48
|
+
def in_process?
|
49
|
+
@worker_config_class.in_process?(@config)
|
50
|
+
end
|
47
51
|
end
|
48
52
|
end
|
data/lib/qwirk/base_worker.rb
CHANGED
@@ -4,7 +4,7 @@ module Qwirk
|
|
4
4
|
# TODO: Is this necessary anymore or just put in worker.rb? Decide when flat file adapter is implemented.
|
5
5
|
module BaseWorker
|
6
6
|
|
7
|
-
attr_accessor :index, :
|
7
|
+
attr_accessor :index, :config
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def default_name
|
@@ -71,10 +71,6 @@ module Qwirk
|
|
71
71
|
raise "Need to override stop method in #{self.class.name}"
|
72
72
|
end
|
73
73
|
|
74
|
-
def join
|
75
|
-
thread.join
|
76
|
-
end
|
77
|
-
|
78
74
|
def status
|
79
75
|
raise "Need to override status method in #{self.class.name}"
|
80
76
|
end
|
data/lib/qwirk/engine.rb
CHANGED
data/lib/qwirk/manager.rb
CHANGED
@@ -9,6 +9,7 @@ module Qwirk
|
|
9
9
|
attr_reader :env, :worker_configs, :name
|
10
10
|
|
11
11
|
bean_attr_accessor :poll_time, :float, 'How often the manager should poll the workers for their status for use by :idle_worker_timeout and :max_read_threshold'
|
12
|
+
bean_attr_accessor :stop_time, :float, 'How long to wait while shutting down for graceful worker stop'
|
12
13
|
|
13
14
|
@@default_options = {}
|
14
15
|
|
@@ -30,7 +31,8 @@ module Qwirk
|
|
30
31
|
options = @@default_options.merge(options)
|
31
32
|
@stopped = false
|
32
33
|
@name = options[:name] || Qwirk::DEFAULT_NAME
|
33
|
-
@poll_time = 3.0
|
34
|
+
@poll_time = options[:poll_time] || 3.0
|
35
|
+
@stop_time = options[:stop_time]
|
34
36
|
@worker_configs = []
|
35
37
|
@env = options[:env]
|
36
38
|
@worker_options = parse_worker_file(options[:worker_file])
|
@@ -51,13 +53,12 @@ module Qwirk
|
|
51
53
|
end
|
52
54
|
|
53
55
|
start_timer_thread
|
54
|
-
|
56
|
+
at_exit { stop }
|
55
57
|
end
|
56
58
|
|
57
59
|
# Create a timer_thread to make periodic calls to the worker_configs in order to do such things as expand/contract
|
58
60
|
# workers, etc.
|
59
61
|
def start_timer_thread
|
60
|
-
# TODO: Optionize hard-coded values
|
61
62
|
@timer_thread = Thread.new do
|
62
63
|
begin
|
63
64
|
while !@stopped
|
@@ -75,17 +76,24 @@ module Qwirk
|
|
75
76
|
def stop
|
76
77
|
return if @stopped
|
77
78
|
@stopped = true
|
79
|
+
Qwirk.logger.info "Stopping manager"
|
78
80
|
@timer_thread.wakeup
|
79
81
|
@worker_configs.each { |worker_config| worker_config.stop }
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
stop
|
82
|
+
if @stop_time
|
83
|
+
end_time = Time.now + @stop_time
|
84
|
+
@worker_configs.each do |worker_config|
|
85
|
+
t = end_time - Time.now
|
86
|
+
t = 0 if t < 0
|
87
|
+
worker_config.join(t)
|
87
88
|
end
|
89
|
+
else
|
90
|
+
@worker_configs.each { |worker_config| worker_config.join }
|
88
91
|
end
|
92
|
+
Qwirk.logger.info "Done stopping manager"
|
93
|
+
end
|
94
|
+
|
95
|
+
def stopped?
|
96
|
+
@stopped
|
89
97
|
end
|
90
98
|
|
91
99
|
# Store off any options that are no longer set to default
|
@@ -30,6 +30,10 @@ module Qwirk
|
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
33
|
+
def marshal
|
34
|
+
to_hash.to_yaml
|
35
|
+
end
|
36
|
+
|
33
37
|
def self.from_hash(hash)
|
34
38
|
exc = new
|
35
39
|
exc.message = hash['message']
|
@@ -38,5 +42,9 @@ module Qwirk
|
|
38
42
|
exc.set_backtrace(hash['backtrace'])
|
39
43
|
return exc
|
40
44
|
end
|
45
|
+
|
46
|
+
def self.unmarshal(yaml_str)
|
47
|
+
from_hash(YAML.load(yaml_str))
|
48
|
+
end
|
41
49
|
end
|
42
|
-
end
|
50
|
+
end
|
data/lib/qwirk/task.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
module Qwirk
|
2
2
|
|
3
3
|
# The following options can be used for configuring the class
|
4
|
+
# :max_pending_records => <integer>
|
5
|
+
# This is how many records can be queued at a time.
|
4
6
|
# :
|
5
7
|
module Task
|
6
8
|
#include Qwirk::BaseWorker
|
7
9
|
include Rumx::Bean
|
8
10
|
|
11
|
+
bean_attr_accessor :max_pending_records, :integer, 'The max number of records that can be published without having been responded to (publishing blocks at this point).'
|
9
12
|
bean_attr_reader :task_id, :string, 'The ID for this task'
|
10
|
-
bean_attr_reader :publish_count, :integer, 'The number of requests that have been published'
|
11
13
|
bean_attr_reader :success_count, :integer, 'The number of successful responses'
|
12
14
|
bean_attr_reader :exception_count, :integer, 'The number of exception responses'
|
13
15
|
bean_attr_reader :total_count, :integer, 'The total expected records to be published (optional)'
|
@@ -25,21 +27,22 @@ module Qwirk
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def initialize(publisher, task_id, total_count, opts={})
|
28
|
-
@publisher
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@
|
32
|
-
@
|
33
|
-
@
|
34
|
-
@
|
35
|
-
@
|
36
|
-
@
|
37
|
-
@
|
38
|
-
@
|
39
|
-
@
|
40
|
-
@
|
41
|
-
|
42
|
-
|
30
|
+
@publisher = publisher
|
31
|
+
@pending_hash = Hash.new
|
32
|
+
@pending_hash_mutex = Mutex.new
|
33
|
+
@pending_hash_condition = ConditionVariable.new
|
34
|
+
@task_id = task_id
|
35
|
+
@stopped = false
|
36
|
+
@finished_publishing = false
|
37
|
+
@max_pending_records = opts[:max_pending_records] || 100
|
38
|
+
@retry = opts[:retry]
|
39
|
+
@auto_retry = opts[:auto_retry]
|
40
|
+
@success_count = 0
|
41
|
+
@exception_count = 0
|
42
|
+
@total_count = total_count
|
43
|
+
@exceptions_per_run = []
|
44
|
+
|
45
|
+
@producer, @consumer = publisher.create_producer_consumer_pair(self)
|
43
46
|
@reply_thread = Thread.new do
|
44
47
|
java.lang.Thread.current_thread.name = "Qwirk task: #{task_id}"
|
45
48
|
reply_event_loop
|
@@ -48,10 +51,10 @@ module Qwirk
|
|
48
51
|
end
|
49
52
|
|
50
53
|
# Stuff to override
|
51
|
-
def on_response(response)
|
54
|
+
def on_response(request, response)
|
52
55
|
end
|
53
56
|
|
54
|
-
def on_exception(exception)
|
57
|
+
def on_exception(request, exception)
|
55
58
|
end
|
56
59
|
|
57
60
|
def on_update()
|
@@ -63,23 +66,19 @@ module Qwirk
|
|
63
66
|
def retry=(val)
|
64
67
|
@retry = val
|
65
68
|
if val
|
66
|
-
@
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def auto_retry=(val)
|
71
|
-
@auto_retry = val
|
72
|
-
if val
|
73
|
-
@mutex.synchronize { check_retry }
|
69
|
+
@pending_hash_mutex.synchronize { check_retry }
|
74
70
|
end
|
75
71
|
end
|
76
72
|
|
77
73
|
def publish(object)
|
78
74
|
marshaled_object = @publisher.marshaler.marshal(object)
|
79
|
-
@
|
75
|
+
@pending_hash_mutex.synchronize do
|
76
|
+
while !@stopped && @pending_hash.size >= @max_pending_records
|
77
|
+
@pending_hash_condition.wait(@pending_hash_mutex)
|
78
|
+
end
|
80
79
|
unless @stopped
|
81
|
-
@producer.send(marshaled_object)
|
82
|
-
@
|
80
|
+
message_id = @producer.send(marshaled_object)
|
81
|
+
@pending_hash[message_id] = object
|
83
82
|
end
|
84
83
|
end
|
85
84
|
end
|
@@ -89,14 +88,13 @@ module Qwirk
|
|
89
88
|
end
|
90
89
|
|
91
90
|
def stop
|
92
|
-
@
|
91
|
+
@pending_hash_mutex.synchronize { do_stop }
|
93
92
|
@reply_thread.join
|
94
93
|
end
|
95
94
|
|
96
95
|
def finished_publishing
|
97
|
-
@total_count = @publish_count
|
98
96
|
@finished_publishing = true
|
99
|
-
@
|
97
|
+
@pending_hash_mutex.synchronize { check_finish }
|
100
98
|
@reply_thread.join
|
101
99
|
end
|
102
100
|
|
@@ -104,6 +102,18 @@ module Qwirk
|
|
104
102
|
private
|
105
103
|
#######
|
106
104
|
|
105
|
+
def verify_fail_queue_creation
|
106
|
+
unless @fail_producer
|
107
|
+
@fail_producer, @fail_consumer = publisher.create_producer_fail_consumer_pair(@task_id)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def publish_fail_request(object)
|
112
|
+
verify_fail_queue_creation
|
113
|
+
marshaled_object = @publisher.marshaler.marshal(object)
|
114
|
+
@fail_producer.send(marshaled_object)
|
115
|
+
end
|
116
|
+
|
107
117
|
# Must be called within a mutex synchronize
|
108
118
|
def do_stop
|
109
119
|
return if @stopped
|
@@ -113,19 +123,26 @@ module Qwirk
|
|
113
123
|
end
|
114
124
|
|
115
125
|
def reply_event_loop
|
116
|
-
while !@stopped &&
|
117
|
-
|
126
|
+
while !@stopped && pair = @consumer.receive
|
127
|
+
message_id, response = pair
|
128
|
+
@pending_hash_mutex.synchronize do
|
118
129
|
unless @stopped
|
119
|
-
|
120
|
-
|
121
|
-
|
130
|
+
request = @pending_hash.delete(message_id)
|
131
|
+
if request
|
132
|
+
if response.kind_of?(RemoteException)
|
133
|
+
publish_fail_request(request)
|
134
|
+
on_exception(request, response)
|
135
|
+
@exception_count += 1
|
136
|
+
else
|
137
|
+
on_response(request, response)
|
138
|
+
@success_count += 1
|
139
|
+
end
|
122
140
|
else
|
123
|
-
|
124
|
-
on_response(response)
|
141
|
+
Qwirk.logger.warn("#{self}: Read unexpected response with message_id=#{message_id}")
|
125
142
|
end
|
126
143
|
@consumer.acknowledge_message
|
127
144
|
check_finish
|
128
|
-
@
|
145
|
+
@pending_hash_condition.signal
|
129
146
|
end
|
130
147
|
end
|
131
148
|
end
|
@@ -138,8 +155,8 @@ module Qwirk
|
|
138
155
|
|
139
156
|
# Must be called within a mutex synchronize
|
140
157
|
def check_finish
|
141
|
-
if @finished_publishing
|
142
|
-
if @
|
158
|
+
if @finished_publishing && @pending_hash.empty?
|
159
|
+
if @exception_count == 0
|
143
160
|
do_stop
|
144
161
|
else
|
145
162
|
check_retry
|
@@ -149,7 +166,7 @@ module Qwirk
|
|
149
166
|
|
150
167
|
# Must be called within a mutex synchronize
|
151
168
|
def check_retry
|
152
|
-
if @finished_publishing && @
|
169
|
+
if @finished_publishing && @pending_hash.empty? && @exception_count > 0 && (@retry || @auto_retry)
|
153
170
|
# If we're just doing auto_retry but nothing succeeded last time, then don't run again
|
154
171
|
return if !@retry && @auto_retry && @exception_count == @exceptions_per_run.last
|
155
172
|
Qwirk.logger.info "#{self}: Retrying exception records, exception count = #{@exception_count}"
|
@@ -165,7 +182,7 @@ module Qwirk
|
|
165
182
|
@fail_consumer.acknowledge_message
|
166
183
|
end
|
167
184
|
@finished_publishing = true
|
168
|
-
@
|
185
|
+
@pending_hash_mutex.synchronize { check_finish }
|
169
186
|
rescue Exception => e
|
170
187
|
do_stop
|
171
188
|
Qwirk.logger.error "#{self}: Exception, thread terminating: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
|
data/lib/qwirk/worker.rb
CHANGED
@@ -37,7 +37,7 @@ module Qwirk
|
|
37
37
|
include Qwirk::BaseWorker
|
38
38
|
|
39
39
|
attr_accessor :message
|
40
|
-
attr_reader :status, :impl, :start_worker_time, :start_read_time, :start_processing_time
|
40
|
+
attr_reader :thread, :status, :impl, :start_worker_time, :start_read_time, :start_processing_time
|
41
41
|
|
42
42
|
module ClassMethods
|
43
43
|
def queue(name, opts={})
|
@@ -122,7 +122,7 @@ module Qwirk
|
|
122
122
|
end
|
123
123
|
|
124
124
|
def start
|
125
|
-
|
125
|
+
@thread = Thread.new do
|
126
126
|
java.lang.Thread.current_thread.name = "Qwirk worker: #{self}" if RUBY_PLATFORM == 'jruby'
|
127
127
|
#Qwirk.logger.debug "#{worker}: Started thread with priority #{Thread.current.priority}"
|
128
128
|
event_loop
|
@@ -136,14 +136,25 @@ module Qwirk
|
|
136
136
|
# From an InMemory perspective, we don't want the workers stopping until all messages in the queue have been processed.
|
137
137
|
# Therefore we want to stop the
|
138
138
|
def stop
|
139
|
-
Qwirk.logger.debug "#{self}: In
|
139
|
+
Qwirk.logger.debug "#{self}: In worker stop"
|
140
140
|
@status = 'Stopping'
|
141
141
|
@stopped = true
|
142
142
|
@processing_mutex.synchronize do
|
143
143
|
# This should interrupt @impl.receive_message above and cause it to return nil
|
144
144
|
@impl.stop
|
145
145
|
end
|
146
|
-
|
146
|
+
end
|
147
|
+
|
148
|
+
def join(timeout=nil)
|
149
|
+
time_str = timeout ? "#{timeout} seconds" : 'indefinitely'
|
150
|
+
Qwirk.logger.info "#{self}: Waiting #{time_str} for worker to stop"
|
151
|
+
# See http://jira.codehaus.org/browse/JRUBY-6896
|
152
|
+
if timeout && !self.thread.join(timeout)
|
153
|
+
Qwirk.logger.warn "#{self}: WARNING - worker stop commanded but thread did not exit gracefully in #{timeout} seconds"
|
154
|
+
else
|
155
|
+
self.thread.join
|
156
|
+
end
|
157
|
+
Qwirk.logger.info "#{self}: worker stop complete"
|
147
158
|
end
|
148
159
|
|
149
160
|
def perform(object)
|
@@ -163,7 +174,7 @@ module Qwirk
|
|
163
174
|
def event_loop
|
164
175
|
Qwirk.logger.debug "#{self}: Starting receive loop"
|
165
176
|
@start_worker_time = Time.now
|
166
|
-
|
177
|
+
until @stopped || (config.stopped? && @impl.ready_to_stop?)
|
167
178
|
Qwirk.logger.debug "#{self}: Waiting for read"
|
168
179
|
@start_read_time = Time.now
|
169
180
|
msg = @impl.receive_message
|
@@ -186,8 +197,6 @@ module Qwirk
|
|
186
197
|
Qwirk.logger.error "#{self}: Exception, thread terminating: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
|
187
198
|
ensure
|
188
199
|
@status = 'Stopped'
|
189
|
-
# TODO: necessary?
|
190
|
-
@impl.stop
|
191
200
|
Qwirk.logger.flush if Qwirk.logger.respond_to?(:flush)
|
192
201
|
config.worker_stopped(self)
|
193
202
|
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
== Welcome to Rails
|
2
|
+
|
3
|
+
Rails is a web-application framework that includes everything needed to create
|
4
|
+
database-backed web applications according to the Model-View-Control pattern.
|
5
|
+
|
6
|
+
This pattern splits the view (also called the presentation) into "dumb"
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
12
|
+
and directing data to the view.
|
13
|
+
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
17
|
+
methods. You can read more about Active Record in
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
19
|
+
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
25
|
+
Rails. You can read more about Action Pack in
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
27
|
+
|
28
|
+
|
29
|
+
== Getting Started
|
30
|
+
|
31
|
+
1. At the command prompt, create a new Rails application:
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
33
|
+
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
36
|
+
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
39
|
+
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
41
|
+
the following resources handy:
|
42
|
+
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
45
|
+
|
46
|
+
|
47
|
+
== Debugging Rails
|
48
|
+
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
50
|
+
will help you debug it and get it back on the rails.
|
51
|
+
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
56
|
+
|
57
|
+
You can also log your own messages directly into the log file from your code
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
59
|
+
|
60
|
+
class WeblogController < ActionController::Base
|
61
|
+
def destroy
|
62
|
+
@weblog = Weblog.find(params[:id])
|
63
|
+
@weblog.destroy
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
The result will be a message in your log file along the lines of:
|
69
|
+
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
71
|
+
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
73
|
+
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
75
|
+
several books available online as well:
|
76
|
+
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
79
|
+
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
81
|
+
programming in general.
|
82
|
+
|
83
|
+
|
84
|
+
== Debugger
|
85
|
+
|
86
|
+
Debugger support is available through the debugger command when you start your
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
89
|
+
resume execution! You need to install ruby-debug to run the server in debugging
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
91
|
+
|
92
|
+
class WeblogController < ActionController::Base
|
93
|
+
def index
|
94
|
+
@posts = Post.all
|
95
|
+
debugger
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
101
|
+
|
102
|
+
>> @posts.inspect
|
103
|
+
=> "[#<Post:0x14a6be8
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
105
|
+
#<Post:0x14a6620
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
108
|
+
=> "hello from a debugger"
|
109
|
+
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
111
|
+
|
112
|
+
>> f = @posts.first
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
114
|
+
>> f.
|
115
|
+
Display all 152 possibilities? (y or n)
|
116
|
+
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
118
|
+
|
119
|
+
|
120
|
+
== Console
|
121
|
+
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
126
|
+
without arguments will launch it in the development environment.
|
127
|
+
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
129
|
+
directory.
|
130
|
+
|
131
|
+
Options:
|
132
|
+
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
134
|
+
made to the database.
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
137
|
+
|
138
|
+
To reload your controllers and models after launching the console run
|
139
|
+
<tt>reload!</tt>
|
140
|
+
|
141
|
+
More information about irb can be found at:
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
143
|
+
|
144
|
+
|
145
|
+
== dbconsole
|
146
|
+
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
152
|
+
PostgreSQL and SQLite 3.
|
153
|
+
|
154
|
+
== Description of Contents
|
155
|
+
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
157
|
+
|
158
|
+
|-- app
|
159
|
+
| |-- assets
|
160
|
+
| |-- images
|
161
|
+
| |-- javascripts
|
162
|
+
| `-- stylesheets
|
163
|
+
| |-- controllers
|
164
|
+
| |-- helpers
|
165
|
+
| |-- mailers
|
166
|
+
| |-- models
|
167
|
+
| `-- views
|
168
|
+
| `-- layouts
|
169
|
+
|-- config
|
170
|
+
| |-- environments
|
171
|
+
| |-- initializers
|
172
|
+
| `-- locales
|
173
|
+
|-- db
|
174
|
+
|-- doc
|
175
|
+
|-- lib
|
176
|
+
| `-- tasks
|
177
|
+
|-- log
|
178
|
+
|-- public
|
179
|
+
|-- script
|
180
|
+
|-- test
|
181
|
+
| |-- fixtures
|
182
|
+
| |-- functional
|
183
|
+
| |-- integration
|
184
|
+
| |-- performance
|
185
|
+
| `-- unit
|
186
|
+
|-- tmp
|
187
|
+
| |-- cache
|
188
|
+
| |-- pids
|
189
|
+
| |-- sessions
|
190
|
+
| `-- sockets
|
191
|
+
`-- vendor
|
192
|
+
|-- assets
|
193
|
+
`-- stylesheets
|
194
|
+
`-- plugins
|
195
|
+
|
196
|
+
app
|
197
|
+
Holds all the code that's specific to this particular application.
|
198
|
+
|
199
|
+
app/assets
|
200
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
201
|
+
|
202
|
+
app/controllers
|
203
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
204
|
+
automated URL mapping. All controllers should descend from
|
205
|
+
ApplicationController which itself descends from ActionController::Base.
|
206
|
+
|
207
|
+
app/models
|
208
|
+
Holds models that should be named like post.rb. Models descend from
|
209
|
+
ActiveRecord::Base by default.
|
210
|
+
|
211
|
+
app/views
|
212
|
+
Holds the template files for the view that should be named like
|
213
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
214
|
+
eRuby syntax by default.
|
215
|
+
|
216
|
+
app/views/layouts
|
217
|
+
Holds the template files for layouts to be used with views. This models the
|
218
|
+
common header/footer method of wrapping views. In your views, define a layout
|
219
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
220
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
221
|
+
layout.
|
222
|
+
|
223
|
+
app/helpers
|
224
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
225
|
+
generated for you automatically when using generators for controllers.
|
226
|
+
Helpers can be used to wrap functionality for your views into methods.
|
227
|
+
|
228
|
+
config
|
229
|
+
Configuration files for the Rails environment, the routing map, the database,
|
230
|
+
and other dependencies.
|
231
|
+
|
232
|
+
db
|
233
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
234
|
+
sequence of Migrations for your schema.
|
235
|
+
|
236
|
+
doc
|
237
|
+
This directory is where your application documentation will be stored when
|
238
|
+
generated using <tt>rake doc:app</tt>
|
239
|
+
|
240
|
+
lib
|
241
|
+
Application specific libraries. Basically, any kind of custom code that
|
242
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
243
|
+
the load path.
|
244
|
+
|
245
|
+
public
|
246
|
+
The directory available for the web server. Also contains the dispatchers and the
|
247
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
248
|
+
server.
|
249
|
+
|
250
|
+
script
|
251
|
+
Helper scripts for automation and generation.
|
252
|
+
|
253
|
+
test
|
254
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
255
|
+
command, template test files will be generated for you and placed in this
|
256
|
+
directory.
|
257
|
+
|
258
|
+
vendor
|
259
|
+
External libraries that the application depends on. Also includes the plugins
|
260
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
261
|
+
vendor/rails/. This directory is in the load path.
|