Skip to content

Commit fbfd3bf

Browse files
committed
Add myjobs example
1 parent a8a6251 commit fbfd3bf

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class My_worker extends \yidas\queue\worker\Controller
159159
// ...
160160
```
161161

162-
> As above, `myjobs` library is defined by your own application which handles your job processes.
162+
> As above, `myjobs` library is defined by your own application which handles your job processes. [Example code of myjobs with Redis](https://github.com/yidas/codeigniter-queue-worker/blob/master/examples/myjobs/MyjobsWithRedis.php)
163163
164164
#### 2. Build Worker
165165

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* myjobs sample code using database for single worker
5+
*
6+
* This sample library uses database table records as a queue, we delete `exist()` and `popJob()`
7+
* methods and add a `getJob()` to implement the database characteristics for single worker usage.
8+
*
9+
* @author Nick Tsai <[email protected]>
10+
*/
11+
class MyjobsWithDatabase
12+
{
13+
function __construct()
14+
{
15+
$this->CI = & get_instance();
16+
$this->CI->load->model("job_queue_model");
17+
}
18+
19+
/**
20+
* Get a undo job from database table
21+
*
22+
* @return array
23+
*/
24+
public function getJob()
25+
{
26+
return $this->CI->job_queue_model->getJob();
27+
}
28+
29+
/**
30+
* Process then delete a job
31+
*
32+
* @param array $job
33+
* @return boolean
34+
*/
35+
public function processJob($job)
36+
{
37+
// Your own job process here
38+
39+
// Delete job record after finishing process
40+
return $this->CI->job_queue_model->deleteJob($job['id']);
41+
}
42+
}

examples/myjobs/MyjobsWithRedis.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* myjobs sample code using Redis
5+
*
6+
* This sample library mixed Redis client connection, which you can separate into components in
7+
* actual development.
8+
*
9+
* @author Nick Tsai <[email protected]>
10+
* @package https://github.com/nrk/predis
11+
*/
12+
class MyjobsWithRedis
13+
{
14+
/**
15+
* Redis key
16+
*/
17+
const QUEUE_KEY = 'your-job-key';
18+
19+
/**
20+
* Redis client
21+
*
22+
* @var Predis\Client
23+
*/
24+
protected $redisClient;
25+
26+
function __construct()
27+
{
28+
// You can store Redis config into application config, for example: `$this->CI->config->item('redis', 'services');`
29+
$this->redisClient = new Predis\Client(['host'=>'yourHostOrIP', 'scheme'=>'tcp', 'port'=>6379], ['parameters'=>['password'=>'yourpass']]);
30+
31+
// Connection check
32+
try {
33+
34+
$this->redisClient->type('test');
35+
36+
} catch (Predis\Connection\ConnectionException $e) {
37+
38+
if (ENVIRONMENT=='development') {
39+
throw $e;
40+
}
41+
// Prevent further error
42+
exit;
43+
}
44+
}
45+
46+
/**
47+
* Check if there are any jobs from Redis queue
48+
*
49+
* @return boolean
50+
*/
51+
public function exists()
52+
{
53+
return $this->redisClient->exists(self::QUEUE_KEY);
54+
}
55+
56+
/**
57+
* Pop up a job from Redis queue
58+
*
59+
* @return array
60+
*/
61+
public function popJob()
62+
{
63+
// Assume storing JSON string data in queue
64+
// Using LPOP or RPOP depends on your producer push
65+
$taskJSON = $this->redisClient->lpop(self::QUEUE_KEY);
66+
67+
return json_decode($taskJSON, true);
68+
}
69+
70+
/**
71+
* Process a job
72+
*
73+
* @param array $job
74+
* @return boolean
75+
*/
76+
public function processJob($job)
77+
{
78+
// Your own job process here
79+
80+
return true;
81+
}
82+
}

0 commit comments

Comments
 (0)