Use of batch in REST endpoints I want to use batch in POST in rest endpoints in Drupal 8, but my batch is not getting progressed.
I am creating rest endpoints for my module which will send email to bulk users. So for this purpose I need to use batch which will send email to all users without giving time out error. Please help.

my source code is:

class BatchController {

    public static function callBatch() {

        $batch = array(
        'title' =&gt; t('Sending ...'),
        'operations' =&gt; [],
        'init_message' =&gt; t('Sending is starting.'),
        'finished' =&gt; '\Drupal\module_mail\sendEmails::sendEmailsItemCallback',
        );

        $subject='hello';
        $body='body';

        $data = array(0 =&gt; array('id'=&gt;1,'Fullname'=&gt;"joy", 'Email'=&gt;"example@gmail.com")
            );
        foreach($data as $row) {
        $batch['operations'][] = ['\Drupal\module_mail\sendEmails::sendEmailsUserItem',[$row,$subject,$body]];
        }
        batch_set($batch);
        return batch_process('home');
    }


}


namespace Drupal\module_mail;

class sendEmails {

    public static function sendEmailsUserItem($user, $subject, $body, &amp;$context)
    {           
        $message = 'Sending emails';        
        $mailManager = \Drupal::service('plugin.manager.mail');
        $module = 'module_mail';
        $key = '';
        $to= $user['Email'];
        $params['email_body'] = $body;
        $params['email_subject'] = $subject;
        $langcode = \Drupal::currentUser()-&gt;getPreferredLangcode();
        $send = true;
        $result = $mailManager-&gt;mail($module, $key, $to, $langcode, $params, NULL, $send);  
    }


        //batch completed
    public static function sendEmailsItemCallback($success, $results, $operations)
    {
        if ($success) {
            $message = \Drupal::translation()-&gt;formatPlural(
                    count($results),
                    'sent.', 
                    'Emails sent succesfully'
            );
        }
        else {
            $message = t('Finished with an error.');
        }
        drupal_set_message('Sent succesfully');

    }


}


Right now I am getting this output:
Batch API operations are driven by the end user and browser with meta refresh, which makes it not possible to use in web services. However we can think about implementing the same thing using Queue API.

This should give you enough to go on to re-implement your RestResource or Controller to use your own Queue service.

Summary

I've used the following pattern in the past.


First request has a payload that can instantiate the operation.


It's important to make sure we're not flooding the serve so make sure you have flood protection.
Make sure that we have access to do the operation.

A response is returned with progress state and a queue item ID.
Subsequent requests have a payload with the queue item ID from the previous response.


These requests process 1..n queue items.
We need to claim queue items ourselves because we're processing queue items manually and not with a cron queue worker.
Make sure that things are valid and return the appropriate response code for validation, fatal or other errors.



Request payloads


Initial: {"queue": null}
Subsequent: {"queue": {"item_id": 1}}


Response payloads

Initial

{
   "success": true,
   "finished": false,
   "queue": {"item_id": 1},
   "progress": 0,
   "max": 23,
   "errors": [],
   "result": "something custom here",
}


Finished

{
   "success": true,
   "finished": true,
   "queue": null,
   "progress": 23,
   "max": 23,
   "errors": ["something wrong happened at some point"],
   "result": "something custom here",
}


Queue API

The complexity here is that we cannot use cron queue workers. So we need to implement the database calls to get and claim queue items ourselves. Unfortunately the current Queue API classes are pretty limited in both Drupal 7 and Drupal 8. In the former, I used custom functions to fetch and update queue items directly from the database while in the latter we can create our own service, based on DatabaseQueue, and inject that into your RestResource plugin or Controller.