Guard crashing Spork when using guard-spork - ruby

I've followd the "How I Test" screencast at RailsCasts, however I ran into a problem with spork
$ guard
Guard is now watching at '/Users/darth/projects/auth-before'
Starting Spork for Test::Unit & RSpec
Couldn't find a supported test framework that begins with 'testunit'
Supported test frameworks:
( ) Cucumber
(*) RSpec
Legend: ( ) - not detected in project (*) - detected
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
ERROR: Could not start Spork server for Test::Unit & RSpec. Make sure you can use it manually first.
# here I get growl notification "Test::Unit & RSpec NOT started
Guard::RSpec is running, with RSpec 2!
Running all specs
Running tests with args ["--color", "--format", "progress", "--format", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--require", "/Users/darth/.rvm/gems/ruby-1.9.2-p290/gems/guard-rspec-0.5.0/lib/guard/rspec/formatters/notification_rspec.rb", "spec"]...
.
Finished in 14.47 seconds
1 example, 0 failures
Done.
When I try to run spork in a separate terminal window, it doesn't help, as it gets killed instantly once I run guard
$ spork
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Killed: 9
If I just run spork and then try rspec . --drb, it works just fine. Here's link to gist with my Gemfile, Guardfile and spec_helper.rb

You should change:
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' },
:wait => 120
to:
guard 'spork', :rspec_env => { 'RAILS_ENV' => 'test' },
cucumber: false,
test_unit: false

This problem is actually caused by guard killing spork before it can even load, which is a problem on my slower MacBook pro.
The solution is to increase the wait time with :wait => 120 in the Guardfile, e.g.
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' }, :wait => 120
....

Related

dalli on heroku not caching

I want to enable action caching in my rails app on heroku.
In development.rb I set:
config.action_controller.perform_caching = true
and see in logs
Started GET "..." for 127.0.0.1 at 2013-05-17 14:03:25 +0400
...
Write fragment ...
OR
Read fragment ... (0.2ms)
->
To move to production I installed memcache add-on via $heroku addons:add memcache , installed new gem in Gemfile: gem 'dalli' and changed settings in production.rb:
config.action_controller.perform_caching = true
config.cache_store = :dalli_store #, ENV['MEMCACHE_SERVERS'], { :namespace => 'myapp', :expires_in => 1.day, :compress => true }
I have also tried to enable those two commented parameters, but anyway I don't see Read/Write fragment ... pieces in logs, I see that app gets authenticated, but cache is always missing
Started GET "..." for 195.178.108.38 at 2013-05-17 09:54:19 +0000
Dalli/SASL authenticating as myapp%40heroku.com
Dalli/SASL: Authenticated
cache: [GET ...] miss
Running $heroku run console I check that the cache is loading:
irb(main):001:0> Rails.cache.read('color')
Dalli/SASL authenticating as myapp%40heroku.com
Dalli/SASL: Authenticated
=> nil
irb(main):002:0> Rails.cache.write('color', 'red')
=> true
irb(main):003:0> Rails.cache.read('color')
=> "red"
Why action caching does not work?
Can you try using memcachier instead?
remove memcahe add-on
add memcachier add-on
add "memcachier" gem just above "dalli" in your gem file
it should "just work"
See here in the DevCenter: Memcachier

Daemons do not get restarted?

I am trying to run the same script in multiple daemons.
myapp.rb looks like this:
loop do
sleep 5
1 / 0 # crash it
end
my myapp_controller.rb:
require 'rubygems'
require 'daemons'
options = {
:log_output => true,
:backtrace => true,
:monitor => true,
:multiple => true,
:log_dir => '/mnt/log/',
:hard_exit => true
}
Daemons.run(File.join(File.dirname(__FILE__), 'myapp.rb'), options)
When I run ruby myapp_controller.rb start several times in a row, it creates that many daemons, as I expect. But, after a while, due to an error in myapp.rb the daemons crash and the monitor restarts just one and not all. So I end up with a single running daemon.
Why? What am I doing wrong?
I was able to reproduce the behavior. It is not anything you are doing wrong; it is the way the daemons gem behaves.
Going through the code for the daemons gem, turns out the :multiple option doesn't work well with the :monitor option.
The :monitor option works only when the daemon is run in single mode.
I have created a bug report on the daemons project page referencing this question as the source.
More info about the reproduction of the issue:
Multiple daemon processes are created when :multiple => true. Each process has its own pid file in the format of <scriptname>.rb<number>.pid.
However, only one monitor process is created (with a single <scriptname>.rb_monitor.pid file.)
Here are the list of processes started when I start the daemon process 3 times:
$ ps -fe | grep my_server
501 1758 1 0 12:25PM ?? 0:00.63 my_server.rb
501 1759 1 0 12:25PM ?? 0:00.43 my_server.rb_monitor
501 1764 1 0 12:25PM ?? 0:00.54 my_server.rb
501 1834 1 0 12:51PM ?? 0:00.31 my_server.rb
The files in the pid/log folder:
$ ls /tmp/daemons-2013-01-25/
my_server.rb.log my_server.rb1.pid my_server.rb_monitor.pid
my_server.rb0.pid my_server.rb2.pid
Until the issue is resolved, you can change your code to something like this:
#myapp_controller.rb
require 'rubygems'
require 'daemons'
number = ARGV.fetch(1)
options = {
:app_name => "daemon-#{number}" # provide app_name
:log_output => true,
:backtrace => true,
:monitor => true,
:multiple => false, # disable multiple option
:log_dir => '/mnt/log/',
:hard_exit => true
}
Daemons.run(File.join(File.dirname(__FILE__), 'myapp.rb'), options)
And then start your daemons with these commands:
ruby myapp_controller.rb start 1
ruby myapp_controller.rb start 2
...
This slightly changes your startup code, but now you will have a monitor process for each of your daemon processes.

OSX RoR Guard failing to auto run on file updates for certain files

I'm absolutely baffled.. (And quite frustrated too.. )
For some reason guard isn't detecting file updates on osx, but only for certain files. It's almost like the regexp defined in the watch function isn't working or something..
I have created a brand new rails app for fiddling around following Ryans guard railscast --
http://railscasts.com/episodes/264-guard
And have the default guardfile as generated by;
guard init spec
which looks like this;
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'rspec', :cli => "--color --format nested --fail-fast" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}# {m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/# {m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/# {m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
If I edit application_controller.rb or a spec file in the spec dir, guard will autorun just fine.
But if I edit and save any other controller, nothing happens.
I have run guard as both guard and bundle exec guard with no difference; It just runs the full suite once then sits there at a prompt.
macbook pro os v10.6.8.
RVM with Ruby-1.9.3-p286 and Rails 3.2.8
rb-fsevent 0.9.2
One of my coworkers is running the exact same thing directly pulled from git with the same system specs (company issued mbp's), and it works for him.
When he edits app/home_controller.rb and just gives it an extra line or something, hits save.. the tests fire.
When I edit the file and hit save, nothing happens but if I edit app/application_controller.rb it fires..
I'm confused.. I'm frustrated.. I need help as I'm at a loss..
Thoughts?
It looks like a problem with rb-fsevent an OS X 10.6. You can try if an older version of rb-fsevent will work by setting a fixed version in your Gemfile like
gem 'rb-fsevent', '0.9.0'
or you can work around the issue by switching to polling:
bundle exec guard --force-polling

Gem Daemons - How to run several different daemons

Basically I just want to run several daemons in my ruby script :
require 'daemons'
Daemons.run path_1, { :ARGV => ['start'], :app_name => 'app1', :multiple => true, ... }
Daemons.run path_2, { :ARGV => ['start'], :app_name => 'app2', :multiple => true, ... }
But the second Daemons.run is never called when ARGV[0] == 'start' (works perfectly with 'status'/'stop'). What is the right way to do it ?
from http://daemons.rubyforge.org
3- Control a bunch of daemons from another application
Layout: you have an application my_app.rb that wants to run a bunch of server tasks as daemon processes.
# this is my_app.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
task1 = Daemons.call(:multiple => true) do
# first server task
loop {
conn = accept_conn()
serve(conn)
}
end
task2 = Daemons.call do
# second server task
loop {
something_different()
}
end
# the parent process continues to run
# we can even control our tasks, for example stop them
task1.stop
task2.stop
exit
does it fit?

Pass the environment in a custom Airbrake notification

I've written a custom notification to Airbrake from a Ruby EM server we run along with our Rails app. In the case of an exception, I notify Airbrake as per the API docs
Airbrake.notify(
:error_class => "Ruby EM Server Error:",
:error_message => "EM Server Crashed with error: #{$!}",
:backtrace => $#,
) if ENV['RAILS_ENV'] == 'production'
However, when the error comes through as "Unknown: EM Server Crashed with error:"
I know what the RAILS_ENV is when I send off the notification so I was wondering if there was any way I can pass this info on to Airbrake without writing XML and posting it.
I'd like to see the error to come through as "Production: EM Server Crashed with error:"
Try this:
Airbrake.notify(
:error_class => "Ruby EM Server Error:",
:error_message => "EM Server Crashed with error: #{$!}",
:backtrace => $#,
:environment_name => ENV['RAILS_ENV']
) if ENV['RAILS_ENV'] == 'production'
The environment_name option is in the gem's documentation.

Resources