henriquesati

henriquesati

Mnesia Memento being update through iex and files except for oban jos

I have the given Mnesia domain

defmodule MnesiaPendingContracts do
  require Logger
  use Memento.Table,
    attributes: [:pubkey, :last_attempt, :attempts, :time_limit, :ok_trys, :failed_retrys],
    type: :ordered_set

end

defmodule Testespay.DataLayer.MnesiaPendingContractsDomain do
  require Logger
  alias Memento.Query
  @time_limit Application.get_env(:testespay, :transaction_seconds_time_limit)
  def insert_record(pk) do
    Memento.transaction(fn ->
      record = %MnesiaPendingContracts{
        pubkey: pk,
        last_attempt: 0,
        attempts: 0,
        time_limit: @time_limit,
        ok_trys: 0,
        failed_retrys: 0
      }

      Query.write(record)
    end)
  end
  def att_ok_trys(pk) do
    Memento.transaction!(fn ->
      case Memento.Query.read(MnesiaPendingContracts, pk) do
        nil ->
          Logger.debug("Registro não encontrado para: #{pk}")
          :not_found

          record ->
            Logger.debug("enter ------------------------------------------------------------------------------------------------------------")
          updated = %{record |
            ok_trys: record.ok_trys + 1,
            last_attempt: DateTime.utc_now()
          }
          Logger.debug("------------------------------------------------------------------------------------------------------------")
          Logger.debug(updated)
          Memento.Query.write(updated)
      end
    end)
  end

  def att_failed_retrys(pk) do
    Memento.transaction!(fn ->
      case Memento.Query.read(MnesiaPendingContracts, pk) do
        nil ->
          Logger.debug("Registro não encontrado para: #{pk}")
          :not_found

          record ->
          updated = %{record |
            failed_retrys: record.failed_retrys + 1,
            last_attempt: DateTime.utc_now()
          }
          Memento.Query.write(updated)
      end
    end)
  end

I can call att_ok_trys from iex

iex(1)> Testespay.DataLayer.MnesiaPendingContractsDomain.insert_record("A")
{:ok,
 %MnesiaPendingContracts{
   __meta__: Memento.Table,
   pubkey: "A",
   last_attempt: 0,
   attempts: 0,
   time_limit: 100,
   ok_trys: 0,
   failed_retrys: 0
 }}

[debug] enter ------------------------------------------------------------------------------------------------------------
[debug] ------------------------------------------------------------------------------------------------------------
[debug] [__struct__: MnesiaPendingContracts, __meta__: Memento.Table, pubkey: "A", last_attempt: ~U[2025-02-26 01:15:21.442310Z], attempts: 0, time_limit: 100, ok_trys: 1, failed_retrys: 0]
%MnesiaPendingContracts{
  __meta__: Memento.Table,
  pubkey: "A",
  last_attempt: ~U[2025-02-26 01:15:21.442310Z],
  attempts: 0,
  time_limit: 100,
  ok_trys: 1,
  failed_retrys: 0
}

and it works
I can call from a gen server init callback

defmodule Testespay.Backgroundjobs.FetchAwaitingContracts do
  alias Testespay.Oban.PubkeyCheckWorker
  use GenServer
  require Logger
  alias Testespay.DataLayer.MnesiaDomain
  alias Oban
  alias Testespay.DataLayer.MnesiaPendingContractsDomain
  @interval 5_000 # 5 segundos

  def start_link(_) do
    GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
  end

  @impl true
  def init(state) do
    MnesiaPendingContractsDomain.insert_record("address")
    MnesiaPendingContractsDomain.att_ok_trys("address")
    schedule_work()
    {:ok, state}
  end

