Code Monkey home page Code Monkey logo

Comments (4)

sorentwo avatar sorentwo commented on July 30, 2024

However, I haven’t found a way to save my data atomically with my job succeeding. There may be good reasons for this but would I can’t think of any. My use case is that I want to get some data from an external rest api and store the result in the database, could I do this within a transaction together with the job so that if it fails the data will never be stored unless the entire job succeeds?

There isn't anything built in that does this automatically, but you can easily handle it within your job. This example uses a private function from the Oban.Query module. All it is doing is setting the state to completed and recording a completed_at timestamp.

def perform(args, job) do
  changeset =
    args
    |> MyApp.RestAPI.get()
    |> MyApp.Record.changeset()

  MyApp.Repo.transaction(fn repo ->
    repo.insert!(changeset)
    Oban.Query.complete_job(Oban.config(), job)
  end)
end

If you happen to have multiple repos you can inject the transaction's repo into the config:

config = %{Oban.config() | repo: repo}

Alternately, if you'd rather not use Oban internals at all, you can make a changeset:

repo.update(Ecto.Changeset.change(job, state: "completed", completed_at: DateTime.utc_now()))

from oban.

hfjallemark avatar hfjallemark commented on July 30, 2024

Got it! So calling Oban.Query.complete_job is basically what Oban would do after my perform function and if I do it inside the function it won't cause any other issues?

from oban.

sorentwo avatar sorentwo commented on July 30, 2024

Exactly. Oban will mark complete a second time, but there isn’t any harm in it.

from oban.

hfjallemark avatar hfjallemark commented on July 30, 2024

Great, thanks for clarifying!

from oban.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.