-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibGearman.php
More file actions
executable file
·172 lines (155 loc) · 4.65 KB
/
LibGearman.php
File metadata and controls
executable file
·172 lines (155 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
*
* Background Job Library Function Require for Background job
*
* @category PHP
* @package SocialManager
* @author Kishan Jasani <kishanjasani007@yahoo.in>
* @license https://github.com/kishanjasani/SociaManager
*
*/
class LibGearman {
public $gearman_host = array();
public $gearman_port = array();
public $errors = array();
public $client = null;
public $worker = null;
public $priority = array('high', 'low', 'normal');
/**
* Constructor
*
* @access public
* @return void
*/
public function __construct() {
if (!$this->is_supported()) {
return false;
}
}
/**
* Is supported
*
* Returns false if Gearman is not supported on the system.
* If it is, we setup the gearman object & return true
*/
public function is_supported() {
if (!extension_loaded('gearman')) {
//log_message('error', 'The Gearman Extension must be loaded to use Gearman client.');
return false;
}
return true;
}
/**
* Function to create a gearman client
*
* @access public
* @return void
*/
public function gearman_client() {
$this->client = new GearmanClient();
$this->_auto_connect($this->client);
return $this->client;
}
/**
* Function to create a gearman worker
*
* @access public
* @return void
*/
public function gearman_worker() {
$this->worker = new GearmanWorker();
$this->_auto_connect($this->worker);
return $this->worker;
}
/**
* get worker or client obj
*
* @access public
* @param string
* @return object
*/
public function current($obj = 'client') {
return (isset($this->{$obj})) ? $this->{$obj} : false;
}
/**
* Function to assign a function name against an identifier
*
* @access public
* @param string
* @param string
* @return void
*/
public function add_worker_function($identifier, $function_name) {
$this->worker->addFunction($identifier, $function_name);
//log_message('debug', "Gearman Library: Successfully added worker function with identifier $identifier with function $function_name");
}
/**
* Listen for a job
*
* @access public
* @return bool true on sucess, false on failure
*/
public function work() {
return $this->worker->work();
}
/**
* Perform a job in background for a client
*
* @access public
* @param string
* @param string
* @param string [high|low]
* @return void
*/
public function do_job_background($function, $param, $priority = 'normal') {
if (!in_array($priority, $this->priority)) {
return false;
}
$callback_function = ($priority == 'normal') ? 'doBackground' : 'do' . ucfirst($priority) . 'Background';
$this->client->{$callback_function}($function, $param, md5(uniqid(rand(), true)));
//log_message('debug', "Gearman Library: Performed task with function $function with parameter $param");
}
/**
* Perform a job in foreground for a client
*
* @access public
* @param string
* @param string
* @param string [high|normal|low]
* @return string
*/
public function do_job_foreground($function, $param, $priority = 'normal') {
if (!in_array($priority, $this->priority)) {
return false;
}
$callback_function = 'do' . ucfirst($priority);
//log_message('debug', "Gearman Library: Performed task with function $function with parameter $param");
return $this->client->{$callback_function}($function, $param, md5(uniqid(rand(), true)));
}
/**
* Runs through all of the servers defined in the configuration and attempts to connect to each
*
* @param object
* @return void
*/
private function _auto_connect($object) {
$this->gearman_host = array('127.0.0.1');
$this->gearman_port = array('4730');
foreach ($this->gearman_host as $key => $server) {
if (!$object->addServer($server, $this->gearman_port[$key])) {
$this->errors[] = "Gearman Library: Could not connect to the server named $key";
}
}
}
/**
* Returns worker error
*
* @access public
* @return void
*
*/
public function error() {
return $this->worker->error();
}
}