[debug] enter ------------------------------------------------------------------------------------------------------------
[debug] ------------------------------------------------------------------------------------------------------------
[debug] [struct: MnesiaPendingContracts, pubkey: “address”, meta: Memento.Table, last_attempt: ~U[2025-02-26 01:31:20.746365Z], attempts: 0, time_limit: 100, ok_trys: 1, failed_retrys: 0]
Interactive Elixir (1.17.0) - press Ctrl+C to exit (type h() ENTER for help)
[info] Starting background task to fetch recent contracts
[debug] {:ok, [%MnesiaPendingContracts{meta: Memento.Table, pubkey: “address”, last_attempt: ~U[2025-02-26 01:31:20.746365Z], attempts: 0, time_limit: 100, ok_trys: 1, failed_retrys: 0}]}
[debug] Resultado fetching: [%MnesiaPendingContracts{meta: Memento.Table, pubkey: “address”, last_attempt: ~U[2025-02-26 01:31:20.746365Z], attempts: 0, time_limit: 100, ok_trys: 1, failed_retrys: 0}]
[debug] {:ok, }
[info] No unexpired contracts found in Mnesia.

on a handle info

def handle_info(:work, state) do
    Logger.info("on handle info callback")
    MnesiaPendingContractsDomain.insert_record("address")
    MnesiaPendingContractsDomain.att_ok_trys("address")

[info] on handle info callback
[debug] enter ------------------------------------------------------------------------------------------------------------
[debug] ------------------------------------------------------------------------------------------------------------
[debug] [struct: MnesiaPendingContracts, pubkey: “address”, meta: Memento.Table, last_attempt: ~U[2025-02-26 01:36:58.362964Z], attempts: 0, time_limit: 100, ok_trys: 1, failed_retrys: 0]

but when I try to run it from an oban job, doesn’t work

 def perform(%Oban.Job{attempt: attempt, meta: meta} = job) do

    Logger.configure(level: :debug)
    Logger.debug("initing job....")
    contract_data = job.args["contract"]
    address = contract_data["pubkey"]
    target_value = contract_data["amount"]

    case perform_request(address) do
      {:ok, response_value}->
        MnesiaPendingContractsDomain.insert_record(address)
        MnesiaPendingContractsDomain.att_ok_trys(address)

        case handle_check_balance(response_value, target_value) do
            :paid -> :ok
            {:snooze, @snooze_time} -> {:snooze, @snooze_time}
          end

      {:error, :rate_limit} ->
        MnesiaPendingContractsDomain.att_failed_retrys(address)
      {:error, reason} -> {:snooze, @snooze_time}
    end
  end

[debug] initing job…
[debug] init request…
[debug] 35XeuP2xcB4KQ6EU3NVMenNMnVYQet84knezSoE2zzRY
[debug] https://rpc.ankr.com/solana_devnet/ae321adefbcd42cb37d0790c522ce876c7e9f19dbfece1d44b9e90845e855100
[debug] Resposta recebida: %Finch.Response{status: 200, body: “{"id":1,"jsonrpc":"2.0","result":{"context":{"apiVersion":"2.1.14","slot":363622841},"value":0}}”, headers: [{“date”, “Wed, 26 Feb 2025 01:39:45 GMT”}, {“content-type”, “application/json”}, {“content-length”, “96”}, {“connection”, “keep-alive”}, {“strict-transport-security”, “max-age=15724800; includeSubDomains”}, {“access-control-allow-origin”, “*”}, {“access-control-allow-credentials”, “true”}, {“access-control-allow-methods”, “GET, PUT, POST, DELETE, PATCH, OPTIONS”}, {“access-control-allow-headers”, “DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,solana-client”}, {“access-control-max-age”, “1728000”}], trailers: }
[debug] Resposta HTTP 200: {“id”:1,“jsonrpc”:“2.0”,“result”:{“context”:{“apiVersion”:“2.1.14”,“slot”:363622841},“value”:0}}
[debug] Saldo extraído: 0

Marked As Solved

sorentwo

sorentwo

Oban Core Team

It turns out this was related to an issue in the code. Oban rescues and records all exceptions and it wasn’t apparent that an error was happening.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement