Quantcast

Loading application variables

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Loading application variables

J. Pablo Fernández
Hello,
I have written an Erlang application called MiSelfu (well, miselfu
actually). I can start it from the command line with:

erl -sname miselfu -boot miselfu
-config /usr/local/etc/miselfu/miselfu.config

but I'd like to be able to start it from a already-running emulator,
that's so I can run it from within my shell running within Emacs.
I can try:

> application:start(miselfu).

=INFO REPORT==== 11-Jun-2006::12:04:35 ===
    application: miselfu
    exited: {shutdown,{miselfu_app,start,[normal,[]]}}
    type: temporary
{error,{shutdown,{miselfu_app,start,[normal,[]]}}}
>

but as you can see, I get a non-very descriptive error (at least, for my
eyes). I only can think of the missing config file as the problem. How
do I load that config file[1] to be able to run my application ?
Am I doing something wrong ?
As a newbie to Erlang and not knowing anyone else using it (I have never
seen anyone else codding on Erlang), I am not sure about the best way to
go thru the development cicle.
Thank you.
--
Pupeno <[hidden email]> http://pupeno.com

[1] The config file contais:

%% Configuration file for MiSelfu.

[{miselfu,
  [
   %% Ports for the different processes.
   {echoTCPPort, 7},
   {echoUDPPort, 7},
   {chargenTCPPort, 19},
   {chargenUDPPort, 19},
   {daytimeTCPPort, 13},
   {daytimeUDPPort, 13},
   {timeTCPPort, 37},
   {timeUDPPort, 37},

   %% Offset, it will be added to all the ports.
   {portOffset, 0}]}].

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Ulf Wiger-2

I don't know if there's a standard function for loading the .config file,  
but you can read and parse a .config file with  
file:consult("miselfu.config"), and you can set environment variables with  
application:set_env(AppName, Key, Value).

BR,
Ulf Wiger


Den 2006-06-11 12:30:24 skrev Pupeno <[hidden email]>:

> Hello,
> I have written an Erlang application called MiSelfu (well, miselfu
> actually). I can start it from the command line with:
>
> erl -sname miselfu -boot miselfu
> -config /usr/local/etc/miselfu/miselfu.config
>
> but I'd like to be able to start it from a already-running emulator,
> that's so I can run it from within my shell running within Emacs.
> I can try:
>
>> application:start(miselfu).
>
> =INFO REPORT==== 11-Jun-2006::12:04:35 ===
>     application: miselfu
>     exited: {shutdown,{miselfu_app,start,[normal,[]]}}
>     type: temporary
> {error,{shutdown,{miselfu_app,start,[normal,[]]}}}
>>
>
> but as you can see, I get a non-very descriptive error (at least, for my
> eyes). I only can think of the missing config file as the problem. How
> do I load that config file[1] to be able to run my application ?
> Am I doing something wrong ?
> As a newbie to Erlang and not knowing anyone else using it (I have never
> seen anyone else codding on Erlang), I am not sure about the best way to
> go thru the development cicle.
> Thank you.



--
Ulf Wiger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

J. Pablo Fernández
On Sun, 2006-06-11 at 14:02 +0200, Ulf Wiger wrote:
> I don't know if there's a standard function for loading the .config file,  
> but you can read and parse a .config file with  
> file:consult("miselfu.config"), and you can set environment variables with  
> application:set_env(AppName, Key, Value).

So, you don't normally work that way ? Opening up a shell on Emacs,
running the application and re-loading code as it is modified ?

Thanks.
--
Pupeno <[hidden email]> http://pupeno.com

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Ulf Wiger-2
Den 2006-06-11 14:06:50 skrev Pupeno <[hidden email]>:

> On Sun, 2006-06-11 at 14:02 +0200, Ulf Wiger wrote:
>> I don't know if there's a standard function for loading the .config  
>> file,
>> but you can read and parse a .config file with
>> file:consult("miselfu.config"), and you can set environment variables  
>> with
>> application:set_env(AppName, Key, Value).
>
> So, you don't normally work that way ? Opening up a shell on Emacs,
> running the application and re-loading code as it is modified ?
>
> Thanks.

Well, you can also set environment variables via the command line, e.g.

erl -miselfu echoTCPPort 7 ...

but in order for them to actually take, you also need to load the miselfu  
application, using application:load(miselfu). If your variables are  
defined in a .config file, I thought it would be easier to write a small  
erlang function that reads it and sets them appropriately. It can be done  
in less than 10 lines of code.

I personally try to use builder in situations like that. It builds a bash  
script that loads all given applications, but doesn't start them (except  
the mandatory kernel and stdlib), essentially so that one could fire up an  
erlang shell, with the code path and all the environment variables set,  
and then do initialization or debugging from the shell.

BR,
Ulf W

--
Ulf Wiger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Sean Hinde
In reply to this post by J. Pablo Fernández

On 11 Jun 2006, at 13:06, Pupeno wrote:

> On Sun, 2006-06-11 at 14:02 +0200, Ulf Wiger wrote:
>> I don't know if there's a standard function for loading  
>> the .config file,
>> but you can read and parse a .config file with
>> file:consult("miselfu.config"), and you can set environment  
>> variables with
>> application:set_env(AppName, Key, Value).
>
> So, you don't normally work that way ? Opening up a shell on Emacs,
> running the application and re-loading code as it is modified ?

I would think that most more serious erlang applications use the  
sys.config file for a few parameters that are only required at  
startup and not subject to change. Parameters that should change at  
runtime would typically be stored in a database or opened up as APIs  
so they can be changed without restarting the application.

Of course your application might be such that it can be restarted  
after every config change, in which case sys.config might be sufficient.

Sean


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

J. Pablo Fernández
In reply to this post by Ulf Wiger-2
On Sun, 2006-06-11 at 14:22 +0200, Ulf Wiger wrote:
> Den 2006-06-11 14:06:50 skrev Pupeno <[hidden email]>:
> Well, you can also set environment variables via the command line, e.g.
>
> erl -miselfu echoTCPPort 7 ...

If I was running erl myself I would just run

erl  -config /usr/local/etc/miselfu/miselfu.config

and then application:start(miselfu). just works. But the thing is that I
am not running erl myself, Emacs is. And although I can customize the
command line, I don't want to always load the config file, I may not be
codding miselfu itself.


> but in order for them to actually take, you also need to load the miselfu  
> application, using application:load(miselfu). If your variables are  
> defined in a .config file, I thought it would be easier to write a small  
> erlang function that reads it and sets them appropriately. It can be done  
> in less than 10 lines of code.

I understand it can be written, but since nobody has written it yet I
suppose people work in other ways, if so, I want to learn that instead
of writing a function (that doesn't belong to my project anyway, but to
Erlang itself).

> I personally try to use builder

What is builder ?

> in situations like that. It builds a bash  
> script that loads all given applications, but doesn't start them (except  
> the mandatory kernel and stdlib), essentially so that one could fire up an  
> erlang shell, with the code path and all the environment variables set,  
> and then do initialization or debugging from the shell.

I think that solution is not quite what I need either.

Thank you.
--
Pupeno <[hidden email]> http://pupeno.com

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

J. Pablo Fernández
In reply to this post by Sean Hinde
On Sun, 2006-06-11 at 13:39 +0100, Sean Hinde wrote:

> On 11 Jun 2006, at 13:06, Pupeno wrote:
>
> > On Sun, 2006-06-11 at 14:02 +0200, Ulf Wiger wrote:
> >> I don't know if there's a standard function for loading  
> >> the .config file,
> >> but you can read and parse a .config file with
> >> file:consult("miselfu.config"), and you can set environment  
> >> variables with
> >> application:set_env(AppName, Key, Value).
> >
> > So, you don't normally work that way ? Opening up a shell on Emacs,
> > running the application and re-loading code as it is modified ?
>
> I would think that most more serious erlang applications use the  
> sys.config file for a few parameters that are only required at  
> startup and not subject to change. Parameters that should change at  
> runtime would typically be stored in a database or opened up as APIs  
> so they can be changed without restarting the application.
>
> Of course your application might be such that it can be restarted  
> after every config change, in which case sys.config might be sufficient.
So far, these configurations, are port numbers. You have to interrupt
the service anyway to change the port you offer the service on. Although
I may store them on a DB latter, right now I only want to be able to run
the app from erl.
--
Pupeno <[hidden email]> http://pupeno.com

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Ulf Wiger-2
In reply to this post by J. Pablo Fernández
Den 2006-06-11 14:42:43 skrev Pupeno <[hidden email]>:

> I understand it can be written, but since nobody has written it yet I
> suppose people work in other ways, if so, I want to learn that instead
> of writing a function (that doesn't belong to my project anyway, but to
> Erlang itself).

What I normally do is (possibly) use environment variables to store
defaults, and then providing an API for setting the desired values
at runtime.

Mnesia, for example, relies on environment variables for various settings,  
but you can also call mnesia:start(Options), and set the same values that  
way. What mnesia does at startup is to read the options - either passed as  
arguments to the start function, or given as environment variables - and  
then storing them as temporary objects in the schema.

The module proplists is a convenient way to handle {Key, Value} options.  
You can, for example, do:

start(StartOpts) ->
   Options = StartOpts ++ application:get_all_env(?APP_NAME),
   Port = proplists:get_value(port, Options),
   ...


The proplists module will fetch the first instance of a given key, so all  
you need to do to override the defaults is to prepend the new values to  
the list.

Regards,
Ulf W
--
Ulf Wiger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

J. Pablo Fernández
On Sun, 2006-06-11 at 16:25 +0200, Ulf Wiger wrote:

