Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How do I ensure that File.write finished?

Reply
Thread Tools

How do I ensure that File.write finished?

 
 
Emil Kampp
Guest
Posts: n/a
 
      12-10-2009
Hi there.

I am trying to make a form to update the values in a yml file (my
applications configuration file). This is working kind of fine.
Sometimes it works, sometimes not.

I have confirmed (through logging) that a content for +string+ is always
there. So there is always something trying to be written into the yml
file, but still sometimes it ends up being empty. And an empty
configuration file is no good!

Code:
# server-controller (user generated controller in rails)
def update
# This inserts a new setting into the hash
unless params[:new_setting][:key].blank? and
params[:new_setting][:value].blank?
params[:settings].collect{ |env, settings|
settings[params[:new_setting][:key].to_sym] =
params[:new_setting][:value].to_s }
end

# This writes the hash to the file (See the second snippet for
details)
ServerSystem::update_configuration(params[:settings])

# Redirects to the action that restarts the server to load the new
configurations
redirect_to admin_server_restart_path
end
Code:
# Custom class
def update_configuration(settings)
string = settings.to_yaml
File.new(APP_CONFIG_FILE, "w").write string
end
The problem is that the setup is dodgy, sometimes it works, sometimes it
looks like the server is restarted before the content of +string+ is
written to the configuration file.
How can I ensure that the content has been written before executing next
line of code (the restart in this case)?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      12-10-2009
On Thursday 10 December 2009, Emil Kampp wrote:
> |Hi there.
> |
> |I am trying to make a form to update the values in a yml file (my
> |applications configuration file). This is working kind of fine.
> |Sometimes it works, sometimes not.
> |
> |I have confirmed (through logging) that a content for +string+ is always
> |there. So there is always something trying to be written into the yml
> |file, but still sometimes it ends up being empty. And an empty
> |configuration file is no good!
> |
> |
Code:
> |# server-controller (user generated controller in rails)
> |  def update
> |    # This inserts a new setting into the hash
> |    unless params[:new_setting][:key].blank? and
> |params[:new_setting][:value].blank?
> |      params[:settings].collect{ |env, settings|
> |settings[params[:new_setting][:key].to_sym] =
> |params[:new_setting][:value].to_s }
> |    end
> |
> |    # This writes the hash to the file (See the second snippet for
> |details)
> |    ServerSystem::update_configuration(params[:settings])
> |
> |    # Redirects to the action that restarts the server to load the new
> |configurations
> |    redirect_to admin_server_restart_path
> |  end
> |
> |
> |
Code:
> |# Custom class
> |    def update_configuration(settings)
> |      string = settings.to_yaml
> |      File.new(APP_CONFIG_FILE, "w").write string
> |    end
> |
> |
> |The problem is that the setup is dodgy, sometimes it works, sometimes it
> |looks like the server is restarted before the content of +string+ is
> |written to the configuration file.
> |How can I ensure that the content has been written before executing next
> |line of code (the restart in this case)?
> |


I'm not sure, but maybe the fact you're not closing the file after writing has
something to do with that. Try replacing the last line of update_configuration
with:

File.open(APP_CONFIG_FILE, 'w'){|f| f.write string}

When given a block, File.open closes the file after calling the block, which
should ensure that everything you wrote to file should have actually been
written.

I hope this helps

Stefano

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-10-2009
2009/12/10 Stefano Crocco <(E-Mail Removed)>:
> On Thursday 10 December 2009, Emil Kampp wrote:
>> |Hi there.
>> |
>> |I am trying to make a form to update the values in a yml file (my
>> |applications configuration file). This is working kind of fine.
>> |Sometimes it works, sometimes not.
>> |
>> |I have confirmed (through logging) that a content for +string+ is alway=

s
>> |there. So there is always something trying to be written into the yml
>> |file, but still sometimes it ends up being empty. And an empty
>> |configuration file is no good!
>> |
>> |
Code:
>> |# server-controller (user generated controller in rails)
>> | =A0def update
>> | =A0 =A0# This inserts a new setting into the hash
>> | =A0 =A0unless params[:new_setting][:key].blank? and
>> |params[:new_setting][:value].blank?
>> | =A0 =A0 =A0params[:settings].collect{ |env, settings|
>> |settings[params[:new_setting][:key].to_sym] =3D
>> |params[:new_setting][:value].to_s }
>> | =A0 =A0end
>> |
>> | =A0 =A0# This writes the hash to the file (See the second snippet for
>> |details)
>> | =A0 =A0ServerSystem::update_configuration(params[:settings])
>> |
>> | =A0 =A0# Redirects to the action that restarts the server to load the =
Code:
new
>> |configurations
>> | =A0 =A0redirect_to admin_server_restart_path
>> | =A0end
>> |

>> |
>> |
Code:
>> |# Custom class
>> | =A0 =A0def update_configuration(settings)
>> | =A0 =A0 =A0string =3D settings.to_yaml
>> | =A0 =A0 =A0File.new(APP_CONFIG_FILE, "w").write string
>> | =A0 =A0end
>> |
>> |
>> |The problem is that the setup is dodgy, sometimes it works, sometimes i=

t
>> |looks like the server is restarted before the content of +string+ is
>> |written to the configuration file.
>> |How can I ensure that the content has been written before executing nex=

t
>> |line of code (the restart in this case)?
>> |

>
> I'm not sure, but maybe the fact you're not closing the file after writin=

g has
> something to do with that.


That's definitively the reason.

> Try replacing the last line of update_configuration
> with:
>
> File.open(APP_CONFIG_FILE, 'w'){|f| f.write string}
>
> When given a block, File.open closes the file after calling the block, wh=

ich
> should ensure that everything you wrote to file should have actually been
> written.


[ADV]: see also
http://blog.rubybestpractices.com/po...ks_for_Robust=
ness.html

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      12-10-2009
> File.new(APP_CONFIG_FILE, "w").write string

This file is never closed, so if the process is exited without running
its finalizers for some reason, buffered data could be lost.

-r
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Emil Kampp
Guest
Posts: n/a
 
      12-10-2009
It seems that that did the job. Thank you very much
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How much memory do I need to ensure 2 version of IOS stored? Farouq Cisco 1 09-02-2005 02:46 PM
Duplicate post as last - but with my subscription id's nospam to ensure an answer from MS =?Utf-8?B?Um9iIEx5bmNo?= ASP .Net 9 05-04-2004 01:01 PM
What does EnsureChildControls really Ensure? Sally ASP .Net 1 04-16-2004 06:19 PM



Advertisments