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

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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

We're in Beta

About us Mission Statement