Index: branches/release-30/extlib/TheSchwartz.pm
===================================================================
--- branches/release-30/extlib/TheSchwartz.pm (revision 1104)
+++ branches/release-30/extlib/TheSchwartz.pm (revision 1406)
@@ -3,7 +3,7 @@
 package TheSchwartz;
 use strict;
-use fields qw( databases retry_seconds dead_dsns retry_at funcmap_cache verbose all_abilities current_abilities current_job cached_drivers driver_cache_expiration scoreboard );
-
-our $VERSION = "1.06";
+use fields qw( databases retry_seconds dead_dsns retry_at funcmap_cache verbose all_abilities current_abilities current_job cached_drivers driver_cache_expiration scoreboard prioritize );
+
+our $VERSION = "1.07";
 
 use Carp qw( croak );
@@ -36,4 +36,5 @@
 
     $client->{retry_seconds} = delete $args{retry_seconds} || RETRY_DEFAULT;
+    $client->set_prioritize(delete $args{prioritize});
     $client->set_verbose(delete $args{verbose});
     $client->set_scoreboard(delete $args{scoreboard});
@@ -167,10 +168,17 @@
                 funcid        => $funcid,
                 @options
-                }, { limit => $limit });
+                }, { limit => $limit,
+                    ( $client->prioritize ? ( sort => 'priority',
+                    direction => 'descend' ) : () )
+                });
         } else {
             push @jobs, $driver->search('TheSchwartz::Job' => {
                 funcid        => $funcid,
                 @options
-                }, { limit => $limit });
+                }, { limit => $limit,
+                    ( $client->prioritize ? ( sort => 'priority',
+                        direction => 'descend' ) : () )
+                }
+            );
         }
     }
@@ -215,5 +223,9 @@
                     grabbed_until => \ "<= $unixtime",
                     coalesce      => { op => $op, value => $coval },
-                }, { limit => $FIND_JOB_BATCH_SIZE });
+                }, { limit => $FIND_JOB_BATCH_SIZE,
+                    ( $client->prioritize ? ( sort => 'priority',
+                        direction => 'descend' ) : () )
+                }
+            );
         };
         if ($@) {
@@ -254,5 +266,9 @@
                     run_after     => \ "<= $unixtime",
                     grabbed_until => \ "<= $unixtime",
-                }, { limit => $FIND_JOB_BATCH_SIZE });
+                }, { limit => $FIND_JOB_BATCH_SIZE,
+                    ( $client->prioritize ? ( sort => 'priority',
+                    direction => 'descend' ) : () )
+                }
+            );
         };
         if ($@) {
@@ -504,5 +520,6 @@
     my $class = $job ? $job->funcname : undef;
     if ($job) {
-        $job->debug("TheSchwartz::work_once got job of class '$class'");
+        my $priority = $job->priority ? ", priority " . $job->priority : "";
+        $job->debug("TheSchwartz::work_once got job of class '$class'$priority");
     } else {
         $client->debug("TheSchwartz::work_once found no jobs");
@@ -682,4 +699,14 @@
 }
 
+sub prioritize {
+    my TheSchwartz $client = shift;
+    return $client->{prioritize};
+}
+
+sub set_prioritize {
+    my TheSchwartz $client = shift;
+    $client->{prioritize} = shift;
+}
+
 # current job being worked.  so if something dies, work_safely knows which to mark as dead.
 sub current_job {
@@ -806,4 +833,10 @@
 messages will not be logged.
 
+=item * C<prioritize>
+
+A value indicating whether to utilize the job 'priority' field when selecting
+jobs to be processed. If unspecified, jobs will always be executed in a
+randomized order.
+
 =item * C<driver_cache_expiration>
 
Index: branches/release-30/extlib/TheSchwartz/FuncMap.pm
===================================================================
--- branches/release-30/extlib/TheSchwartz/FuncMap.pm (revision 1098)
+++ branches/release-30/extlib/TheSchwartz/FuncMap.pm (revision 1406)
@@ -17,7 +17,14 @@
     my($driver, $funcname) = @_;
 
+    ## Attempt to select funcmap record by name. If successful, return
+    ## object, otherwise proceed with insertion and return.
+    my ($map) = $driver->search('TheSchwartz::FuncMap' =>
+            { funcname => $funcname }
+        );
+    return $map if $map;
+
     ## Attempt to insert a new funcmap row. Since the funcname column is
     ## UNIQUE, if the row already exists, an exception will be thrown.
-    my $map = $class->new;
+    $map = $class->new;
     $map->funcname($funcname);
     eval { $driver->insert($map) };
