The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 06:48:32 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Queue processing for Kaptains  (Read 1534 times)
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« on: November 02, 2009, 12:31:40 PM »

I am putting this in the database board because i'm hoping i can use some mysql trickery to accomplish what i'm trying to do.

basically, i have a process that queues up jobs to run and another process that picks up jobs and executes them.  What i'd like to do is have a bunch of the job runners going to get the number of jobs running per minute higher.  The concern i have is the job runners picking up the same job from the queue table and running the job twice.

So, what kind of trickery can i use to make it so my little worker bees won't be running the same jobs multiple times?

Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: November 02, 2009, 12:49:24 PM »

Use a stored procedure.

The unique ID from a row (that represents a job) is what you want. If you're INNODB then you can just lock the row, if MyISAM then you'll need to lock the table, but in an SP it'd be lightening fast.

Essentially, your stored procedure would look like this (coming right out my a$$, haven't checked this):

procedure getOneRow()
BEGIN
   declare tempID integer;

   lock table mytable;
   select id into tempID from mytable where fulfilled = 0;
   update mytable set fulfilled=1 where id=tempID;
   unlock table mytable;
   select tempID;
END;


Then in PHP, particularly if you were using my dbConnection class, you would simply say:

$rowID $db->singleAnswer('call getOneRow()');

This procedure could then be called from any number of processes from anywhere and you'd always get exactly 1 row that was unique and not in use by anyone else.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #2 on: November 02, 2009, 01:19:39 PM »

that's sexy and exactly what i'm looking for.

thanks much!
Logged

We can't stop here, this is bat country.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!