Merb overrides configuration given

6 views
Skip to first unread message

Jeremy Evans

unread,
Dec 4, 2008, 7:26:53 PM12/4/08
to merb
I'm trying to get a Merb very_flat app to work in the root of a Rails
app. It's been a pain and required overriding an internal method, but
I've finally got it somewhat working. I think the problem is that
Merb is overriding the configuration given to Merb::Config inside
Merb.start.

Here's the code I'm using:

require 'rubygems'
require 'merb-core'

use_template_engine :erb

Merb::Config.use { |c|
c[:session_store] = 'cookie'
c[:exception_details] = true
c[:log_level] = :debug
c[:log_stream] = $stdout
c[:reload_classes] = false
c[:reload_templates] = false
c[:use_mutex] = false
c[:fork_for_class_load] = false
}

Merb::Router.prepare do
match('/').to(:controller => 'merb_setup', :action =>'index')
end

class MerbSetup < Merb::Controller
def index
"Hi, I am 'very flat' Merb application. I have everything in one
single file and well suited for dynamic stub pages."
end
end

# Needed to override this or Merb would give an error: http://p.ramaze.net/13770
def Merb.load_paths
Hash.new{|h,k| ["", nil]}
end

Merb.disable(:initfile)
p Merb::Config.instance_variable_get(:@configuration)
begin
Merb.start(%w'-a mongrel')
rescue SystemExit => e
p Merb::Config.instance_variable_get(:@configuration)
e.backtrace.each{|x| puts x}
end


Here's the output I get:

$ ruby merb_setup.rb
{:log_stream=>#<IO:
0x20cc11368>, :adapter=>"runner", :reap_workers_quickly=>true, :reload_classes=>false, :log_auto_flush=>false, :session_store=>"cookie", :bind_fail_fatal=>true, :reload_templates=>false, :name=>"merb", :exception_details=>true, :environment=>"development", :merb_root=>"/
data/code/scaffolding_extensions/
test_site", :use_mutex=>false, :log_level=>:debug, :disabled_components=>
[:initfile], :deferred_actions=>
[], :host=>"0.0.0.0", :fork_for_class_load=>false, :verbose=>false, :port=>"4000", :log_delimiter=>"
~ "}
~ Parent pid: 2790
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000
^C ~ Reaping Workers
{:log_stream=>#<IO:
0x20cc11368>, :adapter=>"mongrel", :reap_workers_quickly=>true, :reload_classes=>true, :log_auto_flush=>false, :bind_fail_fatal=>true, :name=>"merb", :environment=>"development", :merb_root=>"/
data/code/scaffolding_extensions/
test_site", :use_mutex=>true, :log_level=>:info, :disabled_components=>
[:initfile], :deferred_actions=>
[], :host=>"0.0.0.0", :fork_for_class_load=>true, :verbose=>false, :port=>"4000", :log_delimiter=>"
~ "}
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:667:in `exit'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:667:in `exit_gracefully'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:715:in `start_transaction'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:739:in `call'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:739:in `select'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:739:in `start_transaction'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:725:in `loop'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:725:in `start_transaction'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:691:in `loop'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:691:in `start_transaction'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:623:in `run'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
bootloader.rb:99:in `run'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
server.rb:172:in `bootup'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
server.rb:42:in `start'
/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core.rb:
170:in `start'
merb_setup.rb:35


As you can see from the output, the configuration I give is overridden
(e.g. :reload_classes). I tried modifying the configuration outside
of the block and before Merb.start based on a suggestion from wycats
in #merb, but that didn't change anything (which makes sense since you
can see it is correct in the first dump).

Am I doing something wrong? Is this the expected behavior? Is there
anything I can do to fix this?

Jeremy

Michael Klishin

unread,
Dec 4, 2008, 10:56:46 PM12/4/08
to me...@googlegroups.com

On 05.12.2008, at 2:26, Jeremy Evans wrote:

> Merb is overriding the configuration given to Merb::Config inside
> Merb.start.

It's intentional I guess. very flat applications need to be started
with explicitly given init file path (using -I option, that's capital
i and not lowercase L).
Init file is then loaded and config is updated.

How do you run your application?

MK

Jeremy Evans

unread,
Dec 5, 2008, 12:22:07 PM12/5/08
to merb
On Dec 4, 7:56 pm, Michael Klishin <michael.s.klis...@gmail.com>
wrote:
Like the previous message shows, "ruby merb_setup.rb". I disabled the
init file because Merb shouldn't need to load it, I'm doing all of the
controller, model, and configuration setup myself before calling
Merb.start (and if I used the init file, it would be loaded twice).
The reason for this is I deploy with ruby-style, so I can't use the
merb command line tool.

Is it not supported to use Merb without the merb command line tool? I
could probably work around the issue by putting most of the
configuration in a separate file and calling "Merb.start(%w'-I
merb_setup2.rb -a mongrel')" instead, I'll give that a try. That's
uglier than I'd like, but I'll deal with it. I suppose I could also
use the "if __FILE__ == $0" trick to keep everything in one file.

Camping, Ramaze, and Sinatra all work correctly using pretty much the
same thing I'm trying with Merb, FWIW.

Jeremy

Jeremy Evans

unread,
Dec 5, 2008, 1:08:38 PM12/5/08
to merb
On Dec 4, 4:26 pm, Jeremy Evans <jeremyeva...@gmail.com> wrote:
> I'm trying to get a Merb very_flat app to work in the root of a Rails
> app.  It's been a pain and required overriding an internal method, but
> I've finally got it somewhat working.  I think the problem is that
> Merb is overriding the configuration given to Merb::Config inside
> Merb.start.

I think I've found the problem:

/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core.rb
591 def load_config(options = {})
592 Merb::Config.setup(Merb::Config.defaults.merge(options))
593 Merb::BootLoader::Logger.run
594 end

/usr/local/lib/ruby/gems/1.8/gems/merb-core-1.0.3/lib/merb-core/
config.rb
153 def setup(settings = {})
154 config = defaults.merge(settings)
155

The patch should be obvious. Should I fork merb and send in a pull
request, or can one of you devs take care of it?

Jeremy

Jeremy Evans

unread,
Dec 6, 2008, 3:02:49 PM12/6/08
to merb
On Dec 5, 10:08 am, Jeremy Evans <jeremyeva...@gmail.com> wrote:
> On Dec 4, 4:26 pm, Jeremy Evans <jeremyeva...@gmail.com> wrote:
>
> > I'm trying to get a Merb very_flat app to work in the root of a Rails
> > app.  It's been a pain and required overriding an internal method, but
> > I've finally got it somewhat working.  I think the problem is that
> > Merb is overriding the configuration given to Merb::Config inside
> > Merb.start.

Problem fixed: http://github.com/jeremyevans/merb/commit/4cfc946ece4fe1f66ed46006f663be4bdd8c29ae

This works with the following code:

ENV['FORK'] = '0'
require 'rubygems'
$:.unshift('/data/code/merb/merb-core/lib')
require 'merb-core'

use_template_engine :erb

Merb::Config.use { |c|
c[:session_store] = 'cookie'
c[:exception_details] = true
c[:log_level] = :debug
c[:log_stream] = $stdout
c[:reload_classes] = false
c[:reload_templates] = false
c[:use_mutex] = false
c[:fork_for_class_load] = false
#c[:verbose] = true
c[:framework] = {:application=>["", nil], :view=>[nil,
nil], :public=>["p", nil], :model=>["m", nil], :helper=>["h",
nil], :controller=>["c", nil], :mailer=>[nil, nil], :part=>[nil,
nil], :config=>["", nil], :router=>["", nil], :lib=>["l",
nil], :merb_session=>[nil, nil], :log=>[nil, nil], :stylesheet=>[nil,
nil], :javascript=>[nil, nil], :image=>[nil, nil]}
}

Merb::Router.prepare do
match('/').to(:controller => 'merb_setup', :action =>'index')
end

class MerbSetup < Merb::Controller
def index
"Hi, I am 'very flat' Merb application. I have everything in one
single file and well suited for dynamic stub pages."
end
end

Merb.disable(:initfile)
Merb.start(%w'-a mongrel')

Here's the output:

$ ruby merb_setup.rb
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000

I've sent a pull request, so hopefully this can get applied.

Jeremy

jaigouk

unread,
Jan 8, 2009, 7:18:46 PM1/8/09
to merb
I have similar problem.(merb 1.0.7.1) http://gist.github.com/44941


I referenced
http://merb.lighthouseapp.com/projects/7433/tickets/618-rake-dbautomigrate-loads-initrb-twice
http://groups.google.com/group/merb/browse_thread/thread/3016d380bde5d60e/fb2c7ab24ef40c52?lnk=gst&q=load+init+twice#fb2c7ab24ef40c52
But I can not solve this.


$merb-gen flat myapp
$nano config/init.rb
------------------------
...
merb_gems_version = "1.0.7.1"
dm_gems_version = "0.9.9"
dependency "merb-action-args", merb_gems_version
dependency "merb-assets", merb_gems_version
dependency "merb-helpers", merb_gems_version
dependency "merb_datamapper", merb_gems_version

dependency "dm-aggregates", dm_gems_version
dependency "dm-migrations", dm_gems_version
dependency "dm-timestamps", dm_gems_version
dependency "dm-types", dm_gems_version
dependency "dm-validations", dm_gems_version
dependency "do_sqlite3", "0.9.10.1"
dependency "dm-core", dm_gems_version

dependency 'merb-cache', merb_gems_version do
Merb::Cache.setup do
register(:twitter_fragment_store, Merb::Cache::FileStore, :dir =>
Merb.root / 'cache' / 'fragments')
register(:twitter_page_store, Merb::Cache::PageStore
[Merb::Cache::FileStore], :dir => Merb.root / 'public' / 'page_cache')
register(:default, Merb::Cache::AdhocStore
[:twitter_page_store, :twitter_fragment_store])
end
end

...
------------------
The 'merb-cache' causes problem. If I delete the cache block, "rake
db:automigrae" runs well.
I can not use cache on flat app?

Thanks.

-- Jai-Gouk Kim

jaigouk

unread,
Jan 8, 2009, 11:14:32 PM1/8/09
to merb
This snippet is written by Guillaume Maury(giom). He helped me to run
my flat app :)
I better dig more deeply about merb-cache.
-------------------------------------------------------
dependency 'merb-cache', merb_gems_version do
Merb::Cache.setup do
unless defined? CACHE_SETUP
register(:twitter_fragment_store, Merb::Cache::FileStore, :dir
=> Merb.root / 'cache' / 'fragments')
register(:twitter_page_store, Merb::Cache::PageStore
[Merb::Cache::FileStore], :dir => Merb.root / 'public' / 'page_cache')
register(:default, Merb::Cache::AdhocStore
[:twitter_page_store, :twitter_fragment_store])
end
CACHE_SETUP = true
end
end

jaigouk

unread,
Jan 9, 2009, 8:01:11 AM1/9/09
to merb
I solved the problem. Here is the snippet written by Guillaume Maury
(giom).

dependency 'merb-cache', merb_gems_version do
Merb::Cache.setup do
unless defined? CACHE_SETUP
register(:twitter_fragment_store, Merb::Cache::FileStore, :dir
=> Merb.root / 'cache' / 'fragments')
register(:twitter_page_store, Merb::Cache::PageStore
[Merb::Cache::FileStore], :dir => Merb.root / 'public' / 'page_cache')
register(:default, Merb::Cache::AdhocStore
[:twitter_page_store, :twitter_fragment_store])
end
Reply all
Reply to author
Forward
0 new messages