erlang Elixir GenServer handle_cast未被调用

rur96b6h  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(142)

我有一个句柄转换的问题。在模块加载后,它调用periodic_comm,每隔5秒调用保存_data方法。在我的save_data方法中,日志被打印,但语句什么也不做。

GenServer.cast(__MODULE__, {:save_load_data, data})

句柄转换方法在此处定义

def handle_cast({:save_load_data, data}, state) do
  Logger.error("uin saving data====================================")
  # state = Map.put_new(state, :load_data, data)
  # Logger.debug "updated state : #{inspect state}"
  {:noreply, state}
end

整个文件如下所示

defmodule Core.Inverter.Manager do
    # core inverter process that handles the inverter communication
    use GenServer
    require Logger

    def start_link(_) do
      Logger.info("putting some string")
      GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
    end

    @impl true
    def init(_) do
      Logger.info("inverter Manager init'ed")
      {:ok, %{configs: nil, inverter_connection: nil, inverter_impl: nil}}
    end

    # public api
    # initialize the inverter and start periodic comm
    def init_inverter(configs) do
      GenServer.cast(__MODULE__, {:init_inverter, configs})
      GenServer.cast(__MODULE__, {:start_periodic_comm, configs})
    end

    def inverter_off() do
      GenServer.call(__MODULE__, :inverter_off)
    end

    @impl true
    def handle_cast({:init_inverter, configs}, state) do
      Logger.debug("initializing the inverter")
      # fetching inverter type/configurations
      inverter_impl = get_inverter_impl(configs)
      # initializing inverter comm channel
      inverter_connection = inverter_impl.init(configs)

      state = %{
        configs: configs,
        inverter_connection: inverter_connection,
        inverter_impl: inverter_impl
      }

      Logger.warn("inverter type #{state.inverter_impl}")
      {:noreply, state}
    end

    def handle_cast({:start_periodic_comm, configs}, state) do
      Logger.warn(
        "Actually Starting inverter periodic COMM with inverter type #{state.inverter_impl}"
      )

      start_periodic_comm(state)
      {:noreply, state}
    end

    @impl true
    def handle_call(
          :inverter_off,
          _from,
          %{inverter_impl: inverter_impl, inverter_connection: inverter_connection} = state
        ) do
      {:ok, inverter_connection} = inverter_impl.inverter_off(inverter_connection)
      {:reply, :ok, %{state | inverter_connection: inverter_connection}}
    end

    def handle_cast({:save_load_data, data}, state) do
      Logger.error("uin saving data====================================")
      # state = Map.put_new(state, :load_data, data)
      # Logger.debug "updated state : #{inspect state}"
      {:noreply, state}
    end

    defp start_periodic_comm(state) do
      periodic_comm(state)
    end

    defp periodic_comm(state) do
      # call functions from the inverter_type module, e.g infit.ex
      receive do
      after
        5000 ->
          {:ok, data} = state.inverter_impl.get_load_data(state)
          save_data(data)
      end

      periodic_comm(state)
    end

    # defp save_data() do end
    def save_data(data) do
      Logger.debug("saving data")
      Logger.debug("in savinf data data is : #{inspect(data)}")
      GenServer.cast(__MODULE__, {:save_load_data, data})
    end

    defp get_inverter_impl(configs) do
      # add case statements here
      Core.Inverter.Infini
    end
  end
uyhoqukh

uyhoqukh1#

我对elixir的语法不是很熟悉,但在我看来periodic_comm是循环的:

defp periodic_comm(state) do
      # call functions from the inverter_type module, e.g infit.ex
      receive do
      after
        5000 ->
          {:ok, data} = state.inverter_impl.get_load_data(state)
          save_data(data)
      end

      periodic_comm(state)
    end

不管save_data的结果如何,线程都在无休止地循环调用periodic_comm,因此它没有机会接收和执行save_load_data消息。
为了解决这个问题,你应该重构服务器,使handle_X中的逻辑没有循环,其中receive包含所有类型的消息。你可以使用erlang:start_timer(我不知道酏剂的对应物)或依赖gen_server中的Timeout返回值来接收超时消息。

相关问题