From cc5eebd173503ce654b5c52fb34229668345e853 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Tue, 29 Nov 2016 22:40:19 +0000
Subject: [PATCH] The frontend worker can now fork background processes

---
 include/poller.php | 22 ++++++++++++++++++++++
 mod/worker.php     |  5 +++++
 2 files changed, 27 insertions(+)

diff --git a/include/poller.php b/include/poller.php
index b8e14a0965..3aa9cf4b64 100644
--- a/include/poller.php
+++ b/include/poller.php
@@ -508,6 +508,28 @@ function call_worker_if_idle() {
 		return;
 	}
 
+	// Do we have "proc_open"? Then we can fork the poller
+	if (function_exists("proc_open")) {
+		// When was the last time that we called the poller?
+		// Less than 5 minutes? Then we quit
+		if ((time() - get_config("system", "proc_run_started")) < 300) {
+			return;
+		}
+
+		// Remove long running and crashed process entries
+		poller_kill_stale_workers();
+
+		// Do we have an already running worker? Then we quit here.
+		if (poller_active_workers() > 0) {
+			return;
+		}
+
+		get_app()->proc_run(array('php', 'include/poller.php'));
+	}
+
+	// We cannot execute background processes.
+	// We now run the processes from the frontend.
+	// This won't work with long running processes.
 	poller_run_cron();
 
 	clear_worker_processes();
diff --git a/mod/worker.php b/mod/worker.php
index 3fb86dd5b2..9d0902bf36 100644
--- a/mod/worker.php
+++ b/mod/worker.php
@@ -14,6 +14,11 @@ function worker_init($a){
 		return;
 	}
 
+	// We don't need the following lines if we can execute background jobs
+	if (function_exists("proc_open")) {
+		return;
+	}
+
 	clear_worker_processes();
 
 	$workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
-- 
2.39.5