Quantcast
Channel: Active questions tagged cron - Drupal Answers
Viewing all articles
Browse latest Browse all 37

How to execute code that uses the dependency injection container, at regular intervals?

$
0
0

Using Drupal 10 - I tried installing Ultimate Cron for D10, but it is in Alpha state and has deprecated methods that will not let it work.

This Cron needs to be executed only on a specific day and time. But the question is, how?

I have created a service that will be used to collect some data and send an email.

services:  rmdrs.send_reminder_service:    class: Drupal\rmdrs\Service\SendRMDRSService    arguments: ['@database', '@plugin.manager.mail', '@language_manager']

The SendRMDRSService class:

<?phpnamespace Drupal\rmdrs\Service;use \Drupal\Core\Language\LanguageManagerInterface;use \Drupal\Core\Mail\MailManagerInterface;use \Drupal\Core\Database\Connection;use Symfony\Component\DependencyInjection\ContainerInterface;class SendRMDRSService {  private $db;  private $today_timestamp;  /**   * The mail manager.   *   * @var \Drupal\Core\Mail\MailManagerInterface   */  protected $mailManager;  /**   * The language manager.   *   * @var \Drupal\Core\Language\LanguageManagerInterface   */  protected $languageManager;  public function __construct(Connection $connection, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager) {    // Create and set a database connection    $this->db = $connection;    $this->today_timestamp = strtotime(date("m/d/Y"));    $this->mailManager = $mail_manager;    $this->languageManager = $language_manager;  }  public static function create(ContainerInterface $container): static {    return new self(      $container->get('database'),      $container->get('plugin.manager.mail'),      $container->get('language_manager')    );  }  public function collectAndSend() {      $due_reminders_data = $this->getDueReminderData();      $reminders_forecast = $this->getRemindersForecast();      $reminder_data = array('total_reminders_due' => $due_reminders_data['total_reminders_due'],'reminders_due' => $due_reminders_data['reminders_due'],'reminder_forecast' => $reminders_forecast,'host' => $_SERVER['HTTP_HOST'],      );      // All system mails need to specify the module and template key (mirrored      // from hook_mail()) that the message they want to send comes from.      $module = 'rmdrs';      $key = 'rmdrs_cron_message';      // Specify 'to' and 'from' addresses.      $to = 'cyberjobesx@gmail.com';      $from = 'cyberjobes@gmail.com';      $build = ['#theme' => 'rmdrs_cron_message','#cron_var' => $reminder_data,      ];      $params['cron_email_body'] = \Drupal::service('renderer')->render($build);      $language_code = $this->languageManager->getDefaultLanguage()->getId();      $send_now = TRUE;      $result = $this->mailManager->mail($module, $key, $to, $language_code, $params, $from, $send_now);  }  private function getDueReminderData() {    $reminder_due_data = null;    $reminders_due_query = $this->db->select('rmdrs_schedules', 'sched');    $reminders_due_query->join('rmdrs_reminders', 'rem', 'rem.id = sched.reminder_id');    $reminders_due_query->join('rmdrs_categories', 'cat', 'cat.id = rem.category_id');    $reminders_due_query->join('rmdrs_frequencies', 'freq', 'freq.frequency_code = rem.frequency');    $reminders_due_query->addField('sched', 'id', 'schedule_id');    $reminders_due_query->addField('rem', 'id', 'reminder_id');    $reminders_due_query->fields('cat', ['category'])->fields('rem', ['title',])      ->fields('sched', ['next_reminder_date', 'next_reminder_timestamp', 'reminder_id'])      ->fields('freq', ['frequency_text'])      ->orderBy("sched.next_reminder_timestamp", "ASC")      ->condition('sched.active', '1')      ->condition('sched.completed', '1', '<>')      ->condition('sched.next_reminder_timestamp', $this->today_timestamp, '<=');    $reminder_due_data['total_reminders_due'] = count($reminders_due_query->execute()->fetchAll());    if ($reminder_due_data['total_reminders_due'] != 0) {      $reminder_due_data['reminders_due'] = $reminders_due_query->range(0, 10)->execute()->fetchAll();    } else {      $reminder_due_data['reminders_due'] = "No Reminders due at this time";    }    return $reminder_due_data;  }  private function getRemindersForecast() {    $reminders_forecast_query = $this->db->select('rmdrs_schedules', 'sched');    $reminders_forecast_query->join('rmdrs_reminders', 'rem', 'rem.id = sched.reminder_id');    $reminders_forecast_query->join('rmdrs_categories', 'cat', 'cat.id = rem.category_id');    $reminders_forecast_query->join('rmdrs_frequencies', 'freq', 'freq.frequency_code = rem.frequency');    $reminders_forecast_query->addField('sched', 'id', 'schedule_id');    $reminders_forecast_query->addField('rem', 'id', 'reminder_id');    $reminders_forecast_query->fields('cat', ['category'])->fields('rem', ['title',])      ->fields('sched', ['next_reminder_date', 'next_reminder_timestamp', 'reminder_id'])      ->fields('freq', ['frequency_text'])      ->orderBy("sched.next_reminder_timestamp", "ASC")      ->condition('sched.active', '1')      ->condition('sched.completed', '1', '<>')      ->condition('sched.next_reminder_timestamp', $this->today_timestamp, '>');    return $reminders_forecast_query->range(0, 10)->execute()->fetchAll();  }}

Since I cannot use Ultimate Cron I want to use the Cron in my linux server. How do I fire off this service via Cron?

I've been reading the doc but I'm not getting what they want me to do here. I tried this

rmdrs.servicetest:  path: '/service-test'  defaults:    _controller: 'Drupal\rmdrs\Service\SendRMDRSService::collectAndSend'  arguments: ['@database', '@plugin.manager.mail', '@language_manager']  requirements:    _permission: 'TRUE'

But I continue to get an error. Here is what I am getting:

ArgumentCountError: Too few arguments to function Drupal\rmdrs\Service\SendRMDRSService::__construct(), 0 passed in /Users/jobes/Websites/int-apps/web/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php on line 31 and exactly 3 expected in Drupal\rmdrs\Service\SendRMDRSService->__construct() (line 28 of /Users/jobes/Websites/int-apps/web/modules/custom/rmdrs/src/Service/SendRMDRSService.php).

Can I get some guidance in this?


Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>