2

I have a MAIN process that spawn an implementation of a gen_fsm behavior, but this MAIN process is not an implementation of supervisor behavior, its just another module. Let say the implementation of gen_fsm is called GAME_ROOM. My case is like this:

  1. When ever there are 3 peoples ready, the MAIN process will spawn a new GAME_ROOM.
  2. I use gen_fsm:start_link function to initiate a new GAME_ROOM, so if the GAME_ROOM exit by error, my MAIN process could spawn a new one, to replace the downed process.
  3. I managed to make my MAIN process detect the EXIT event of all downed GAME_ROOM

The problem is: I need to restore all downed GAME_ROOM states at the new one. My question is: How can I use gen_fsm's terminate function to pass the latest states of the gen_fsm to my MAIN process, so when I respawn a new GAME_ROOM, I can pass that states?

Peer Stritzinger
  • 8,232
  • 2
  • 30
  • 43
Bromo Programmer
  • 670
  • 2
  • 13
  • 37

4 Answers4

1

Read about process_flag ({trap_exit, true}) and handle info 'EXIT'.

Sergey Miryanov
  • 1,820
  • 16
  • 29
1

One simple way would be for GAME_ROOM terminate/3 to send a message with the necessary state information to MAIN. For this to work the GAME_ROOM must know the pid of MAIN (easy) and you have to be certain that terminate/3 is really called.

rvirding
  • 20,848
  • 2
  • 37
  • 56
0

First of all, I would really suggest you to look at using supervisors in your implementation, to avoid re-inventing the wheel.

A possibility could be to create an ETS table in your MAIN, so you can store data from within your gen_fsms which can survive process crashes.

Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112
  • agree with Robert, if you are using erlang and not designing your application as per OTP principles, you will most probably end up with similar problems. Refer this: http://www.erlang.org/doc/design_principles/ for help docs. – Abhinav Singh May 20 '11 at 14:17
  • @Abhinav: more useful sub-link is this one: http://www.erlang.org/doc/design_principles/des_princ.html – Asim Ihsan May 21 '11 at 09:32
0

My belief is that if GAME_ROOM exits because of an error, there is nothing to save (how do you know your state is valid, otherwise you would trap the error inside GAME_ROOM).

Victor Moroz
  • 9,167
  • 1
  • 19
  • 23