Running an '.exs' script vs inside 'iex'

I am currently working through The Little Elixir & OTP Guidebook and to consolidate knowledge I have made a mix project that uses several spawned processes with a coordinator that writes the collected data to a JSON file when they have completed.

When I run the main function in an iex session the file gets written but if I run the function using an .exs file the JSON never gets written. Any suggestions?

1 Like

Can you show the script? If you’re doing something async, the exs file is going to terminate as soon as nothing blocks the main process. IEX however will hang around.

Yeah sure, it’s here.

Is the gitgo thing flushing the file out before it returns? If not then it can be done ‘later’, and if the system ends before ‘later’ occurs then no file will be written. Have you tried Process.sleeping after your puts call to test?

Right so your code spawns a bunch of stuff, but nothing waits around for that stuff to finish. https://github.com/ronanyeah/gitgit/blob/master/lib/gitgit.ex#L16

IEX never quits anyway, so eventually stuff finishes. exs however will quit as soon as the main process returns, regardless of if other processes are doing things.

You should consider writing this as a proper mix application.

1 Like

Thanks! I’m actually in the middle of writing it using a GenServer, is that what you mean?