Skip to content

Instantly share code, notes, and snippets.

@marcaddeo
Last active July 3, 2018 18:45
Show Gist options
  • Save marcaddeo/c016f91aa777b67ca0a61f776e6dbff8 to your computer and use it in GitHub Desktop.
Save marcaddeo/c016f91aa777b67ca0a61f776e6dbff8 to your computer and use it in GitHub Desktop.
Fixes to background_process
diff --git a/README.md b/README.md
index 09008b1..2b08d1b 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,3 @@
-<?php
-
-/**
- * @file
- * Module Installation Note.
- */
-?>
INTRODUCTION
------------
@@ -28,14 +21,12 @@ USAGE
-----
Executing a background process:
-
+```php
<?php
$handle = background_process_start([$myobject, 'mymethod']);
$handle = background_process_start(['myclass', 'mystaticfunction']);
-
-?>
-
+```
Executing arbitrary http requests:
@@ -47,6 +38,7 @@ Plans to join forces with HTTPRL are in place.
Drupal way: blocking/synchronous:
+```php
<?php
$r = [];
for ($i = 0; $i < 10; $i++) {
@@ -54,12 +46,12 @@ for ($i = 0; $i < 10; $i++) {
$r[] = (string) $response->getBody();
}
print_r($r);
-?>
-
+```
Background process way:
non-blocking/a-synchronous (limited to 5 concurrent connections):
+```php
<?php
$r = [];
for ($i = 0; $i < 10; $i++) {
@@ -67,12 +59,13 @@ for ($i = 0; $i < 10; $i++) {
}
background_process_http_request_process($r, ['limit' => 5]);
print_r($r);
-?>
+```
Background process way:
non-blocking/a-synchronous with callback (limited to 5 concurrent connections):
+```php
<?php
/**
@@ -88,7 +81,7 @@ for ($i = 0; $i < 10; $i++) {
}
background_process_http_request_process($r, ['limit' => 5]);
print_r($r);
-?>
+```
Pool management:
@@ -97,8 +90,9 @@ In multiserver environments, it is possible to delegate requests to specific
hosts by declaring service groups and service hosts in the settings file.
Example
+```php
<?php
-$settings['background_process_service_hosts'] = [
+$config['background_process.settings']['background_process_service_hosts'] = [
'ultimate_cron_poorman' => [
'base_url' => 'http://192.168.1.100',
'http_host' => 'mysite.example.com',
@@ -118,7 +112,7 @@ $settings['background_process_service_hosts'] = [
],
];
-$settings['background_process_service_groups'] = [
+$config['background_process.settings']['background_process_service_groups'] = [
'default' => [
'hosts' => ['ultimate_cron_poorman', 'www1', 'www2'],
],
@@ -129,13 +123,12 @@ $settings['background_process_service_groups'] = [
'hosts' => ['www1', 'www2'],
],
];
-?>
-
+```
Default service host:
If a process is started using a service host which is not defined,
-the service host "default" will be used. If "default" is not defined in the
+the service host "default" will be used. If "default" is not defined in the
"background_process_service_hosts" variable, Background Process will fallback
to the "determined default" service host, and ultimately $base_url.
@@ -158,8 +151,9 @@ Background Process and Background Process Apacher Server Status module.
The random load balancer from the Background Process
module is used if none is specified.
+```php
<?php
-$settings['background_process_service_groups'] = [
+$config['background_process.settings']['background_process_service_groups'] = [
// "default" uses the "mymodule_myloadbalancermethod" load balancer.
'default' => [
'hosts' => ['ultimate_cron_poorman', 'www1', 'www2'],
@@ -180,8 +174,7 @@ $settings['background_process_service_groups'] = [
'hosts' => ['www1', 'www2'],
],
];
-?>
-
+```
BUNDLED Modules
---------------
@@ -199,6 +192,7 @@ To programmatically launch a background batch job,
just use background_batch_process_batch() instead of batch_process.
Snippet from Batch API:
+```php
<?php
/**
@@ -220,14 +214,12 @@ function batch_example($options1, $options2, $options3, $options4) {
batch_set($batch);
background_batch_process_batch('node/1');
}
-
-?>
-
+```
Background Process Apache Server Status:
In case a background process dies in such a way that cleanup failed,
-this module checks if the process is running by using
+this module checks if the process is running by using
the apache mod_status module. ExtendedStatus must be on.
A definition per service host is needed for Apache Server Status to work.
diff --git a/background_process.info.yml b/background_process.info.yml
index 8a772f1..fe11803 100644
--- a/background_process.info.yml
+++ b/background_process.info.yml
@@ -1,6 +1,7 @@
-name: 'Background Process'
-description: 'Provides framework for running code in the background'
+name: Background Process
+description: Provides framework for running code in the background
core: 8.x
-php: '5.0'
-datestamp: '1491719592'
+php: '5.6'
type: module
+dependencies:
+ - progress:progress
diff --git a/background_process.module b/background_process.module
index 7b14591..797dd2e 100755
--- a/background_process.module
+++ b/background_process.module
@@ -15,6 +15,7 @@
* @see hook_service_group()
*/
+use Drupal\Core\Url;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Database\Database;
@@ -562,7 +563,7 @@ function background_process_set_process($handle, $callback, $uid, $args, $token)
// Get user.
if (!isset($uid)) {
$user = \Drupal::currentUser();
- $uid = $user->uid;
+ $uid = $user->id();
if ($uid == '') {
$uid = '0';
}
@@ -758,7 +759,7 @@ function background_process_set_service_host($handle, $service_host) {
*/
function background_process_get_service_groups() {
$service_groups = \Drupal::config('background_process.settings')->get('background_process_service_groups');
- $service_groups = [
+ $service_groups += [
'default' => [
'hosts' => [
\Drupal::config('background_process.settings')->get('background_process_default_service_host'),
@@ -841,11 +842,13 @@ function background_process_build_request($url, $service_hostname = NULL, $optio
if (!$service_hostname || empty($service_hosts[$service_hostname])) {
$service_hostname = 'default';
}
+ $service_host = $service_hosts[$service_hostname];
+
$options += [
'absolute' => TRUE,
- 'base_url' => $service_hosts,
+ 'base_url' => $service_host['base_url'],
];
- $url = $service_hosts . '/' . $url;
+ $url = Url::fromUri('base:' . $url, $options)->toString();
$parsed = parse_url($url);
$host = !empty($service_hosts['http_host']) ? $service_hosts['http_host'] : (isset($parsed['host']) ? (isset($parsed['port']) ? ($parsed['host'] . ':' . $parsed['port']) : $parsed['host']) : NULL);
$headers = _background_process_request_headers();
@@ -1327,12 +1330,7 @@ function _background_process_request_headers() {
}
if (substr($key, 0, 5) == 'HTTP_') {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
- if (empty($key)) {
- $headers[$key] = $value;
- }
- else {
- $headers[$key] .= "; $value";
- }
+ $headers[$key] = $value;
}
}
return $headers;
@@ -1397,7 +1395,20 @@ function _background_process_restart($process, $args = []) {
function background_process_get_service_hosts() {
global $base_url;
- $service_hosts = \Drupal::config('background_process.settings')->get('background_process_service_hosts');
- $service_hosts += \Drupal::config('background_process.settings')->get('background_process_derived_default_host');
+ $config = \Drupal::config('background_process.settings');
+
+ $service_hosts = $config->get('background_process_service_hosts');
+ $derived_default_host = $config->get('background_process_derived_default_host');
+
+ if (empty($derived_default_host)) {
+ $derived_default_host = [
+ 'default' => [
+ 'base_url' => $base_url,
+ ],
+ ];
+ }
+
+ $service_hosts += $derived_default_host;
+
return $service_hosts;
}
diff --git a/config/install/background_process.settings.yml b/config/install/background_process.settings.yml
index b9e6f7c..1f765e2 100644
--- a/config/install/background_process.settings.yml
+++ b/config/install/background_process.settings.yml
@@ -1,6 +1,6 @@
background_process_token: ''
-background_process_default_service_group: Default
-background_process_default_service_host: Default
+background_process_default_service_group: default
+background_process_default_service_host: default
background_process_ssl_verification: true
background_process_user_agent: 'Drupal (+http://drupal.org/)'
background_process_service_timeout: 0
@@ -10,7 +10,7 @@ background_process_redispatch_threshold: 10
background_process_cleanup_age: 120
background_process_cleanup_age_running: 28800
background_process_cleanup_age_queue: 86400
-background_process_service_hosts: Default
-background_process_derived_default_host: Default
+background_process_service_hosts: { }
+background_process_derived_default_host: { }
background_process_queues: 3
-background_process_service_groups: Default
+background_process_service_groups: { }
diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php
index 7df5066..e57d0cd 100644
--- a/src/Controller/DefaultController.php
+++ b/src/Controller/DefaultController.php
@@ -2,6 +2,7 @@
namespace Drupal\background_process\Controller;
+use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Controller\ControllerBase;
@@ -121,14 +122,19 @@ class DefaultController extends ControllerBase {
$url = Url::fromUri($base_url . '/background-process/unlock/' . rawurlencode($process->handle));
$external_link = \Drupal::l($this->t('Unlock'), $url);
- if ($process->callback[1] != '') {
- $process->callback = $process->callback[1];
- }
-
+ $user = User::load($process->uid);
$data[] = [
$process->handle,
_background_process_callback_name($process->callback),
- $process->uid,
+ 'user' => [
+ 'data' => [
+ '#title' => $user->getDisplayName(),
+ '#type' => 'link',
+ '#url' => Url::fromRoute('entity.user.canonical', [
+ 'user' => $user->id(),
+ ]),
+ ],
+ ],
$process->service_host,
\Drupal::service('date.formatter')->format((int) $process->start, 'custom', 'Y-m-d H:i:s'),
$progress ? sprintf("%.02f%%", $progress->progress * 100) : $this->t('N/A'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment