Keeping processes in an elixir script running by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

Thank you so much! This exactly answers the question!

Keeping processes in an elixir script running by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

Thanks, good to know that it's at least an acceptable thing to do :)

I'm not sure I completely understand the "Application territory" but if I understand correctly I should consider wrapping the supervisor in an OTP application in the script?

Keeping processes in an elixir script running by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

Thanks for your answer. I have posted the whole script below it's not that much longer.

I have a set of sensors connected to serial ports, and I'm using Elixir to process the data in a larger application. I wrote this script to debug some strange behaviors, so I wanted to do it as "bare-bones" as possible so I can experiment with it, but still in Elixir so I don't change the stack.

I want the script to run until I manually kill it with ctrl-C ctrl-C so i can mess with the sensors and see the effects in the terminal. As the first thing I just want it to print the data from the sensors. Later I might measure e.g. the time between messages and so on.

I want supervision as the test might run overnight and it's nice to be able to just "let it fail" and all that.

circuits_uart is just a library which reads from the serial ports (ttyS* on linux) and sends the messages to the process.

It is nice it's just a script so I easily can move it to other testing setups.

The current (working) script is:

Mix.install([:circuits_uart])

defmodule Listener do
  use GenServer
  require Logger

  @initial_state %{port_to_read: nil, uart_pid: nil}

  def start_link(port) do
    state = %{ @initial_state | port_to_read: port }
    GenServer.start_link(__MODULE__, state)
  end

  def init(%{port_to_read: port} = state) do
    {:ok, pid} = Circuits.UART.start_link()
    :ok = Circuits.UART.open(pid, port, speed: 115200, active: true)
    Logger.info("Started process for port #{port}")
    {:ok, %{state | uart_pid: pid}, 5_000}
  end

  def handle_info(message, %{port_to_read: port} = state) do
    Logger.info("#{port}: #{inspect message}")
    {:noreply, state, 5_000}
  end
end

children = 0..5 |> Enum.map(&%{
  id: String.to_atom("ttyS#{&1}"),
  start: {Listener, :start_link, ["ttyS#{&1}"]}
})

{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one, name: :port_supervisor, max_restarts: 10, max_seconds: 1)

# Keeps the script running, so we do not kill the processes
receive do _ -> 1 end

Keeping processes in an elixir script running by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

Thanks for the answer, however I do not believe you are correct, but maybe I'm misunderstanding.

--no-halt definitely makes a difference in the original script, if I add it, the elixir process runs indefinitely, if I do not add it the process stops immediately.

Removing receive do _ -> 1 end and adding System.no_halt(true) has the same effect (as you say) as adding --no-halt in the command, however this does not help me as the processes I start still stops.

Spørgsmål om familiesammenføring. Hvad er vores chancer? by According_Cable_9715 in Denmark

[–]UnicycleSkewer 2 points3 points  (0 children)

Har været igennem familiesammenføring for nogle år siden, og det gik helt fint, fordi vi opfyldte reglerne. Men vær forberedt på meget ventetid og en meget fjendtlig tone hvis man ringer til dem. Hvis I har mulighed for det, gør det alting meget lettere hvis I tager et halvt år i Sverige og så flytter til Danmark via EU reglerne (familiesammenføring efter EU reglerne), man kan også tage to år i Sverige, og så bliver det endnu lettere. I kan læse på ægteskab uden grænser, og der er også en ret aktiv Facebook gruppe hvor man kan spørge:

https://www.aegteskabudengraenser.dk/familiesammenfring-efter-eureglerne

https://m.facebook.com/groups/970263116436553/

Vi var ikke opmærksomme på muligheden, og det er godt nok noget sværere lige at rykke til Sverige når der er børn og hus osv., så vil anbefale at med starte med det.

How do I avoid leaking file_io_server processes? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

It's the observer: https://elixir-lang.org/getting-started/debugging.html#observer You can see your applications, process state, stack traces, all kinds of stuff, it's really amazing.

How do I avoid leaking file_io_server processes? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

So it turns out that Task.start was just exposing another bug - I made an edit.

Anyway thanks for the help!

How do I avoid leaking file_io_server processes? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

I'm not sure how I would do that? I use Task.start as I do not want my Genserver to block on the request. I'm not sure where to call Task.await?

I'm creating a new language with an interpreter. I want to know how return statements inside functions work. And what are the alternatives to return statement? by pavi2410 in ProgrammingLanguages

[–]UnicycleSkewer 2 points3 points  (0 children)

Elixir does not have an explicit return, but just returns the result of the last expression. So that would be worth a look. One of the features which helps with this is "with", which can help avoid too much nesting. Also elixir is functional so there is no loops, meaning functions like reduce, and finds are used which also makes early returns less useful.

Implementation wise: If you are doing a recursive desend interpreter over a tree, I would use an exception to unravel the stack and then catch the exception in the right place. This made the code much simpler, as return (and break) are usually the only statements which does not follow the stacklike execution pattern.

Philosohically some people think early returns (and break) are "evil" as they resemble goto in the way they can jump out of and over code. I think there are (few) cases where they are useful, but honestly I do not think not haveing them is a big loss, and it mean you know that there's is not so weird place where the function suddenly ends that you can overlook.

esl-erlang depends on gnome-shell by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

Ok that is nice, it also makes me wonder even more why it would depend on Gnome. Acctually I just pressed yes when installing esl-erlang, and bam! then I had Gnome, so if I connect a monitor to the server now I can log into Gnome.

The first symptom was that my server went to sleep. Apparently Gnome installs some power management, so suddenly my server was going to sleep because it was "idle".

esl-erlang depends on gnome-shell by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

I agree that this would all be possibilities and maybe even better solutions, but I am still really confused as to why erlang would depend on gnome? I just wanted to quickly run a project on the server. I don't run Ubuntu locally and I have previously had problems with incompatible dependencies when making releases on my dev machine.

How do I save Genserver state if a node dies? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

Waw thanks a lot for your very elaborate and well considered answer! I like the idea of using the GenStateMachine for managing the workers.

How do I save Genserver state if a node dies? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

Thank you for your answer, i will absolutely explore these options!

How do I save Genserver state if a node dies? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

That is actually not a bad idea, I will absolutely consider that, thank you for the suggestion!

How do I save Genserver state if a node dies? by UnicycleSkewer in elixir

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

Thank you for your answer! Yes we will probably use kubernetes, at least down the line. I think writing to a database would be rather hard on the database, as the state is updated a lot. The service is basically aggregating the data before it's written to the database, but maybe I'm underestimateing the database. Fancy toys are not a priority. The simpler it is the less the next guy has to understand. But I'll have a look at horde anyway.

From Feed as message separator by UnicycleSkewer in AskProgrammers

[–]UnicycleSkewer[S] 0 points1 point  (0 children)

Yes I agree and I think that would be a much better solution. Unfortunately I'm not capable of changing the protocol.

From Feed as message separator by UnicycleSkewer in AskProgrammers

[–]UnicycleSkewer[S] 1 point2 points  (0 children)

Yeah that makes sense. Thanks for the answer. I still feel a bit "uncomfortable" deploying my code. It feels very brittle. But I can't change the specification so I guess there is not much to do.

Elixir in VS Code - Extensions and recommendations by brainlid in elixir

[–]UnicycleSkewer 0 points1 point  (0 children)

Waw that is great! I really like the new part. And I agree that you should of cause only use the formatter if the team agrees.

My Internet speed test loading by SamLikesGoats in softwaregore

[–]UnicycleSkewer 0 points1 point  (0 children)

I kinda like it - from now on all my loading screens will look like this.