Permalink
Browse files

Adding status.php and some improvements to worker and creator

  • Loading branch information...
1 parent cea4a4e commit 7a2f910318919718452fc7239fb6b08ba6a93ca4 @JustinCarmony committed Jan 10, 2012
Showing with 105 additions and 0 deletions.
  1. +91 −0 status.php
  2. +14 −0 worker.php
View
@@ -0,0 +1,91 @@
+<?php
+
+/*
+ * Simple CLI Script to Monitor Your Workers & Queues
+ *
+ * Usage: php status.php <delay in seconds>
+ * Example: php status.php 5
+ *
+ */
+
+echo "Creator Script Starting...\n";
+
+require 'config.php';
+
+$predis = new Predis\Client(array(
+ 'scheme' => 'tcp',
+ 'host' => REDIS_HOST,
+ 'port' => REDIS_PORT,
+));
+
+echo "Connected to Redis Server \n";
+
+$delay = $argv[1]; // Get how many jobs to create
+
+if(!$delay)
+{
+ $delay = 3; // Default delay of 3 seconds.
+}
+
+// Loop indefinitely
+while(1)
+{
+ // Get the status
+ $queue_lengths = array();
+ $queue_lengths['high'] = $predis->llen('queue.priority.high');
+ $queue_lengths['normal'] = $predis->llen('queue.priority.normal');
+ $queue_lengths['low'] = $predis->llen('queue.priority.low');
+
+ $queue_total = 0;
+ // Change null values to 0's
+ foreach($queue_lengths as $name => $size)
+ {
+ if($size == null)
+ {
+ $queue_lengths[$name] = 0;
+ }
+
+ $queue_total += $queue_lengths[$name];
+ }
+
+ // Trim out old workers that haven't "worked" in over an hour
+ $workers_time = $predis->hgetall('worker.status.last_time');
+ $time_limit = time() - 60 * 60 * 1;
+
+ foreach($workers_time as $worker_id => $worker_ts)
+ {
+ if($worker_ts < $time_limit)
+ {
+ $predis->hdel('worker.status', $worker_id);
+ $predis->hdel('worker.status.last_time', $worker_id);
+ }
+ }
+
+ $workers = $predis->hgetall('worker.status');
+ ksort($workers);
+
+ // Display Queue status
+ echo "\n----------------------\n";
+ echo "Queue Statuses:\n\n";
+ echo " High: ".$queue_lengths['high']."\n";
+ echo " Normal: ".$queue_lengths['normal']."\n";
+ echo " Low: ".$queue_lengths['low']."\n\n";
+ echo " Total: ".$queue_total."\n\n";
+
+ echo "Worker Statuses:\n\n";
+
+ foreach($workers as $worker_id => $status)
+ {
+ if($worker_id > 0)
+ {
+ echo " Worker [$worker_id]: $status \n";
+ }
+ }
+
+ echo "----------------------\n";
+
+ // Sleep the delay
+ sleep($delay);
+}
+
+
View
@@ -6,6 +6,11 @@
$worker_id = $argv[1];
+if(!$worker_id)
+{
+ $worker_id = rand(100, 999);
+}
+
echo "Worker [$worker_id] Starting...\n";
require 'config.php';
@@ -20,6 +25,7 @@
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Started');
+$predis->hset('worker.status.last_time', $worker_id, time());
/*
* We'll set our base time, which is one hour (in seconds).
@@ -43,6 +49,7 @@
{
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Waiting');
+ $predis->hset('worker.status.last_time', $worker_id, time());
// Check to see if there are any items in the queues in
// order of priority. If all are empty, wait up to 10
@@ -55,9 +62,15 @@
// If a job was pulled out
if($job)
{
+ /*
+ * Start Working
+ *
+ * This is where you will use the data from the
+ */
echo "\nJob Started ";
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Working');
+ $predis->hset('worker.status.last_time', $worker_id, time());
$queue_name = $job[0]; // 0 is the name of the queue
$details = json_decode($job[1]); // parse the json data from the job
@@ -92,5 +105,6 @@
// Setting the Worker's Status
$predis->hset('worker.status', $worker_id, 'Closed');
+$predis->hset('worker.status.last_time', $worker_id, time());
echo "\n\nWorker [$worker_id] Finished! \nGoodbye...\n";

0 comments on commit 7a2f910

Please sign in to comment.