Initialising code

19 views
Skip to first unread message

John Smith

unread,
Sep 25, 2013, 4:48:11 PM9/25/13
to moch...@googlegroups.com
Is there a way to run code at the startup of the server? I need to parse some config files and do a few other things.

Geoff Cant

unread,
Sep 25, 2013, 5:09:59 PM9/25/13
to moch...@googlegroups.com
OTP has a standard way of doing startup in phases ( start_phases in your .app file and $MY_APP_app.erl callback module ).

This functionality is documented here: http://www.erlang.org/doc/man/app.html, and I have an example I used in a presentation here:
* app file: https://github.com/archaelus/erlang-factory-2012-example/blob/master/src/ef.app.src#L14
* callback module: https://github.com/archaelus/erlang-factory-2012-example/blob/master/src/ef_app.erl#L54

The trick essentially is that AppCallback:start/2 happens before AppCallback:start_phase/3 - so in my example, the entire application supervision structure is started before the application begins listening for HTTP requests / work.

( I'm sorry the example uses cowboy instead of mochiweb -- the other thing I work on with mochiweb and start phases is significantly more complicated - https://github.com/heroku/logplex/blob/master/src/logplex_app.erl#L75 )

Cheers,
-Geoff

Bob Ippolito

unread,
Sep 25, 2013, 5:17:23 PM9/25/13
to moch...@googlegroups.com
Yes, just make your supervisor call that code before it initializes mochiweb.


On Wed, Sep 25, 2013 at 1:48 PM, John Smith <ons...@gmail.com> wrote:
Is there a way to run code at the startup of the server? I need to parse some config files and do a few other things.

--
You received this message because you are subscribed to the Google Groups "MochiWeb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mochiweb+u...@googlegroups.com.
To post to this group, send email to moch...@googlegroups.com.
Visit this group at http://groups.google.com/group/mochiweb.
For more options, visit https://groups.google.com/groups/opt_out.

John Smith

unread,
Sep 26, 2013, 7:12:21 AM9/26/13
to moch...@googlegroups.com
Am I correct in understanding that the specified phase in the ef.app.src is the first parameter of start_phase/3 (I am referring to the listen phase)

Geoff Cant

unread,
Sep 26, 2013, 2:30:50 PM9/26/13
to moch...@googlegroups.com
Yes - the {'phase_name', PhaseArgs} term in the .app file will be the arguments to:
AppCallback:start_phase('phase_name', _StartType, PhaseArgs).

-G

On 2013-09-26, at 04:12 , John Smith <ons...@gmail.com> wrote:

> Am I correct in understanding that the specified phase in the ef.app.src is
> the first parameter of start_phase/3 (I am referring to the listen phase)
>
> On Wednesday, 25 September 2013 14:09:59 UTC-7, Geoff Cant wrote:
>>
>> OTP has a standard way of doing startup in phases ( start_phases in your
>> .app file and $MY_APP_app.erl callback module ).
>>
>> This functionality is documented here:
>> http://www.erlang.org/doc/man/app.html, and I have an example I used in a
>> presentation here:
>> * app file:
>> https://github.com/archaelus/erlang-factory-2012-example/blob/master/src/ef.app.src#L14
>> * callback module:
>> https://github.com/archaelus/erlang-factory-2012-example/blob/master/src/ef_app.erl#L54
>>
>> The trick essentially is that AppCallback:start/2 happens before
>> AppCallback:start_phase/3 - so in my example, the entire application
>> supervision structure is started before the application begins listening
>> for HTTP requests / work.
>>
>> ( I'm sorry the example uses cowboy instead of mochiweb -- the other thing
>> I work on with mochiweb and start phases is significantly more complicated
>> - https://github.com/heroku/logplex/blob/master/src/logplex_app.erl#L75 )
>>
>> Cheers,
>> -Geoff
>>
>> On 2013-09-25, at 13:48 , John Smith <ons...@gmail.com <javascript:>>
Reply all
Reply to author
Forward
0 new messages