Skip to content

Commit

Permalink
Merge pull request #211 from esmero/ISSUE-211
Browse files Browse the repository at this point in the history
ISSUE-210: add timeout argument to Hydroponics
  • Loading branch information
DiegoPino authored Nov 27, 2021
2 parents e9be36f + 91f445a commit f4559d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
5 changes: 4 additions & 1 deletion config/schema/strawberryfield.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ strawberryfield.hydroponics_settings:
mapping:
active:
type: boolean
label: If Hydroponics Service is enabled or not
label: 'If Hydroponics Service is enabled or not'
time_to_expire:
type: integer
label: 'How long Hydroponics Service should stay awake, 0 means until done with all queues'
drush_path:
type: string
label: 'Full system path to the /vendor composer Drush installation including drush script'
Expand Down
38 changes: 23 additions & 15 deletions src/Commands/HydroponicsDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Drush\Commands\DrushCommands;
use Drush\Exec\ExecTrait;
use Drush\Runtime\Runtime;
use React\EventLoop\Factory;
use React\EventLoop\Loop;


/**
Expand All @@ -37,7 +37,7 @@ class HydroponicsDrushCommands extends DrushCommands {
public function hydroponics(
) {

$loop = Factory::create();
$loop = Loop::get();
$timer_ping = $loop->addPeriodicTimer(3.0, function () {
// store a heartbeat every 3 seconds.
$currenttime = \Drupal::time()->getCurrentTime();
Expand Down Expand Up @@ -106,22 +106,30 @@ public function hydroponics(
\Drupal::logger('hydroponics')->info("All queues are idle, closing timers");

$loop->cancelTimer($timer);
$loop->stop();
}
}
);

$securitytimer = $loop->addTimer(720.0, function ($timer) use ($loop, $timer_ping, $idle_timer, &$done) {
// Finish all if 720 seconds are reached
\Drupal::logger('hydroponics')->info("720 seconds passed closing Hydroponics Service");
$loop->cancelTimer($timer_ping);
foreach($done as $queue_timer) {
$loop->cancelTimer($queue_timer);
}
\Drupal::state()->set('hydroponics.queurunner_last_pid', 0);
$loop->cancelTimer($idle_timer);
$loop->stop();
}
);
$time_to_expire = (int) \Drupal::config('strawberryfield.hydroponics_settings')->get('time_to_expire');
if ($time_to_expire > 0 ) {
$time_to_expire = round($time_to_expire, 1);
$securitytimer = $loop->addTimer($time_to_expire,
function ($timer) use ($loop, $timer_ping, $idle_timer, &$done, $time_to_expire) {
// Finish all if Time to live in seconds is reached
\Drupal::logger('hydroponics')
->info("@time_to_expire seconds passed closing Hydroponics Service", [
'@time_to_expire' => $time_to_expire,
]);
$loop->cancelTimer($timer_ping);
foreach ($done as $queue_timer) {
$loop->cancelTimer($queue_timer);
}
\Drupal::state()->set('hydroponics.queurunner_last_pid', 0);
$loop->cancelTimer($idle_timer);
$loop->stop();
}
);
}

/* TODO recompile with PCNTL enabled
\pcntl_signal(SIGINT, 'signalhandler');
Expand Down
14 changes: 14 additions & 0 deletions src/Form/HydroponicsSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('strawberryfield.hydroponics_settings');

$active = $config->get('active') ? $config->get('active') : FALSE;
$time_to_expire = $config->get('time_to_expire') !== null ? $config->get('time_to_expire') : 720;
$drush_path = $config->get('drush_path') ? $config->get('drush_path') : NULL;
$home_path = $config->get('home_path') ? $config->get('home_path') : NULL;
$enabled_queues = !empty($config->get('queues')) ? array_flip($config->get('queues')) : [];
Expand Down Expand Up @@ -177,6 +178,17 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#required' => FALSE,
'#default_value' => $active,
];

$form['time_to_expire'] = [
'#title' => 'Time to live (stay awake) for the Hydroponics Service.',
'#description' => 'A value of 0 will force the Service to finish all pending queues before shutting down',
'#type' => 'number',
'#step' => 60,
'#min' => 0,
'#required' => TRUE,
'#default_value' => $time_to_expire,
];

$form['advanced'] = [
'#type' => 'details',
'#title' => 'Advanced settings',
Expand Down Expand Up @@ -307,6 +319,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$global_active = (bool) $form_state->getValue('active');
$drush_path = rtrim($form_state->getValue('drush_path'), '/');
$home_path = rtrim($form_state->getValue('home_path'), '/');
$time_to_expire = (int) trim($form_state->getValue('time_to_expire', 720));
foreach($form_state->getValue('table-row') as $queuename => $queue) {
if ($queue['active'] == 1) {
$enabled[] = $queuename;
Expand All @@ -318,6 +331,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set('drush_path', $drush_path)
->set('home_path', $home_path)
->set('queues', $enabled)
->set('time_to_expire', $time_to_expire)
->save();
parent::submitForm($form, $form_state);
}
Expand Down
8 changes: 8 additions & 0 deletions src/Plugin/Action/StrawberryfieldJsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ public function execute($entity = NULL) {

]);
if (!$this->configuration['simulate']) {
if ($entity->getEntityType()->isRevisionable()) {
// Forces a New Revision for Not-create Operations.
$entity->setNewRevision(TRUE);
$entity->setRevisionCreationTime(\Drupal::time()->getRequestTime());
// Set data for the revision
$entity->setRevisionLogMessage('ADO modified via JSON Patch Search And Replace with Patch:'. $this->configuration['jsonpatch']);
$entity->setRevisionUserId($this->currentUser->id());
}
$entity->save();
}
}
Expand Down

0 comments on commit f4559d6

Please sign in to comment.