Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit 3fb87b8

Browse files
committed
Named Worker feature for more powerful multi-process conrtrol in daemons
1 parent a9bcf01 commit 3fb87b8

16 files changed

+444
-116
lines changed

App/Example.php

100644100755
+37-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class App_Example extends Core_Daemon
1616
*/
1717
protected function __construct()
1818
{
19-
// We want to our daemon to loop once every 5 seconds.
20-
$this->loop_interval = 5.00;
19+
// We want to our daemon to loop once per second.
20+
$this->loop_interval = 1.00;
2121

2222
// Set our Lock Provider
2323
$this->lock = new Core_Lock_File;
@@ -56,6 +56,18 @@ protected function setup()
5656

5757
if ($this->is_parent)
5858
{
59+
// Use the INI plugin to provide an easy way to include config settings
60+
$this->worker('ImpressionsHourly', function() {
61+
echo "WORKER BITCH!";
62+
});
63+
64+
// Tell the Worker API to kill the worker process after 60 seconds. If you set this value too low,
65+
// you're going to get a lot of error reports and you'll have to manually run worker processes to complete your task.
66+
$this->ImpressionsHourly->timeout(60);
67+
68+
// Tell the Worker API to run this setup method when the worker process is created --
69+
// Do this if you connect to a database in this setup() method and the worker needs access to the DB connection.
70+
$this->ImpressionsHourly->run_setup(true);
5971
}
6072
}
6173

@@ -68,17 +80,33 @@ protected function setup()
6880
*/
6981
protected function execute()
7082
{
71-
// The Ini plugin implements the SPL ArrayAccess interface, so in your execute() method you can access the data like this:
72-
$example_key = $this->Ini['example_section']['example_key'];
73-
$this->log("Reading value from INI file using the Ini plugin: $example_key");
74-
75-
$this->log('Executing this stuff every ' . $this->loop_interval . ' seconds. The next 2 messages will be coming from forked children that will exit once they log their message.');
83+
// If it's currently running, this won't do anything. If it's idle, this will run it.
84+
// All error and status info from the prior running will be reset
85+
$this->ImpressionsHourly();
7686

87+
// Or you can do it manually
88+
if ($this->ImpressionsHourly->state() <> Core_Worker::Core_Worker_Running)
89+
$this->ImpressionsHourly(); // Or $this->ImpressionsHourly->execute();
90+
91+
// You can pass args directly into your callback or closure:
92+
$this->ImpressionsHourly($arg1, $arg2);
93+
94+
95+
96+
$this->log('The current worker timeout is: ' . $this->ImpressionsHourly->timeout());
97+
98+
return;
99+
100+
// The Ini plugin implements the SPL ArrayAccess interface, so in your execute() method you can access the data like this:
101+
$example_key = $this->Ini['example_section']['example_key'];
102+
103+
$this->log($example_key);
104+
77105
// If you do have a specific long-running task, maybe emailing a bunch of people or using an external API, you can
78106
// easily fork a child process:
79107
$callback = array($this, 'some_forked_task');
80108

81-
if ($this->fork($callback, array('Hello from the first fork() call. Notice my unique PID?')))
109+
if ($this->fork($callback, array('Hello from the first fork() call')))
82110
{
83111
// If we are here, it means the child process was created just fine. However, we have no idea
84112
// if some_long_running_task() will run successfully. The fork() method returns as soon as the fork is attempted.
@@ -90,7 +118,7 @@ protected function execute()
90118
// after the fork. If you create the connection here in the setup() method, It will LOOK like the child has a valid MySQL
91119
// resource after forking, but in reality it's dead. To fix that, you can easily re-run the setup() method in the child
92120
// process by passing "true" as the 3rd param:
93-
$this->fork($callback, array('Hello from the second fork() call. Notice MY unique PID?'), true);
121+
$this->fork($callback, array('Hello from the second fork() call'), true);
94122
}
95123

96124
protected function some_forked_task($param)

App/config.ini

100644100755
File mode changed.

App/config.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)