Rake tasks and background jobs: patterns for iterating over tenants #195
Replies: 3 comments 6 replies
-
|
This is great, looking at my codebase we always set the tenant before running the job (we don't have jobs that apply to all tenants) Could we perhaps leverage the Active Job Continuations here to track tenants if we don't have idempotent job runs? Regarding the question about api for rails tasks, I'd prefer to be explicit here to show the intent that is to apply to all tenants
|
Beta Was this translation helpful? Give feedback.
-
|
I've been thinking about this a bunch and I think I'm leaning towards just setting up an ENV variable. Similar to how you can use the RAILS_ENV in a rake task. That seems pretty natural to most Rails developers and it theory it's pretty simple to implement (we don't need to add an arg to all the rake tasks). Haven't tested if this works but maybe ARTENANT already does this?: That would solve most of the problems. Maybe we could improve the error message so it's clear to devs what to do. For our app though we're setting up regions as tenants. So I might make an alias to make it really clear what you're doing. Maybe something like: |
Beta Was this translation helpful? Give feedback.
-
|
@flavorjones Should this already be working today? I'm trying something similar, but I'm running into the error below.
|
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
After chatting with @andrewmarkle this morning, I wanted to open a thread to see what patterns people have noticed in their apps around running tasks or jobs for multiple tenants.
Maybe if I give a specific example it will be easier to have a conversation about it.
In the app I'm currently helping build, we have a job that deletes all "due" cards by calling:
But we want this to run for all tenants! So the actual job looks like:
That is, the job loops over all the tenants when it runs. Because it's idempotent, I don't care if the job re-runs.
I can easily imagine another pattern, though, which is to kick off a job for each tenant, something like:
For database tasks, there is a similar set of problems: how to indicate whether an application's rake task should be run over all tenants? One idea is to add a rake task argument:
or add a scoped subtask:
but neither feels great to me.
Does anybody have thoughts on what an API might look like for Active Job and what task declaration style might work for the database tasks, to indicate "add magic here for all tenants"?
Beta Was this translation helpful? Give feedback.
All reactions