> Den 2006-06-11 14:42:43 skrev Pupeno <[hidden email]>:
>
> > I understand it can be written, but since nobody has written it yet I
> > suppose people work in other ways, if so, I want to learn that instead
> > of writing a function (that doesn't belong to my project anyway, but to
> > Erlang itself).
>
> What I normally do is (possibly) use environment variables to store
> defaults, and then providing an API for setting the desired values
> at runtime.
>
> Mnesia, for example, relies on environment variables for various settings,  
> but you can also call mnesia:start(Options), and set the same values that  
> way. What mnesia does at startup is to read the options - either passed as  
> arguments to the start function, or given as environment variables - and  
> then storing them as temporary objects in the schema.
>
> The module proplists is a convenient way to handle {Key, Value} options.  
> You can, for example, do:
>
> start(StartOpts) ->
>    Options = StartOpts ++ application:get_all_env(?APP_NAME),
>    Port = proplists:get_value(port, Options),
>    ...
>
>
> The proplists module will fetch the first instance of a given key, so all  
> you need to do to override the defaults is to prepend the new values to  
> the list.
Does this mean that I would not be using the standard way of application
to load configuration files ?
--
Pupeno <[hidden email]> http://pupeno.com

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Gerd Flaig
In reply to this post by J. Pablo Fernández
Pupeno <[hidden email]> writes:

> Hello,
> I have written an Erlang application called MiSelfu (well, miselfu
> actually). I can start it from the command line with:
>
> erl -sname miselfu -boot miselfu
> -config /usr/local/etc/miselfu/miselfu.config
>
> but I'd like to be able to start it from a already-running emulator,
> that's so I can run it from within my shell running within Emacs.

you can also try setting inferior-erlang-machine-options in Emacs
before starting the emulator, e.g. like this (untested!):

   (setq inferior-erlang-machine-options '("-sname" "miselfu" "-boot" "miselfu" "-config" "/usr/local/etc/miselfu/miselfu.confog"))

I would be very interested to hear if anybody has configured
erlang-mode in a way so that erlang-compile and erlang-next-error
interact with a distel-connected node instead of an inferior-erlang
buffer.

     Goodbyte, Gerd.
--
The last thing one knows in constructing a work is what to put first.
                -- Blaise Pascal

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Ulf Wiger-2
In reply to this post by J. Pablo Fernández
Den 2006-06-11 16:42:55 skrev Pupeno <[hidden email]>:

>> The module proplists is a convenient way to handle
>> {Key, Value} options. You can, for example, do:
>>
>> start(StartOpts) ->
>>    Options = StartOpts ++ application:get_all_env(?APP_NAME),
>>    Port = proplists:get_value(port, Options),
>>    ...
>>
>>
>> The proplists module will fetch the first instance of a given
>> key, so all you need to do to override the defaults is to
>> prepend the new values to the list.
>
> Does this mean that I would not be using the standard way of application
> to load configuration files ?

No, you would use applications and config files in the normal fashion. You  
just don't look up the environment parameters using application:get_env()  
at runtime, but rather in bulk at application start, copying the values to  
your own dynamic store (e.g. a mnesia ram copy table). This gives you the  
freedom to tweak the environment at will.

BR,
Ulf W
--
Ulf Wiger
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Loading application variables

Serge-8
In reply to this post by J. Pablo Fernández
Actually, there is a way to reload the application configuration from
sys.config without restarting a node using a non-documented
application_controller's call (in a similar way how it's done by the
release handler):

reload_config(AppNames, ConfigFile) ->
     {ok, [Config]} = file:consult(ConfigFile),
     Apps = [{application, A, element(2,application:get_all_key(A))}
             || {A,_,_} <- application:while_applications()],
     application_controller:change_application_data(Apps,Config).

It would be nice if the following function was exported from the
application_controller:

check_conf/0

then, you wouldn't need to pass the ConfigFile name to the
reload_config/2 written above, but do:

Config = application_controller:check_conf().

Regards,

Serge


Pupeno wrote:

> On Sun, 2006-06-11 at 13:39 +0100, Sean Hinde wrote:
>
>>On 11 Jun 2006, at 13:06, Pupeno wrote:
>>
>>
>>>On Sun, 2006-06-11 at 14:02 +0200, Ulf Wiger wrote:
>>>
>>>>I don't know if there's a standard function for loading  
>>>>the .config file,
>>>>but you can read and parse a .config file with
>>>>file:consult("miselfu.config"), and you can set environment  
>>>>variables with
>>>>application:set_env(AppName, Key, Value).
>>>
>>>So, you don't normally work that way ? Opening up a shell on Emacs,
>>>running the application and re-loading code as it is modified ?
>>
>>I would think that most more serious erlang applications use the  
>>sys.config file for a few parameters that are only required at  
>>startup and not subject to change. Parameters that should change at  
>>runtime would typically be stored in a database or opened up as APIs  
>>so they can be changed without restarting the application.
>>
>>Of course your application might be such that it can be restarted  
>>after every config change, in which case sys.config might be sufficient.
>
>
> So far, these configurations, are port numbers. You have to interrupt
> the service anyway to change the port you offer the service on. Although
> I may store them on a DB latter, right now I only want to be able to run
> the app from erl.

Loading...