Ruby on Rails + Daemons 6
Getting a Rails script to run in the background shouldn’t be hard, but there’s a few issues with the stock configuration that need to be fixed before anything will work properly.
First, the Daemons GEM will switch your current working directory to be ‘/’ which will mean that whatever RAILS_ROOT you have will likely be incorrect. Usually it’s something like “../config/../” which works only if you’re in the “correct” location.
A quick fix for that can be put into your environment.rb file right up at the top, after RAILSGEMVERSION.
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'), Dir.getwd)Then you need to create a controller script in some directory. scripts/ is probably fine, but you may have a preference for something else.
Here’s an example that launches myscript.rb which resides in the same directory as the controller script.
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
Daemons.run(File.join(File.dirname(__FILE__), 'myscript.rb'),
:dir => "../log",
:dir_mode => :script,
:backtrace => true,
:monitor => true
)Nothing special needs to be done in myscript.rb but you must keep in mind that all output will be lost. To log it, you might do something like this before sending any output to STDOUT:
$stdout.reopen(File.open("#{RAILS_ROOT}/log/agent.log", "w"))
$stderr.reopen(File.open("#{RAILS_ROOT}/log/agent.err", "w"))Rails Migrations with Fixtures 4
When creating a model, in addition to creating the base class, Rails will create a fixture file in YAML format and a very rough migration class. In most cases you will want to import some data at first, for example, to create the “Administrator” user.
Here’s a simple way to automatically load in the fixtures within the migration file:
require 'active_record/fixtures'
class CreateMyModel < ActiveRecord::Migration
def self.up
begin
ActiveRecord::Base.transaction do
create_table :my_models do |t|
t.column :name, :string
# ...
end
Fixtures.create_fixtures("test/fixtures", :my_models)
end
rescue
self.down
raise
end
end
def self.down
drop_table : my_models if (MyModel.table_exists?)
end
endNote that if there’s an error in your YAML file you’ll get a failure that might not make any sense. I spent a good fifteen minutes trying to track down a problem that was related to having an “empty” entry. In this case I had a value that looked like:
test:
# ...
With no data in there, the Fixture instance was being given ‘nil’ instead of some data and threw an exception as a result. I think it’s valid YAML, but not valid enough for Fixture.
Social Networking in Disarray 4
I’ve been making an effort to find better sites in the “Social Networking” space beside Digg and Reddit, but few seem even close to that modest standard of usability and popularity.
So many are derivative to the point of being unflattering clones of some other service, or are just so far off base as to be either impractical or inscrutable. Blink this, Shadow that, they’re all shades of bizarre, or worse, mere outlets for spam links.
Delicious will be integrated with Popyula soon, all a matter of collecting the right data, but there’s few others that seem so urgent to introduce.
