Cron error: LogicException : Render context is empty In a custom module (sel), I have this code:

function sel_cron()
{
  $accountSwitcher = Drupal::service('account_switcher');
  $account = \Drupal\user\Entity\User::load(1);
  $accountSwitcher-&gt;switchTo($account);

  $query = \Drupal::entityQuery('service_category');
  $dLastExtractionDate = strftime("%Y-%m-%d", strtotime("- 1 week", strtotime(\Drupal::state()-&gt;get('sel.NextEmailForNewServices'))));
  $view = views_embed_view('sel_services', 'embed_2', $dLastExtractionDate);
  $view = \Drupal::service('renderer')-&gt;render($view);   &lt;--------------------- problem here

  $accountSwitcher-&gt;switchBack();
}


The expected result of the view is a table but when the result is empty (because of the value of the contextual filter), an unfiltered text ("Nothing to display today. Too bad!") should be displayed instead.
But in this case, I get this error logged:  


  LogicException: Render context is empty, because render() was called
  outside of a renderRoot() or renderPlain() call. Use
  renderPlain()/renderRoot() or #lazy_builder/#pre_render instead. in
  Drupal\Core\Render\Renderer->doRender() (line 243 of
  /Users/efalzon/Sites/JdP8/web/core/lib/Drupal/Core/Render/Renderer.php).


How to fix that?
$view = \Drupal::service('renderer')-&gt;renderPlain($view);


instead of 

$view = \Drupal::service('renderer')-&gt;render($view);


was the solution!
Thanks to @Clive