Comments (4)
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.
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.
Exactly. Oban will mark complete a second time, but there isn’t any harm in it.
from oban.
Great, thanks for clarifying!
from oban.
Related Issues (20)
- Potential bug: available jobs not being run HOT 4
- (Postgrex.Error) ERROR 42601 (syntax_error) on deploy HOT 1
- Enqueueing a job with a non-serializable value (changeset) causes error HOT 1
- Oban.Pro.Testing - Upgrade from 1.3.2 `ensure_queues_started` "process not alive" HOT 6
- Oban.Pro.Worker Hook Docs HOT 1
- oban_web - `KeyError` in `Oban.Web.Jobs.SortComponent.render/1` HOT 5
- Add `crontab: false` to recommended test config HOT 5
- `FunctionClauseError` in `Registry.keys/2` HOT 2
- `not_null_violation` Postgres error for Oban scheduled job HOT 8
- Config for Oban.Met? Not seen this before from AppSignal HOT 5
- Oban.Pro.Workers.Chain with test inline mode causes Ecto error on comparison with nil id HOT 1
- Oban - Partitioning by worker and args at the same time does not work HOT 2
- with_testing_mode is ignored if enqueuing within another process HOT 1
- DynamicCron cannot insert jobs for Crontab entries that override `guaranteed` HOT 5
- Oban.drain_queue isn't working anymore in test HOT 2
- Oban Pro v1.4 Workflow migration fails on Postgres 13 trigger creation HOT 1
- Tests failing since upgrade from 2.17.7 to 2.17.9 HOT 2
- Test fails when upgrading from 2.17.8 to 2.17.9 HOT 1
- Regression: `Check process ancestry tree for test mode override` commit makes my tests fail HOT 1
- Node name not set from DYNO var with mix release on Heroku HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oban.