# Problems with Mongrel

**URL:** <https://www.ruby-forum.com/t/problems-with-mongrel/141753>\
**Category:** Rails\
**Created:** [June 30, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753 "2008-06-30T12:09:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/1 "2008-06-30T12:09:52Z")

</div>

Hi community,  
for my application, I decided to use a configuration based on YAML.  
For this purpose I installed a plugin called “app\_config” (I think  
there are at least two plugins called “app\_config”, so I hope that  
plugin doesn’t cause my problem). I wrote a “settings” controller, so  
that non-developers can easily change config values. I think I post  
the code here (It’s only my first approach to see if this approach is  
a good idea, there is still a lot to improve):

class SettingsController \< ApplicationController  
before\_filter :authorise

def index  
redirect\_to :action =\> “list”  
end

def list  
@changeable = YAML::load\_file “#{RAILS\_ROOT}/config/  
change\_settings.yml”  
@settings = Settings.params.to\_a.flatten &  
@changeable.to\_a.flatten  
end

def update  
redirect\_to\_404 unless request.xhr?  
path\_to\_settings\_yml = RAILS\_ROOT + “/config/settings.yml”  
y = YAML::load\_file path\_to\_settings\_yml  
changeable = YAML::load\_file “#{RAILS\_ROOT}/config/  
change\_settings.yml”  
y[“common”][params[:key]] = params[:value]  
File.open(path\_to\_settings\_yml, “w”) { |f| f.write(y.to\_yaml) }  
render :nothing =\> true  
end  
end

The “update” action is called via link\_to\_remote:  
\<%= link\_to\_remote “Update”, {:url =\> { :action =\> “update”, :key =\>  
setting}, :with =\> “‘value=’ + prompt(‘Wert’, ‘’)” , :method =\>  
“put” }, {:id =\> “update\_#{setting}”} %\>

“Settings” is an instance of the “AppConfig” class provided by the  
plugin:  
class AppConfig  
def initialize(file = nil)  
@sections = {}  
@params = {}  
use\_file!(file) if file  
end  
def use\_file!(file)  
begin  
hash = YAML::load(ERB.new(IO.read(file)).result)  
@sections.merge!(hash) {|key, old\_val, new\_val| (old\_val ||  
new\_val).merge new\_val }  
@params.merge!(@sections[‘common’])  
rescue; end  
end  
def use\_section!(section)  
@params.merge!(@sections[section.to\_s]) if @sections.key?  
(section.to\_s)  
end  
def method\_missing(param)  
param = param.to\_s  
if @params.key?(param)  
@params[param]  
else  
raise "Invalid AppConfig Parameter " + param  
end  
end

def params  
@params  
end  
end

This code works - it’s not very good, but it works. But after a value  
is written, when I reload the page, the value don’t change - although  
it’s successfully changed in the settings.yml. I’m using the server  
provided by Rails 2.1 (started via script/server):  
=\> Booting Mongrel (use ‘script/server webrick’ to force WEBrick)  
=\> Rails 2.1.0 application starting on [http://0.0.0.0:3000](http://0.0.0.0:3000)  
=\> Call with -d to detach  
=\> Ctrl-C to shutdown server  
\*\* Starting Mongrel listening at 0.0.0.0:3000  
\*\* Starting Rails with development environment…  
\*\* Rails loaded.  
\*\* Loading any Rails specific GemPlugins  
\*\* Signals ready. TERM =\> stop. USR2 =\> restart. INT =\> stop (no  
restart).  
\*\* Rails signals registered. HUP =\> reload (without restart). It  
might not work well.  
\*\* Mongrel 1.1.4 available at 0.0.0.0:3000  
\*\* Use CTRL-C to stop.

After I restart the server, the changed value appears on the page. It  
seems to me that the server is using some kind of caching mechanism,  
but it is also possible that my browser causes the problem (I tested  
it with Safari and Firefox). I hope anybody can help me to solve this  
problem, it’s a very annoying one.

Thank you very much in advance  
Christoph

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/2 "2008-06-30T12:25:06Z")

</div>

On 30 Jun 2008, at 11:09, Christoph wrote:

> This code works - it’s not very good, but it works. But after a value  
> is written, when I reload the page, the value don’t change - although  
> it’s successfully changed in the settings.yml. I’m using the server  
> provided by Rails 2.1 (started via script/server):

It doesn’t look like the AppConfig stuff will reload its file between  
requests. I bet that you’d see the changes if you restarted the server  
after the update.

Fred

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/3 "2008-06-30T12:36:50Z")

</div>

Yes, you are right. If I restart the server I see the changes. Do you  
have any ideas how to change the code, so AppConfig reloads the file ?

Christoph

On 30 Jun., 12:24, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/4 "2008-06-30T22:17:23Z")

</div>

On Jun 30, 9:02 pm, Christoph [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> There was a file init.rb provided with the plugin:  
> c = AppConfig.new  
> c.use\_file!(“#{RAILS\_ROOT}/config/settings.yml”)  
> c.use\_section!(RAILS\_ENV)  
> ::Settings = c
> 
> You see, this code loads the configuration files. As you said, this  
> seems to be executed only on server startup. Are there any  
> possibilities to execute this on every request ?

You could reload the file after having made changes. Personally I’d  
just have your settings controller modify the settings object so that  
it doesn’t need reloading.

Fred

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/5 "2008-06-30T22:03:17Z")

</div>

There was a file init.rb provided with the plugin:  
c = AppConfig.new  
c.use\_file!("#{RAILS\_ROOT}/config/settings.yml")  
c.use\_section!(RAILS\_ENV)  
::Settings = c

You see, this code loads the configuration files. As you said, this  
seems to be executed only on server startup. Are there any  
possibilities to execute this on every request ?

Christoph

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [June 30, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/problems-with-mongrel/141753/6 "2008-06-30T22:22:43Z")

</div>

Thank you very much, you saved my day 🙂 I don’t know why I didn’t  
think of such a simple solution.

Christoph

On 30 Jun., 22:16, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
