Basically I have a model, which needs to automatically update its value in the database if a condition has not been met, but I don’t know how to do that.
For example:
I have the player as a model
If the player after 30 minutes that was inserted (inserted_at) has not been selected, I need the database to change his status from selectable to non-selectable.
What should I use to resolve this?
Hi @matheusmello I’d consider using something like Oban’s scheduled jobs to have something which runs every minute and runs a Repo.update_all query to update relevant rows.
3 Likes
I see a few ways to achieve this:
- Schedule a job for each player (as mentioned by @benwilson512)
- Store a field on the player like
selectable_until: time
and use this field to calculate the selectable
state on-demand (not updating the actual state machine of the player)
- A hybrid approach where you store the field but transition the state for all users that are not
selectable
anymore with one periodic job. This has the benefit that you don’t need one job per player.
2 Likes