Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have a perl web application (now CGI, soon mod_perl) that depends on an external search engine I am connecting to. There are three mirrors of the search engine and what I need is something like a semi automatic failover.

So I need some place where to store the IP of the instance the application should use. If I know beforehand about maintenance going on I want to change the setting by hand but I also want the application itself to change the setting after it encounters connection problems.

This last requirement makes it necessary to save it where apache has write access - but where?

the temp-dir comes to mind but that might be cleaned by other processes (like a server restart). If I use other places I need another config variable where to find the config file. Something relative to the application dir wouldn't work with mod_perl. Putting it in the lib dir and "require"-ing it from my application is good for reading but not for writing.

So I am a bit lost with this seemingly easy task. Any ideas?

Replies are listed 'Best First'.
Re: Where to store config info?
by dsheroh (Monsignor) on Oct 16, 2009 at 12:46 UTC
    Personally, I tend to stick config information that needs to be application-writable into a database rather than a plain file.

    In this particular case, though, you don't need to be able to store it anywhere external to the app once it's been moved to mod_perl. mod_perl provides a persistent environment, so you just need to update the address list in memory and the change will stick around for the life of that process. Granted, each new process spawned will need to repeat the "retry, fail, and update address list" for itself when it receives its first request, but a) it's not that big a deal for each process to repeat that if each process then goes on to handle a couple hundred requests using the updated list and b) this provides a mechanism to automatically go back to using the primary/preferred address after the connection recovers/maintenance is complete.

Re: Where to store config info?
by chaos_cat (Scribe) on Oct 16, 2009 at 12:11 UTC
    I apologize in advance if I have missed some nuance of your situation, but it seems to me that you should have one config file with all three IP addresses, in default search order. The application could read them in and try each in sequence until it found one that worked. To manually override the search order, you could set a command line parameter for what IP to try first, for example. It seems to me this would eliminate the need for write access to the file.

    --cc
      The problem is where to put this one config file. The module that needs it is designed to run in lots of different environments, under Windows, Linux and Solaris, Web- and Script-based, so I cannot just hardcode e.g. /etc/myapp or whatever. What I need is some perl mechanism to find a config file without hardcoding the exact location.
        Why not just stick your configuration in /opt/appname/etc/config.file ? You can create an /opt/appname/etc on Windows, right?

        Options:

        • Search in the current directory for appname.conf / appname.ini or the like
        • Search in the directory containing the main script (i.e. dirname($0).'/appname.conf')
        • Place all your executable scripts in somewhere/bin, place the configuration files in somewhere/etc, and place your own modules in somewhere/lib. Use C<$FindBin::Bin> to locate your executable, take its dirname(), replace '/bin' with '/etc/appname.ini'.
        • Require that an environment variable is set up to point to your configuration file. Refuse to work unless it is set.
        • Pass the name of the configuration file as command line argument (does not work in web environment).
        • Search in the "usual" places: /etc/appname.conf or /etc/appname/setup.conf, and $HOME/.appname or $HOME/.appname/setup.conf on Unix-like systems, H:\Documents and Settings\Joe User\Application Data\AppName on Windows. Don't hardcode the path names on Windows, they keep changing from translation to translation and from version to version. Use Win32::GetFolderPath(CSIDL_xxx).

        Note that forward slashes work both on Unix-like systems AND on Windows. Only some stupid Windows command line utilities can't handle file names with forward slashes. Even ancient DOS versions DO support forward slashes in path names at the API level (i.e. INT 0x21), only the applications refused to pass forward slashes to the API.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Where to store config info?
by Anonymous Monk on Oct 22, 2009 at 19:22 UTC
    Thanks for all the input. Lots of ideas to try...