~keith-hughitt/helioviewer.org/2.0

« back to all changes in this revision

Viewing changes to api/lib/Resque.php

  • Committer: Keith Hughitt
  • Date: 2012-04-02 20:31:19 UTC
  • Revision ID: keith.hughitt@nasa.gov-20120402203119-po7ueah02jq539js
Helioviewer.orgĀ 2.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
require_once dirname(__FILE__) . '/Resque/Event.php';
 
3
require_once dirname(__FILE__) . '/Resque/Exception.php';
 
4
 
 
5
/**
 
6
 * Base Resque class.
 
7
 *
 
8
 * @package             Resque
 
9
 * @author              Chris Boulton <chris.boulton@interspire.com>
 
10
 * @copyright   (c) 2010 Chris Boulton
 
11
 * @license             http://www.opensource.org/licenses/mit-license.php
 
12
 */
 
13
class Resque
 
14
{
 
15
        const VERSION = '1.0';
 
16
 
 
17
        /**
 
18
         * @var Resque_Redis Instance of Resque_Redis that talks to redis.
 
19
         */
 
20
        public static $redis = null;
 
21
 
 
22
        /**
 
23
         * Given a host/port combination separated by a colon, set it as
 
24
         * the redis server that Resque will talk to.
 
25
         *
 
26
         * @param mixed $server Host/port combination separated by a colon, or
 
27
         * a nested array of servers with host/port pairs.
 
28
         */
 
29
        public static function setBackend($server, $database = 0)
 
30
        {
 
31
                if(is_array($server)) {
 
32
                        require_once dirname(__FILE__) . '/Resque/RedisCluster.php';
 
33
                        self::$redis = new Resque_RedisCluster($server);
 
34
                }
 
35
                else {
 
36
                        list($host, $port) = explode(':', $server);
 
37
                        require_once dirname(__FILE__) . '/Resque/Redis.php';
 
38
                        self::$redis = new Resque_Redis($host, $port);
 
39
                }
 
40
 
 
41
        self::redis()->select($database);
 
42
        }
 
43
 
 
44
        /**
 
45
         * Return an instance of the Resque_Redis class instantiated for Resque.
 
46
         *
 
47
         * @return Resque_Redis Instance of Resque_Redis.
 
48
         */
 
49
        public static function redis()
 
50
        {
 
51
                if(is_null(self::$redis)) {
 
52
                        self::setBackend('localhost:6379');
 
53
                }
 
54
 
 
55
                return self::$redis;
 
56
        }
 
57
 
 
58
        /**
 
59
         * Push a job to the end of a specific queue. If the queue does not
 
60
         * exist, then create it as well.
 
61
         *
 
62
         * @param string $queue The name of the queue to add the job to.
 
63
         * @param array $item Job description as an array to be JSON encoded.
 
64
         */
 
65
        public static function push($queue, $item)
 
66
        {
 
67
                self::redis()->sadd('queues', $queue);
 
68
                self::redis()->rpush('queue:' . $queue, json_encode($item));
 
69
        }
 
70
 
 
71
        /**
 
72
         * Pop an item off the end of the specified queue, decode it and
 
73
         * return it.
 
74
         *
 
75
         * @param string $queue The name of the queue to fetch an item from.
 
76
         * @return array Decoded item from the queue.
 
77
         */
 
78
        public static function pop($queue)
 
79
        {
 
80
                $item = self::redis()->lpop('queue:' . $queue);
 
81
                if(!$item) {
 
82
                        return;
 
83
                }
 
84
 
 
85
                return json_decode($item, true);
 
86
        }
 
87
 
 
88
        /**
 
89
         * Return the size (number of pending jobs) of the specified queue.
 
90
         *
 
91
         * @return int The size of the queue.
 
92
         */
 
93
        public static function size($queue)
 
94
        {
 
95
                return self::redis()->llen('queue:' . $queue);
 
96
        }
 
97
 
 
98
        /**
 
99
         * Create a new job and save it to the specified queue.
 
100
         *
 
101
         * @param string $queue The name of the queue to place the job in.
 
102
         * @param string $class The name of the class that contains the code to execute the job.
 
103
         * @param array $args Any optional arguments that should be passed when the job is executed.
 
104
         * @param boolean $monitor Set to true to be able to monitor the status of a job.
 
105
         */
 
106
        public static function enqueue($queue, $class, $args = null, $trackStatus = false)
 
107
        {
 
108
                require_once dirname(__FILE__) . '/Resque/Job.php';
 
109
                $result = Resque_Job::create($queue, $class, $args, $trackStatus);
 
110
                if ($result) {
 
111
                        Resque_Event::trigger('afterEnqueue', array(
 
112
                                'class' => $class,
 
113
                                'args' => $args,
 
114
                        ));
 
115
                }
 
116
 
 
117
                return $result;
 
118
        }
 
119
 
 
120
        /**
 
121
         * Reserve and return the next available job in the specified queue.
 
122
         *
 
123
         * @param string $queue Queue to fetch next available job from.
 
124
         * @return Resque_Job Instance of Resque_Job to be processed, false if none or error.
 
125
         */
 
126
        public static function reserve($queue)
 
127
        {
 
128
                require_once dirname(__FILE__) . '/Resque/Job.php';
 
129
                return Resque_Job::reserve($queue);
 
130
        }
 
131
 
 
132
        /**
 
133
         * Get an array of all known queues.
 
134
         *
 
135
         * @return array Array of queues.
 
136
         */
 
137
        public static function queues()
 
138
        {
 
139
                $queues = self::redis()->smembers('queues');
 
140
                if(!is_array($queues)) {
 
141
                        $queues = array();
 
142
                }
 
143
                return $queues;
 
144
        }
 
145
}