Issues (63)

LibGearman.php (2 issues)

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