Skip to content

Commit

Permalink
增加JSON方式查看队列状态,方便客服端程序处理。支持实时修改定时更新内存内容到磁盘的间隔时间。
Browse files Browse the repository at this point in the history
  • Loading branch information
blog.s135.com committed Jun 21, 2010
1 parent 75af193 commit b315104
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Makefile for httpsqs
CC=gcc
CFLAGS=-O2 -Wall -levent -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc
CFLAGS=-O2 -Wall -levent -ltokyocabinet -lz -lrt -lpthread -lm -lc

httpsqs: httpsqs.c
$(CC) $(CFLAGS) httpsqs.c -o httpsqs
Expand Down
56 changes: 44 additions & 12 deletions httpsqs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HTTP Simple Queue Service - httpsqs v1.3.20100617
HTTP Simple Queue Service - httpsqs v1.3.20100621
Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]
This is free software, and you are welcome to modify and redistribute it under the New BSD License
*/
Expand Down Expand Up @@ -41,7 +41,6 @@ This is free software, and you are welcome to modify and redistribute it under t
/* 全局设置 */
TCBDB *httpsqs_db_tcbdb; /* 数据表 */
int httpsqs_settings_syncinterval; /* 同步更新内容到磁盘的间隔时间 */
char *httpsqs_settings_keepalive; /* Keep-Alive 时间 */
char *httpsqs_settings_pidfile; /* PID文件 */

/* 创建多层目录的函数 */
Expand Down Expand Up @@ -117,15 +116,14 @@ char *urldecode(char *input_str)
static void show_help(void)
{
char *b = "--------------------------------------------------------------------------------------------------\n"
"HTTP Simple Queue Service - httpsqs v" VERSION " (June 19, 2010)\n\n"
"HTTP Simple Queue Service - httpsqs v" VERSION " (June 22, 2010)\n\n"
"Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]\n"
"This is free software, and you are welcome to modify and redistribute it under the New BSD License\n"
"\n"
"-l <ip_addr> interface to listen on, default is 0.0.0.0\n"
"-p <num> TCP port number to listen on (default: 1218)\n"
"-x <path> database directory (example: /opt/httpsqs/data)\n"
"-t <second> timeout for an http request (default: 3)\n"
"-k <second> keep-alive time (default: 15)\n"
"-s <second> the interval to sync updated contents to the disk (default: 5)\n"
"-c <num> the maximum number of non-leaf nodes to be cached (default: 10000)\n"
"-m <size> database memory cache size in MB (default: 100)\n"
Expand Down Expand Up @@ -271,6 +269,15 @@ char *httpsqs_view(const char* httpsqs_input_name, int pos)
return queue_value;
}

/* 修改定时更新内存内容到磁盘的间隔时间,返回间隔时间(秒) */
static int httpsqs_synctime(int httpsqs_input_num)
{
if (httpsqs_input_num >= 1) {
httpsqs_settings_syncinterval = httpsqs_input_num;
}
return httpsqs_settings_syncinterval;
}

/* 获取本次“入队列”操作的队列写入点 */
static int httpsqs_now_putpos(const char* httpsqs_input_name)
{
Expand Down Expand Up @@ -401,7 +408,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
} else {
evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
}
evhttp_add_header(req->output_headers, "Keep-Alive", httpsqs_settings_keepalive);
evhttp_add_header(req->output_headers, "Connection", "keep-alive");
evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
//evhttp_add_header(req->output_headers, "Cneonction", "close");

