Created
December 10, 2013 14:21
-
-
Save tdg5/7891302 to your computer and use it in GitHub Desktop.
beanstalkd peek-oldest command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/dat.h b/dat.h | |
index 2570e76..ac6f6e7 100644 | |
--- a/dat.h | |
+++ b/dat.h | |
@@ -204,6 +204,9 @@ void job_free(job j); | |
/* Lookup a job by job ID */ | |
job job_find(uint64 job_id); | |
+/* Lookup oldest job */ | |
+job job_oldest(); | |
+ | |
/* the void* parameters are really job pointers */ | |
void job_setheappos(void*, int); | |
int job_pri_less(void*, void*); | |
diff --git a/job.c b/job.c | |
index f716fee..4262fa5 100644 | |
--- a/job.c | |
+++ b/job.c | |
@@ -4,6 +4,7 @@ | |
#include "dat.h" | |
static uint64 next_id = 1; | |
+static uint64 oldest_id = 1; | |
static int cur_prime = 0; | |
@@ -84,6 +85,16 @@ job_find(uint64 job_id) | |
} | |
job | |
+job_oldest() | |
+{ | |
+ job jh = NULL; | |
+ uint64 i; | |
+ for (i = oldest_id; i < next_id && !(jh = job_find(i)); i++); | |
+ oldest_id = jh ? jh->r.id : next_id; | |
+ return jh; | |
+} | |
+ | |
+job | |
allocate_job(int body_size) | |
{ | |
job j; | |
diff --git a/prot.c b/prot.c | |
index 1a725e3..51272c0 100644 | |
--- a/prot.c | |
+++ b/prot.c | |
@@ -28,6 +28,7 @@ size_t job_data_size_limit = JOB_DATA_SIZE_LIMIT_DEFAULT; | |
#define CMD_PEEK_READY "peek-ready" | |
#define CMD_PEEK_DELAYED "peek-delayed" | |
#define CMD_PEEK_BURIED "peek-buried" | |
+#define CMD_PEEK_OLDEST "peek-oldest" | |
#define CMD_RESERVE "reserve" | |
#define CMD_RESERVE_TIMEOUT "reserve-with-timeout " | |
#define CMD_DELETE "delete " | |
@@ -54,6 +55,7 @@ size_t job_data_size_limit = JOB_DATA_SIZE_LIMIT_DEFAULT; | |
#define CMD_PEEK_DELAYED_LEN CONSTSTRLEN(CMD_PEEK_DELAYED) | |
#define CMD_PEEK_BURIED_LEN CONSTSTRLEN(CMD_PEEK_BURIED) | |
#define CMD_PEEKJOB_LEN CONSTSTRLEN(CMD_PEEKJOB) | |
+#define CMD_PEEK_OLDEST_LEN CONSTSTRLEN(CMD_PEEK_OLDEST) | |
#define CMD_RESERVE_LEN CONSTSTRLEN(CMD_RESERVE) | |
#define CMD_RESERVE_TIMEOUT_LEN CONSTSTRLEN(CMD_RESERVE_TIMEOUT) | |
#define CMD_DELETE_LEN CONSTSTRLEN(CMD_DELETE) | |
@@ -136,7 +138,8 @@ size_t job_data_size_limit = JOB_DATA_SIZE_LIMIT_DEFAULT; | |
#define OP_QUIT 22 | |
#define OP_PAUSE_TUBE 23 | |
#define OP_JOBKICK 24 | |
-#define TOTAL_OPS 25 | |
+#define OP_PEEK_OLDEST 25 | |
+#define TOTAL_OPS 26 | |
#define STATS_FMT "---\n" \ | |
"current-jobs-urgent: %u\n" \ | |
@@ -273,6 +276,7 @@ static const char * op_names[] = { | |
CMD_QUIT, | |
CMD_PAUSE_TUBE, | |
CMD_JOBKICK, | |
+ CMD_PEEK_OLDEST, | |
}; | |
static job remove_buried_job(job j); | |
@@ -744,6 +748,7 @@ which_cmd(Conn *c) | |
TEST_CMD(c->cmd, CMD_PEEK_READY, OP_PEEK_READY); | |
TEST_CMD(c->cmd, CMD_PEEK_DELAYED, OP_PEEK_DELAYED); | |
TEST_CMD(c->cmd, CMD_PEEK_BURIED, OP_PEEK_BURIED); | |
+ TEST_CMD(c->cmd, CMD_PEEK_OLDEST, OP_PEEK_OLDEST); | |
TEST_CMD(c->cmd, CMD_RESERVE_TIMEOUT, OP_RESERVE_TIMEOUT); | |
TEST_CMD(c->cmd, CMD_RESERVE, OP_RESERVE); | |
TEST_CMD(c->cmd, CMD_DELETE, OP_DELETE); | |
@@ -1311,6 +1316,12 @@ dispatch_cmd(Conn *c) | |
reply_job(c, j, MSG_FOUND); | |
break; | |
+ case OP_PEEK_OLDEST: | |
+ j = job_oldest(); | |
+ if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD); | |
+ | |
+ reply_job(c, j, MSG_FOUND); | |
+ break; | |
case OP_RESERVE_TIMEOUT: | |
errno = 0; | |
timeout = strtol(c->cmd + CMD_RESERVE_TIMEOUT_LEN, &end_buf, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment