Score:1

How to know if a batch process is still running?

lb flag

I have a form with 5 buttons and each button run a different batch, some batch processes take several minutes. I want to block the button if the batch processes is already running, because other user admin can access to the same form and try to run the batch that is already running at this moment for other user.

How can i know what batch processes is running? i try using batch_get(); but every time this function return an empty array.

Score:1
cn flag

As per the API, once you define a batch, you provide the callback function that will be executed when the batch finishes, so until this callback is called, the batch is still ongoing.

So ideally your batch definition would perform an initial action to like setting a DB status that you can query and if it is blocked, you disallow the button, and once your finished callback is executed, you unlock the button again.

Mauricio avatar
lb flag
Thanks @jbarrio i'm currently doing this like you suggest, i use the `code \Drupal::state()->set('batch_import_'. $sourceId, serialize($result_source));` to set a "running" variable to know it. But this batch is a progressive batch, so if the user close this tab the batch never ends and never fall in the finished callback.
jbarrio avatar
cn flag
Well, to be honest, I answered exactly what you asked. What you are mentioning now is a different requirement. But in your new scenario, if a user closes the tab and the batch gets stuck, you can run a cron to clean the stuck batches from the table and reset the batch_import_ID state, so the button could be visible again.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.