Expand Down Expand Up @@ -483,7 +490,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
}
}
}
/* 查看队列状态 */
/* 查看队列状态(普通浏览方式) */
else if (strcmp(httpsqs_input_opt, "status") == 0) {
int maxqueue = httpsqs_read_maxqueue((char *)httpsqs_input_name); /* 最大队列数量 */
int putpos = httpsqs_read_putpos((char *)httpsqs_input_name); /* 入队列写入位置 */
Expand All @@ -508,6 +515,25 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
evbuffer_add_printf(buf, "Get position of queue (%s): %d\n", get_times, getpos);
evbuffer_add_printf(buf, "Number of unread queue: %d\n", ungetnum);
}
/* 查看队列状态(JSON方式,方便客服端程序处理) */
else if (strcmp(httpsqs_input_opt, "status_json") == 0) {
int maxqueue = httpsqs_read_maxqueue((char *)httpsqs_input_name); /* 最大队列数量 */
int putpos = httpsqs_read_putpos((char *)httpsqs_input_name); /* 入队列写入位置 */
int getpos = httpsqs_read_getpos((char *)httpsqs_input_name); /* 出队列读取位置 */
int ungetnum;
const char *put_times;
const char *get_times;
if (putpos >= getpos) {
ungetnum = abs(putpos - getpos); /* 尚未出队列条数 */
put_times = "1";
get_times = "1";
} else if (putpos < getpos) {
ungetnum = abs(maxqueue - getpos + putpos); /* 尚未出队列条数 */
put_times = "2";
get_times = "1";
}
evbuffer_add_printf(buf, "{\"name\":\"%s\",\"maxqueue\":%d,\"putpos\":%d,\"puttimes\":%s,\"getpos\":%d,\"gettimes\":%s,\"unread\":%d}\n", httpsqs_input_name, maxqueue, putpos, put_times, getpos, get_times, ungetnum);
}
/* 查看单条队列内容 */
else if (strcmp(httpsqs_input_opt, "view") == 0 && httpsqs_input_pos >= 1 && httpsqs_input_pos <= 1000000000) {
char *httpsqs_output_value;
Expand Down Expand Up @@ -535,6 +561,16 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
/* 设置取消 */
evbuffer_add_printf(buf, "%s", "HTTPSQS_MAXQUEUE_CANCEL");
}
}
/* 设置定时更新内存内容到磁盘的间隔时间,最小值为1秒,最大值为10亿秒 */
else if (strcmp(httpsqs_input_opt, "synctime") == 0 && httpsqs_input_num >= 1 && httpsqs_input_num <= 1000000000) {
if (httpsqs_synctime(httpsqs_input_num) >= 1) {
/* 设置成功 */
evbuffer_add_printf(buf, "%s", "HTTPSQS_SYNCTIME_OK");
} else {
/* 设置取消 */
evbuffer_add_printf(buf, "%s", "HTTPSQS_SYNCTIME_CANCEL");
}
} else {
/* 命令错误 */
evbuffer_add_printf(buf, "%s", "HTTPSQS_ERROR");
Expand Down Expand Up @@ -581,15 +617,14 @@ int main(int argc, char **argv)
char *httpsqs_settings_datapath = NULL;
bool httpsqs_settings_daemon = false;
int httpsqs_settings_timeout = 3; /* 单位:秒 */
httpsqs_settings_keepalive = "15"; /* 单位:秒 */
httpsqs_settings_syncinterval = 5; /* 单位:秒 */
int httpsqs_settings_cachenonleaf = 10000; /* 缓存非叶子节点数。单位:条 */
int httpsqs_settings_cacheleaf = 20000; /* 缓存叶子节点数。叶子节点缓存数为非叶子节点数的两倍。单位:条 */
int httpsqs_settings_mappedmemory = 104857600; /* 单位:字节 */
httpsqs_settings_pidfile = "/tmp/httpsqs.pid";

/* process arguments */
while ((c = getopt(argc, argv, "l:p:x:t:k:s:c:m:i:dh")) != -1) {
while ((c = getopt(argc, argv, "l:p:x:t:s:c:m:i:dh")) != -1) {
switch (c) {
case 'l':
httpsqs_settings_listen = strdup(optarg);
Expand All @@ -613,10 +648,7 @@ int main(int argc, char **argv)
break;
case 't':
httpsqs_settings_timeout = atoi(optarg);
break;
case 'k':
httpsqs_settings_keepalive = strdup(optarg);
break;
break;
case 's':
httpsqs_settings_syncinterval = atoi(optarg);
break;
Expand Down

0 comments on commit b315104

Please sign in to comment.