By savithac on
I have used hook_cron() function. In hook_cron() function i implemented batch process and drupal_set_message(). While running cron, batch process not working but drupal_set_message() prints the message.
How to execute Batch Process based on cron run?
Comments
I believe the batch API
I believe the batch API requires browser access to work - and there is no browser involved in the cron process. You can look at the Queue API that will work in hook_cron().
Contact me to contract me for D7 -> D10/11 migrations.
I have used
I have used hook_cron_queue_info(). I have written code for batch process inside hook_cron_queue_info(). This function works but only batch process not run.
Could you paste your cron
Could you paste your cron queue function in here? As Jaypan says, batch API wont itself work in cron, you will need to use hook_cron_queue_info and write your own code to batch the process without using the batch API.
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
You can embed your batch
You can embed your batch process in a function, and you can even embed it in another function, which you could even embed in another function. But if the originally triggering function is cron, then your batch process will not work no matter how many functions you embed it into.
The batch process requires a browser to work. If you are not using a browser to access the batch process, it will not work.
Contact me to contract me for D7 -> D10/11 migrations.
Example how to integrate batch with cron using rules.
The batch api is primary design to work with forms and it depends what is the purpose of your use, but you can create a function that mimic form submission, and then you can use batch together with cron, assume you have function that will be triggered by a rule on cron run, so this function is function1:
From this point it will be normal batch process
It works!
Thanks veso_83, your method does work. So, it is possible to indirectly call batch from cron. Great.
Below is my actual code (used as part of nodes (=words) to a particular taxonomy (=lesson), e.g. http://lingopolo.com/french/online-lessons/examples-of-cow-in-use/words):
drupalshrek
Good example
I love this solution.
Rajveer Singh
rajveer.gang@gmail.com
Use Drush
Make a drush command for your batch and then execute the command on a cron tab.
On D7 you could also use the background process module
In D8
/**
* Function called by cron.
*/
function function1() {
$values = ['confirm' => 1];
$form_state = (new FormState())->setValues($values);
\Drupal::formBuilder()->submitForm('Drupal\my_module\Form\ConfirmImportForm', $form_state);
}
ConfirmImportForm.php:
<?php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Defines a confirmation form to confirm importing manually.
*/
class ConfirmImportForm extends ConfirmFormBase {
...
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = function_that_builds_the_batch_array();
batch_set($batch);
$url = new Url('route_to_some_page');
return $form_state->setRedirectUrl($url);
}
....
}
D8 batch via cron via drush - route required
Thanks @ranieri-machado.
When cron is triggered via drush, batch_process() throws an error about "Route required in Drupal\Core\Url::fromRouteMatch()". Set a route in the
submitForm()manually with:Hello Ranieri Machado,
Hello Ranieri Machado,
Wonderful solution for batch process with in cron. Worked fine. Thanks.
Bhanojee Rao