You can use
RAILS_DEFAULT_LOGGER.log "foobar" # or Rails.logger.log "blah"
outside a controller for logging.
You can use
RAILS_DEFAULT_LOGGER.log "foobar" # or Rails.logger.log "blah"
outside a controller for logging.
The last Passenger update brought some good explanation off the problems regarding Passenger and memcache-client (see here).
Smart spawning of Passenger processes creates shared file descriptors. As the connections to memcached are sockets they are shared as well so data on them gets corrupted which is explained very nicely in the Passenger documentation: Example 1: Memcached connection sharing (harmful).
The solution presented there works like a charm. The reestablish_connection_to_memcached line is actually not more than CACHE.reset where CACHE is the reference to the memcache connection.
After that change, spawning methods smart-lv2 and smart will work in connection with environment.rb configured memcache connections.
Edit: As requested in the comments, a little example:
memcache_options = { :c_threshold => 10000, :compression => true, :debug => false, :namespace => 'some_ns', :readonly => false, :urlencode => false } CACHE = MemCache.new memcache_options CACHE.servers = '127.0.0.1:11211' begin PhusionPassenger.on_event(:starting_worker_process) do |forked| if forked # We're in smart spawning mode, so... # Close duplicated memcached connections - they will open themselves CACHE.reset end end # In case you're not running under Passenger (i.e. devmode with mongrel) rescue NameError => error end
In this case, CACHE is the global constant that i use to access my memcache-client.
I guess you’ll need to do the same with the global Rails.cache object, but i’m not sure. Anyway, the above solution works for me.
I recently switch from a mod_proxy / thin setup to Phusion Passenger and my application started to do the funniest things and the production.log was full with errors related to memcached.
It seems, that passengers spawn method “smart” isn’t compatible with memcached. Within seconds on a lightly loaded server the cache gets corrupted big time.
I got better results with a newer memcache client (the ruby gem actually), but for that i need to remove the client from the rails vendor lib. Furthermore, under higher load there still where errors.
Only solution is to set the spawn method to conservative like so
RailsSpawnMethod conservative
Problem seems to be known in the Phusion and Rails teams.
What can cause this snippet to fail:
<% if !@day.user.is_rateable? # Workaround für Darstellungsfehler mit Tabellen ohne Bodies im Safari %> <!-- blah --> <% end %>
This little snippet fell apart today. I don’t know if it is passenger, a newer ruby version, RubyInline. All i know is that i am so totally pissed of this incredible amount of incompatible versions of just 3 modules that i literally feel like puking. What a hell of a day.
By the way, the thing that broke was the one-line comment right after the if.
Ruby on Rails I18n infrastructure did a great job to internationalization in Rails applications. Most things work right out of the box.
Daily Fratze is fully internationalized and i wanted to use ordinal day numbers in the English version. Pity, there is no template for strftime that works that way.
As i already hat monkey patched a “t” method to all date and time related classes, i came up with the following solution:
Parallel to “en.yml” i now have “en.rb” in my locales folder which gets loaded in environment.rb via
config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'app', 'locales', '*.{yml,rb}')]
The en.rb files defines some procs as translation values like so:
{ :'en' => { :date => { :formats => { :dmy_with_long_month => lambda { |date| "%B #{date.day.ordinalize}, %Y" }, } }, :time => { :formats => { :dmy_with_long_month => lambda { |date| "%B #{date.day.ordinalize}, %Y" }, :dmy_with_full_day_and_long_month_and_time => lambda { |date| "%A, %B #{date.day.ordinalize}, %Y at %H:%M" } } } } }
Used with the standard I18n tools you’ll end up with the string representation of the proc object. Useless
So time for monkeypatching like so:
module DateTimeSupport def t(format = nil) type = self.respond_to?(:sec) ? 'time' : 'date' formats = I18n.translate(:"#{type}.formats") format = formats[format.to_sym] if formats && formats[format.to_sym] I18n.localize(self, :format => format.respond_to?(:call) ? format.call(self) : format) end end class Time include DateTimeSupport end class Date include DateTimeSupport end class DateTime include DateTimeSupport end class ActiveSupport::TimeWithZone include DateTimeSupport end
Code resides in a file in config/initializers and gets loaded automatically. It adds a t method to all date and time related classes. The method tries to look up the translation of format just like I18n/Simple does.
If it is proc, it gets called and then passed to I18n, otherwise it the original parameter is used.
That way the t method can use “dmy_with_long_month”, :dmy_with_long_month and any other arbitrary format like “%B %Y” that is not defined in any language file.
Again, the MySQL ruby gem totally annyoed me trying to install it on a fresh Mac OS X 10.5.5 install and MySQL 5.0.67.
This time the following command brought it to life:
sudo env ARCHFLAGS="-arch i386" gem install mysql -- \ --with-mysql-dir=/usr/local/mysql --with-mysql-lib=/usr/local/mysql/lib \ --with-mysql-include=/usr/local/mysql/include
Thanks to a bitter software engineer.
The “x_sendfile” argument on the send_file method in Rails 2.1 is not well thought off as it has an impact in development mode also. I guess most Rails coders won’t have Apache proxying their mongrels in dev mode and so they don’t get to see any images or files but the plain path information.
I’ll guess i stay with the x_send_file solution as described here.
Some days ago, Ruby On Rails 2.1 saw the light of day and as usual, i eagerly updated my Daily Fratze project.
I had some minor problems due to an old version of will_paginate and some major ones with my use of PDF::Writer.
The PDF::Writer library still works very well but the the instructions here (under PDF::Writer (Austin Ziegler) on how the register an “rpdf” template handler don’t apply anymore due to changes in the Rails template system but can easily be fixed:
In environment.rb change
ActionView::Base.register_template_handler 'rpdf', ActionView::PDFRender
to
ActionView::Template.register_template_handler 'rpdf', ActionView::PDFRender
And also change your ActionView::PDFRender class to:
module ActionView # :nodoc: require 'pdf/writer' class PDFRender PAPER = 'A4' include ApplicationHelper include ActionView::Helpers::AssetTagHelper include ActionView::Helpers::TextHelper include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper def initialize(action_view) @action_view = action_view end # Render the PDF def render(template, local_assigns = {}) @action_view.controller.headers["Content-Type"] ||= 'application/pdf' # Retrieve controller variables @action_view.controller.instance_variables.each do |v| instance_variable_set(v, @action_view.controller.instance_variable_get(v)) end pdf = ::PDF::Writer.new( :paper => PAPER ) pdf.compressed = true if RAILS_ENV != 'development' eval template.source, nil, "#{@action_view.base_path}/#{@action_view.first_render}.#{@action_view.finder.pick_template_extension(@action_view.first_render)}" pdf.render end def self.compilable? false end def compilable? self.class.compilable? end end end
And enjoy happy PDF generation
I just discovered the great x_send_file plugin and technique and use it extensivly for daily fratze. I replaced the majority of send_file calls with x_send_file but not all (the in memory thingies i serve cannot be send through apache). This works great in production mode but in development mode, it fails as there is no apache sitting in front the mongrels. Therefore i added the following to my $app_home/config/environments/development.rb:
class ActionController::Base def x_send_file(path, options = {}) send_file(path, options) end end
So all calls to x_send_file in dev mode are delegated to the original send_file.
If anyone can present me a cleaner solution, i.e. with method aliasing, feel free to drop a comment.
In my Daily Fratze project the users should be able to download their faces as a zip file backup.
Until now they have been able to upload zip files. For that, i used rubyzip which worked quite well.
As a starting point i found a nice article on the joy of rubyzip, but this has a major flaw for me. It uses the Zip::ZipFile interface to create its archives. This interfaces takes a filename as parameter and either creates this file if it doesn’t exists or tries to open it as a zip archive.
I doesn’t want my directories polluted by some random zip files so i tried to use TempFile. Creating a new TempFile leads to an existing file which Zip::ZipFile cannot open.
My solution uses the more basic interface Zip::ZipOutputStream. Further requirements were adding binary files with arbitrary names and not like in the examples of rubyzip, creating new files with some textual content. Here we go:
require 'zip/zip' require 'zip/zipfilesystem' t = Tempfile.new("some-weird-temp-file-basename-#{request.remote_ip}") # Give the path of the temp file to the zip outputstream, it won't try to open it as an archive. Zip::ZipOutputStream.open(t.path) do |zos| some_file_list.each do |file| # Create a new entry with some arbitrary name zos.put_next_entry("some-funny-name.jpg") # Add the contents of the file, don't read the stuff linewise if its binary, instead use direct IO zos.print IO.read(file.path) end end # End of the block automatically closes the file. # Send it using the right mime type, with a download window and some nice file name. send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => "some-brilliant-file-name.zip" # The temp file will be deleted some time... t.close
Edit: The basename given to Tempfile.new is what the name says: A basename. It doesn’t need to denote a full path. Tempfile creates an arbitrary path for you in the default temporary directory.