├── .gitignore ├── .travis.yml ├── Makefile ├── README.md ├── dist.ini ├── lib └── resty │ └── beanstalkd.lua ├── rockspec └── lua-resty-beanstalkd-0.0-5.rockspec ├── t ├── 01-version.t ├── 011-watch.t ├── 012-ignore.t ├── 02-put.t ├── 03-reserve.t ├── 031-peek.t ├── 032-delete.t ├── 04-release.t ├── 05-bury.t ├── 06-kick.t ├── 061-kick-job.t ├── 071-stats.t ├── 072-list-tubes.t ├── 073-stats-tubes.t ├── 074-stats-job.t ├── 08-pause-tube.t └── 09-touch.t └── util └── build.sh /.gitignore: -------------------------------------------------------------------------------- 1 | wp 2 | *.swo 3 | *~ 4 | go 5 | t/servroot/ 6 | reindex 7 | *.t_ 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | notifications: 2 | email: false 3 | 4 | sudo: required 5 | dist: trusty 6 | 7 | os: linux 8 | 9 | language: c 10 | 11 | compiler: 12 | - gcc 13 | 14 | before_install: 15 | - sudo apt-get -qq update 16 | - sudo apt-get install -y beanstalkd cpanminus 17 | - sudo cpanm Test::Nginx 18 | 19 | cache: 20 | directories: 21 | - download-cache 22 | 23 | env: 24 | global: 25 | - JOBS=3 26 | - TEST_NGINX_SLEEP=0.5 27 | matrix: 28 | - OR_VERSION=1.11.2.2 29 | 30 | services: 31 | - beanstalkd 32 | 33 | install: 34 | - if [ ! -f download-cache/openresty-$OR_VERSION.tar.gz ]; then wget -P download-cache http://openresty.org/download/openresty-$OR_VERSION.tar.gz; fi 35 | 36 | script: 37 | - tar -xvf download-cache/openresty-$OR_VERSION.tar.gz 38 | - cd openresty-$OR_VERSION/ 39 | - ./configure -j2 --prefix=/usr/local/openresty 40 | - make -j2 41 | - sudo make install 42 | - cd .. 43 | - export PATH=/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin:$PATH 44 | - nginx -V 45 | - prove -r t 46 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OPENRESTY_PREFIX=/usr/local/openresty-debug 2 | 3 | PREFIX ?= /usr/local 4 | LUA_INCLUDE_DIR ?= $(PREFIX)/include 5 | LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION) 6 | INSTALL ?= install 7 | 8 | .PHONY: all test install 9 | 10 | all: ; 11 | 12 | install: all 13 | $(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/resty 14 | $(INSTALL) lib/resty/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/resty 15 | 16 | test: all 17 | PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Name 2 | ==== 3 | 4 | lua-resty-beanstalkd - non-blocking beanstalkd lib for ngx_lua. 5 | 6 | [![Build Status](https://api.travis-ci.org/smallfish/lua-resty-beanstalkd.png)](https://travis-ci.org/smallfish/lua-resty-beanstalkd) 7 | 8 | 9 | Status 10 | ====== 11 | 12 | This library is considered experimental and still under active development. 13 | 14 | The API is still in flux and may change without notice. 15 | 16 | Description 17 | =========== 18 | 19 | This library requires an nginx, the [ngx_lua module](http://wiki.nginx.org/HttpLuaModule) 20 | 21 | Commands 22 | ======== 23 | 24 | All beanstalkd commands are supported, following the name convention below: 25 | * The command name is the method name, with `-` replaced by `_`. 26 | * The arguments of such method are the arguments of corresponding command. 27 | For instance, call `bean:stats_tube(tube)` is equal to send `stats-tube tube` to beanstalkd. 28 | 29 | For more details, please see the below Synopsis section. 30 | 31 | Synopsis 32 | ======== 33 | 34 | ```nginx 35 | lua_package_path "/path/to/lua-resty-beanstalkd/lib/?.lua;;"; 36 | 37 | server { 38 | location /test { 39 | 40 | content_by_lua_block { 41 | local beanstalkd = require 'resty.beanstalkd' 42 | 43 | -- new and connect 44 | local bean, err = beanstalkd:new() 45 | if not bean then 46 | ngx.say("failed to init beanstalkd:", err) 47 | return 48 | end 49 | ngx.say("initialized ok") 50 | 51 | local ok, err = bean:connect() 52 | if not ok then 53 | ngx.say("failed to connect beanstalkd:", err) 54 | return 55 | end 56 | ngx.say("connect ok") 57 | 58 | -- use tube 59 | local ok, err = bean:use("smallfish") 60 | if not ok then 61 | ngx.say("failed to use tube:", err) 62 | end 63 | ngx.say("use smallfish tube ok") 64 | 65 | -- put job 66 | local id, err = bean:put("hello") 67 | if not id then 68 | ngx.say("failed to put hello to smallfish tube, error:", err) 69 | end 70 | ngx.say("put hello to smallfish tube, id:", id) 71 | 72 | -- watch tube 73 | local ok, err = bean:watch("smallfish") 74 | if not ok then 75 | ngx.say("failed to watch tube smallfish, error:", err) 76 | return 77 | end 78 | ngx.say("watch smallfish tube ok, tube size:", ok) 79 | 80 | -- reserve job,with optional timeout in seconds. reserve(timeout?) 81 | local id, data = bean:reserve() 82 | if not id then 83 | ngx.say("reserve hello failed, error:", id, data) 84 | else 85 | ngx.say("reserve hello ok, id:", id, "data:", data) 86 | end 87 | 88 | -- release job 89 | local ok, err = bean:release(id) 90 | if not ok then 91 | ngx.say("failed to release, id:", id, " error:", err) 92 | else 93 | ngx.say("release ok, id:", id) 94 | end 95 | 96 | local id, data = bean:reserve() 97 | if not id then 98 | ngx.say("reserve hello failed, error:", id, data) 99 | else 100 | ngx.say("reserve hello ok, id:", id, " data:", data) 101 | end 102 | 103 | -- peek job 104 | local id, data = bean:peek(id) 105 | if not id then 106 | ngx.say("peek failed, id not found") 107 | else 108 | ngx.say("peek ok, data:", data) 109 | end 110 | 111 | -- bury job 112 | local ok, err = bean:bury(id) 113 | if not ok then 114 | ngx.say("bury failed, id:", id, " error:", err) 115 | else 116 | ngx.say("bury ok, id:", id) 117 | end 118 | 119 | -- kick job 120 | local count, err = bean:kick(1) 121 | if not count then 122 | ngx.say("kick failed, error:", err) 123 | else 124 | ngx.say("kick ok, count:", count) 125 | end 126 | 127 | local id, data = bean:reserve() 128 | if not id then 129 | ngx.say("reserve hello failed, error:", id, data) 130 | else 131 | ngx.say("reserve hello ok, id:", id, " data:", data) 132 | end 133 | 134 | -- delete job 135 | local ok, err = bean:delete(id) 136 | if ok then 137 | ngx.say("delete ok, id:", id) 138 | else 139 | ngx.say("delete failed, id:", id, ok, err) 140 | end 141 | 142 | -- put it into the connection pool of size 100, 143 | -- with 0 idle timeout 144 | 145 | bean:set_keepalive(0, 100) 146 | 147 | -- close and quit beanstalkd 148 | -- bean:close() 149 | } 150 | 151 | } 152 | } 153 | ``` 154 | 155 | Author 156 | ====== 157 | 158 | Chen "smallfish" Xiaoyu (陈小玉) 159 | 160 | Copyright and License 161 | ===================== 162 | 163 | This module is licensed under the BSD license. 164 | 165 | Copyright (C) 2012, by Chen "smallfish" Xiaoyu (陈小玉) 166 | 167 | Portions of the code are from [lua-resty-memcached](https://github.com/agentzh/lua-resty-memcached) Copyright (C) 2012, by Zhang "agentzh" Yichun (章亦春) . 168 | 169 | All rights reserved. 170 | 171 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 172 | 173 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 174 | 175 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 176 | 177 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 178 | 179 | See Also 180 | ======== 181 | * [ngx_lua module](http://wiki.nginx.org/HttpLuaModule) 182 | * [beanstalkd protocol specification](https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt) 183 | * [lua-resty-memcached](https://github.com/agentzh/lua-resty-memcached) 184 | * [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) 185 | * [lua-resty-mysql](https://github.com/agentzh/lua-resty-mysql) 186 | -------------------------------------------------------------------------------- /dist.ini: -------------------------------------------------------------------------------- 1 | name = lua-resty-beanstalkd 2 | abstract = =Lua beastalkd client driver for the ngx_lua based on the cosocket API 3 | author = smallfish 4 | is_original = yes 5 | license = 2bsd 6 | repo_link = https://github.com/smallfish/lua-resty-beanstalkd 7 | lib_dir = lib 8 | doc_dir = lib 9 | version = 0.0.5 10 | main_module = lib/resty/beanstalkd.lua 11 | -------------------------------------------------------------------------------- /lib/resty/beanstalkd.lua: -------------------------------------------------------------------------------- 1 | -- Copyright (C) 2012 Chen "smallfish" Xiaoyu (陈小玉) 2 | 3 | local tcp = ngx.socket.tcp 4 | local strlen = string.len 5 | local strsub = string.sub 6 | local strmatch = string.match 7 | local tabconcat = table.concat 8 | 9 | local _M = {} 10 | 11 | _M.VERSION = "0.0.5" 12 | 13 | local mt = { 14 | __index = _M, 15 | -- to prevent use of casual module global variables 16 | __newindex = function(table, key, val) 17 | error('attempt to write to undeclared variable "' .. key .. '"') 18 | end, 19 | } 20 | 21 | function _M.new(self) 22 | local sock, err = tcp() 23 | if not sock then 24 | return nil, err 25 | end 26 | return setmetatable({sock = sock}, mt) 27 | end 28 | 29 | function _M.set_timeout(self, timeout) 30 | local sock = self.sock 31 | if not sock then 32 | return nil, "not initialized" 33 | end 34 | return sock:settimeout(timeout) 35 | end 36 | 37 | function _M.set_keepalive(self, ...) 38 | local sock = self.sock 39 | if not sock then 40 | return nil, "not initialized" 41 | end 42 | return sock:setkeepalive(...) 43 | end 44 | 45 | function _M.getreusedtimes(self) 46 | local sock = self.sock 47 | if not sock then 48 | return nil, "not initialized" 49 | end 50 | return sock:getreusedtimes() 51 | end 52 | 53 | function _M.connect(self, host, port, ...) 54 | local sock = self.sock 55 | if not sock then 56 | return nil, "not initialized" 57 | end 58 | host = host or "127.0.0.1" 59 | port = port or 11300 60 | return sock:connect(host, port, ...) 61 | end 62 | 63 | function _M.use(self, tube) 64 | local sock = self.sock 65 | if not sock then 66 | return nil, "not initialized" 67 | end 68 | local cmd = {"use", " ", tube, "\r\n"} 69 | local bytes, err = sock:send(tabconcat(cmd)) 70 | if not bytes then 71 | return nil, "failed to use tube, send data error: " .. err 72 | end 73 | local line, err = sock:receive() 74 | if not line then 75 | return nil, "failed to use tube, receive data error: " .. err 76 | end 77 | return line 78 | end 79 | 80 | function _M.watch(self, tube) 81 | local sock = self.sock 82 | if not sock then 83 | return nil, "not initialized" 84 | end 85 | local cmd = {"watch", " ", tube, "\r\n"} 86 | local bytes, err = sock:send(tabconcat(cmd)) 87 | if not bytes then 88 | return nil, "failed to watch tube, send data error: " .. err 89 | end 90 | local line, err = sock:receive() 91 | if not line then 92 | return nil, "failed to watch tube, receive data error: " .. err 93 | end 94 | local size = strmatch(line, "^WATCHING (%d+)$") 95 | if size then 96 | return size, line 97 | end 98 | return nil, line 99 | end 100 | 101 | function _M.ignore(self, tube) 102 | local sock = self.sock 103 | if not sock then 104 | return nil, "not initialized" 105 | end 106 | local cmd = {"ignore", " ", tube, "\r\n"} 107 | local bytes, err = sock:send(tabconcat(cmd)) 108 | if not bytes then 109 | return nil, "failed to ignore tube, send data error: " .. err 110 | end 111 | local line, err = sock:receive() 112 | if not line then 113 | return nil, "failed to ignore tube, receive data error: " .. err 114 | end 115 | local size = strmatch(line, "^WATCHING (%d+)$") 116 | if size then 117 | return size, line 118 | end 119 | return nil, line 120 | end 121 | 122 | function _M.put(self, body, pri, delay, ttr) 123 | local sock = self.sock 124 | if not sock then 125 | return nil, "not initialized" 126 | end 127 | pri = pri or 2 ^ 32 128 | delay = delay or 0 129 | ttr = ttr or 120 130 | local cmd = {"put", " ", pri, " ", delay, " ", ttr, " ", strlen(body), "\r\n", body, "\r\n"} 131 | local bytes, err = sock:send(tabconcat(cmd)) 132 | if not bytes then 133 | return nil, "failed to put, send data error: " .. err 134 | end 135 | local line, err = sock:receive() 136 | if not line then 137 | return nil, "failed to put, receive data error:" .. err 138 | end 139 | local id = strmatch(line, " (%d+)$") 140 | if id then 141 | return id, line 142 | end 143 | return nil, line 144 | end 145 | 146 | function _M.delete(self, id) 147 | local sock = self.sock 148 | if not sock then 149 | return nil, "not initialized" 150 | end 151 | local cmd = {"delete", " ", id, "\r\n"} 152 | local bytes, err = sock:send(tabconcat(cmd)) 153 | if not bytes then 154 | return nil, "failed to delete, send data error: " .. err 155 | end 156 | local line, err = sock:receive() 157 | if not line then 158 | return nil, "failed to delete, receive data error: " .. err 159 | end 160 | if line == "DELETED" then 161 | return true, line 162 | end 163 | return false, line 164 | end 165 | 166 | function _M.reserve(self, timeout) 167 | --Reserve a job from one of the watched tubes, with optional timeout in seconds. 168 | local sock = self.sock 169 | local cmd = {"reserve", "\r\n"} 170 | if timeout then 171 | cmd = {"reserve-with-timeout", " ", timeout, "\r\n"} 172 | end 173 | local bytes, err = sock:send(tabconcat(cmd)) 174 | if not bytes then 175 | return nil, "failed to reserve, send data error: " .. err 176 | end 177 | local line, err = sock:receive() 178 | if not line then 179 | return nil, "failed to reserve, receive data error: " .. err 180 | end 181 | local id, size = strmatch(line, "^RESERVED (%d+) (%d+)$") 182 | if id and size then -- remove \r\n 183 | local data, err = sock:receive(size+2) 184 | return id, strsub(data, 1, -3) 185 | end 186 | return false, line 187 | end 188 | 189 | function _M.release(self, id, pri, delay) 190 | local sock = self.sock 191 | if not sock then 192 | return nil, "not initialized" 193 | end 194 | pri = pri or 2 ^ 32 195 | delay = delay or 0 196 | local cmd = {"release", " ", id, " ", pri, " ", delay, "\r\n"} 197 | local bytes, err = sock:send(tabconcat(cmd)) 198 | if not bytes then 199 | return nil, "failed to release, send data error: " .. err 200 | end 201 | local line, err = sock:receive() 202 | if not line then 203 | return nil, "failed to release, receive data error: " .. err 204 | end 205 | if line == "RELEASED" then 206 | return true, line 207 | end 208 | return false, line 209 | end 210 | 211 | function _M.bury(self, id, pri) 212 | local sock = self.sock 213 | if not sock then 214 | return nil, "not initialized" 215 | end 216 | pri = pri or 2 ^ 32 217 | local cmd = {"bury", " ", id, " ", pri, "\r\n"} 218 | local bytes, err = sock:send(tabconcat(cmd)) 219 | if not bytes then 220 | return nil, "failed to release, send data error: " .. err 221 | end 222 | local line, err = sock:receive() 223 | if not line then 224 | return nil, "failed to release, receive data error: " .. err 225 | end 226 | if line == "BURIED" then 227 | return true, line 228 | end 229 | return false, line 230 | end 231 | 232 | function _M.kick(self, bound) 233 | local sock = self.sock 234 | if not sock then 235 | return nil, "not initialized" 236 | end 237 | local cmd = {"kick", " ", bound, "\r\n"} 238 | local bytes, err = sock:send(tabconcat(cmd)) 239 | if not bytes then 240 | return nil, "failed to release, send data error: " .. err 241 | end 242 | local line, err = sock:receive() 243 | if not line then 244 | return nil, "failed to release, receive data error: " .. err 245 | end 246 | local count = strmatch(line, "^KICKED (%d+)$") 247 | return count, nil 248 | end 249 | 250 | function _M.kick_job(self, id) 251 | local sock = self.sock 252 | if not sock then 253 | return nil, "not initialized" 254 | end 255 | local cmd = {"kick-job", " ", id, "\r\n"} 256 | local bytes, err = sock:send(tabconcat(cmd)) 257 | if not bytes then 258 | return nil, "failed to release, send data error: " .. err 259 | end 260 | local line 261 | line, err = sock:receive() 262 | if not line then 263 | return nil, "failed to release, receive data error: " .. err 264 | end 265 | if line == "KICKED" then 266 | return true, nil 267 | end 268 | return false, line 269 | end 270 | 271 | function _M.touch(self, id) 272 | local sock = self.sock 273 | if not sock then 274 | return nil, "not initialized" 275 | end 276 | local cmd = {"touch", " ", id, "\r\n"} 277 | local bytes, err = sock:send(tabconcat(cmd)) 278 | if not bytes then 279 | return nil, "failed to release, send data error: " .. err 280 | end 281 | local line 282 | line, err = sock:receive() 283 | if not line then 284 | return nil, "failed to release, receive data error: " .. err 285 | end 286 | if line == 'TOUCHED' then 287 | return true, nil 288 | end 289 | return false, line 290 | end 291 | 292 | function _M.pause_tube(self, tube, delay) 293 | if not tube then 294 | return nil, "invalid tube name, please check your input" 295 | end 296 | -- beanstalkd will increase delay 0 to 1 297 | if not delay or delay < 0 then 298 | return nil, "invalid delay, please check your input" 299 | end 300 | local sock = self.sock 301 | if not sock then 302 | return nil, "not initialized" 303 | end 304 | 305 | local cmd = {"pause-tube", " ", tube, " ", delay, "\r\n"} 306 | local bytes, err = sock:send(tabconcat(cmd)) 307 | if not bytes then 308 | return nil, "failed to pause-tube, send data error: " .. err 309 | end 310 | local line 311 | line, err = sock:receive() 312 | if not line then 313 | return nil, "failed to pause-tube, receive data error: " .. err 314 | end 315 | if line == 'PAUSED' then 316 | return true, nil 317 | end 318 | return false, line 319 | end 320 | 321 | function _M.peek(self, id) 322 | local sock = self.sock 323 | local cmd = {"peek", " ", id, "\r\n"} 324 | local bytes, err = sock:send(tabconcat(cmd)) 325 | if not bytes then 326 | return nil, "failed to peek, send data error: " .. err 327 | end 328 | local line, err = sock:receive() 329 | if not line then 330 | return nil, "failed to peek, receive data error: " .. err 331 | end 332 | local id, size = strmatch(line, "^FOUND (%d+) (%d+)$") 333 | if id and size then -- remove \r\n 334 | local data, err = sock:receive(size+2) 335 | return id, strsub(data, 1, -3) 336 | end 337 | return false, line 338 | end 339 | 340 | 341 | local function _peek_job_type(self, job_type) 342 | local sock = self.sock 343 | local cmd = {job_type, "\r\n"} 344 | local bytes, err = sock:send(tabconcat(cmd)) 345 | if not bytes then 346 | return nil, "failed to " .. job_type .. ", send data error: " .. err 347 | end 348 | local line, err = sock:receive() 349 | if not line then 350 | return nil, "failed to " .. job_type .. ", receive data error: " .. err 351 | end 352 | local id, size = strmatch(line, "^FOUND (%d+) (%d+)$") 353 | if id and size then -- remove \r\n 354 | local data, err = sock:receive(size+2) 355 | if err then 356 | return nil, "failed to " .. job_type .. ", receive job body error: " .. err 357 | end 358 | return id, strsub(data, 1, -3) 359 | end 360 | return false, line 361 | end 362 | 363 | function _M.peek_buried(self) 364 | return _peek_job_type(self, "peek-buried") 365 | end 366 | 367 | 368 | function _M.peek_ready(self) 369 | return _peek_job_type(self, "peek-ready") 370 | end 371 | 372 | 373 | function _M.peek_delayed(self) 374 | return _peek_job_type(self, "peek-delayed") 375 | end 376 | 377 | 378 | function _M.close(self) 379 | local sock = self.sock 380 | if not sock then 381 | return nil, "not initialized" 382 | end 383 | sock:send("quit\r\n") 384 | return sock:close() 385 | end 386 | _M.quit = _M.close 387 | 388 | 389 | local function _manager_command(self, ...) 390 | local sock = self.sock 391 | 392 | local bytes, err = sock:send(tabconcat({...}, " ") .. "\r\n") 393 | if not bytes then 394 | return nil, err 395 | end 396 | 397 | local line, err = sock:receive() 398 | if not line then 399 | return nil, err 400 | end 401 | 402 | local size = strmatch(line, "^OK (%d+)$") 403 | if not size then 404 | return nil, line 405 | end 406 | 407 | return sock:receive(size+2) 408 | end 409 | 410 | function _M.stats(self) 411 | return _manager_command(self, "stats") 412 | end 413 | 414 | 415 | function _M.list_tubes(self) 416 | return _manager_command(self, "list-tubes") 417 | end 418 | 419 | function _M.list_tube_used(self) 420 | local sock = self.sock 421 | if not sock then 422 | return nil, "not initialized" 423 | end 424 | local bytes, err = sock:send("list-tube-used\r\n") 425 | if not bytes then 426 | return nil, "failed to list tube used, send data error: " .. err 427 | end 428 | local line 429 | line, err = sock:receive() 430 | if not line then 431 | return nil, "failed to list tube used, receive data error: " .. err 432 | end 433 | local tube = strmatch(line, "^USING (.+)$") 434 | if not tube then 435 | return nil, line 436 | end 437 | return tube, nil 438 | end 439 | 440 | function _M.list_tubes_watched(self) 441 | return _manager_command(self, "list-tubes-watched") 442 | end 443 | 444 | function _M.stats_job(self, id) 445 | return _manager_command(self, "stats-job", id) 446 | end 447 | 448 | function _M.stats_tube(self, tube) 449 | if not tube then 450 | return nil, "invalid tube name, please check your input" 451 | end 452 | 453 | return _manager_command(self, "stats-tube", tube) 454 | end 455 | 456 | 457 | return _M 458 | -------------------------------------------------------------------------------- /rockspec/lua-resty-beanstalkd-0.0-5.rockspec: -------------------------------------------------------------------------------- 1 | package = "lua-resty-beanstalkd" 2 | version = "0.0-5" 3 | 4 | source = { 5 | url = "git://github.com/smallfish/lua-resty-beanstalkd.git" 6 | } 7 | 8 | description = { 9 | summary = "Lua beastalkd client driver for the ngx_lua based on the cosocket API", 10 | detailed = "Lua beastalkd client driver for the ngx_lua based on the cosocket API", 11 | homepage = "https://github.com/smallfish/lua-resty-beanstalkd", 12 | maintainer = "smallfish ", 13 | license = "2bsd" 14 | } 15 | 16 | dependencies = { 17 | "lua >= 5.1" 18 | } 19 | 20 | build = { 21 | type = "builtin", 22 | modules = { 23 | ["resty.beanstalkd"] = "lib/resty/beanstalkd.lua" 24 | } 25 | } -------------------------------------------------------------------------------- /t/01-version.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | 18 | no_long_string(); 19 | #no_diff(); 20 | 21 | run_tests(); 22 | 23 | __DATA__ 24 | 25 | === TEST 1: version 26 | --- http_config eval: $::HttpConfig 27 | --- config 28 | location /t { 29 | content_by_lua ' 30 | local beanstalkd = require "resty.beanstalkd" 31 | ngx.say(beanstalkd.VERSION) 32 | '; 33 | } 34 | --- request 35 | GET /t 36 | --- response_body_like chop 37 | ^\d+\.\d+\.\d+$ 38 | --- no_error_log 39 | [error] 40 | 41 | -------------------------------------------------------------------------------- /t/011-watch.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks() + 1); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: watch 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local size, err = bean:watch("default") 42 | if not size then 43 | ngx.say("2: failed to watch tube: ", err) 44 | return 45 | end 46 | 47 | ngx.say("watching: ", size) 48 | 49 | bean:close() 50 | '; 51 | } 52 | --- request 53 | GET /t 54 | --- response_body 55 | watching: 1 56 | --- no_error_log 57 | [error] 58 | 59 | === TEST 2: handle watching failure 60 | --- http_config eval: $::HttpConfig 61 | --- config 62 | location /t { 63 | content_by_lua ' 64 | local beanstalkd = require "resty.beanstalkd" 65 | 66 | local bean, err = beanstalkd:new() 67 | 68 | local ok, err = bean:connect("127.0.0.1", 2017) 69 | if not ok then 70 | ngx.say("1: failed to connect: ", err) 71 | return 72 | end 73 | 74 | local size, err = bean:watch("default") 75 | if not size then 76 | ngx.say("failed to watch tube: ", err) 77 | return 78 | end 79 | ngx.say("watching: ", size) 80 | bean:close() 81 | '; 82 | } 83 | --- request 84 | GET /t 85 | --- tcp_listen: 2017 86 | --- tcp_query eval 87 | "watch default\r\n" 88 | --- tcp_reply eval 89 | "OUT_OF_MEMORY\r\n" 90 | --- response_body 91 | failed to watch tube: OUT_OF_MEMORY 92 | --- no_error_log 93 | [error] 94 | 95 | -------------------------------------------------------------------------------- /t/012-ignore.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: ignore 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local size, err = bean:watch("default") 42 | if not size then 43 | ngx.say("2: failed to watch tube: ", err) 44 | return 45 | end 46 | 47 | ngx.say("1: watching: ", size) 48 | 49 | local size, err = bean:watch("test") 50 | if not size then 51 | ngx.say("2: failed to watch tube: ", err) 52 | return 53 | end 54 | 55 | ngx.say("2: watching: ", size) 56 | 57 | local size, err = bean:ignore("default") 58 | if not size then 59 | ngx.say("3: failed to ignore tube: ", err) 60 | return 61 | end 62 | 63 | ngx.say("3: watching: ", size) 64 | 65 | bean:close() 66 | '; 67 | } 68 | --- request 69 | GET /t 70 | --- response_body_like chop 71 | 1: watching: \d+ 72 | 2: watching: \d+ 73 | 3: watching: \d+ 74 | --- no_error_log 75 | [error] 76 | 77 | 78 | === TEST 2: handle ignore failure 79 | --- http_config eval: $::HttpConfig 80 | --- config 81 | location /t { 82 | content_by_lua ' 83 | local beanstalkd = require "resty.beanstalkd" 84 | 85 | local bean, err = beanstalkd:new() 86 | 87 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 88 | if not ok then 89 | ngx.say("1: failed to connect: ", err) 90 | return 91 | end 92 | 93 | local size, err = bean:ignore("default") 94 | if not size then 95 | ngx.say("2: failed to ignore tube: ", err) 96 | return 97 | end 98 | 99 | ngx.say("watching: ", size) 100 | 101 | bean:close() 102 | '; 103 | } 104 | --- request 105 | GET /t 106 | --- response_body_like chop 107 | 2: failed to ignore tube: NOT_IGNORED 108 | --- no_error_log 109 | [error] 110 | 111 | -------------------------------------------------------------------------------- /t/02-put.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(3); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: put 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-put") 42 | if not ok then 43 | ngx.say("2: failed to use tube: ", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | ngx.say("put: ", id) 54 | 55 | bean:close() 56 | '; 57 | } 58 | --- request 59 | GET /t 60 | --- response_body_like chop 61 | put: \d+ 62 | --- no_error_log 63 | [error] 64 | 65 | -------------------------------------------------------------------------------- /t/03-reserve.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: reserve 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-reserve") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-reserve") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("5: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | end 66 | 67 | bean:close() 68 | '; 69 | } 70 | --- request 71 | GET /t 72 | --- response_body_like chop 73 | 1: reserve: \d+ 74 | --- no_error_log 75 | [error] 76 | 77 | -------------------------------------------------------------------------------- /t/031-peek.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: peek 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-peek1") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-peek1") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("3: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | local id, data = bean:peek(id) 66 | if not id then 67 | ngx.say("5: peek failed, id not found") 68 | return 69 | else 70 | ngx.say("2: peek: ", data) 71 | end 72 | end 73 | 74 | bean:close() 75 | '; 76 | } 77 | --- request 78 | GET /t 79 | --- response_body_like chop 80 | 1: reserve: \d+ 81 | 2: peek: hello 82 | --- no_error_log 83 | [error] 84 | 85 | 86 | 87 | === TEST 2: peek-buried 88 | --- http_config eval: $::HttpConfig 89 | --- config 90 | location /t { 91 | content_by_lua ' 92 | local beanstalkd = require "resty.beanstalkd" 93 | 94 | local bean, err = beanstalkd:new() 95 | 96 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 97 | if not ok then 98 | ngx.say("1: failed to connect: ", err) 99 | return 100 | end 101 | 102 | local ok, err = bean:use("hello-peek2") 103 | if not ok then 104 | ngx.say("2: failed to use tube: ", err) 105 | return 106 | end 107 | 108 | local id, err = bean:put("hello") 109 | if not id then 110 | ngx.say("3: failed to put: ", err) 111 | return 112 | end 113 | 114 | local ok, err = bean:watch("hello-peek2") 115 | if not ok then 116 | ngx.say("4: failed to watch: ", err) 117 | return 118 | end 119 | 120 | local id, data = bean:reserve() 121 | if not id then 122 | ngx.say("5: failed to reserve: ", err) 123 | return 124 | end 125 | 126 | local ok, err = bean:bury(id) 127 | if not ok then 128 | ngx.say("3: failed to bury: ", id) 129 | return 130 | else 131 | ngx.say("1: bury: ", id) 132 | local id, data = bean:peek_buried() 133 | if not id then 134 | ngx.say("6: peek_buried failed, id not found ", id) 135 | return 136 | else 137 | ngx.say("4: peek_buried: ", data) 138 | end 139 | 140 | bean:delete(id) 141 | end 142 | 143 | bean:close() 144 | '; 145 | } 146 | --- request 147 | GET /t 148 | --- response_body_like chop 149 | 1: bury: \d+ 150 | 4: peek_buried: hello 151 | --- no_error_log 152 | [error] 153 | 154 | === TEST 3: peek-ready 155 | --- http_config eval: $::HttpConfig 156 | --- config 157 | location /t { 158 | content_by_lua ' 159 | local beanstalkd = require "resty.beanstalkd" 160 | 161 | local bean, err = beanstalkd:new() 162 | 163 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 164 | if not ok then 165 | ngx.say("1: failed to connect: ", err) 166 | return 167 | end 168 | 169 | -- use a seperate tube 170 | bean:use("test-peek-ready") 171 | local ok, err = bean:watch("test-peek-ready") 172 | if not ok then 173 | ngx.say("2: failed to watch: ", err) 174 | return 175 | else 176 | bean:ignore("default") 177 | end 178 | 179 | local id, err = bean:put("hello") 180 | if not id then 181 | ngx.say("2: failed to put: ", err) 182 | return 183 | end 184 | bean:put("world") 185 | 186 | local id, data = bean:peek_ready() 187 | if not id then 188 | ngx.say("3: peek_ready failed, id not found ", id) 189 | return 190 | else 191 | ngx.say("3: peek_ready: ", data) 192 | end 193 | id, data = bean:peek_ready() 194 | if not id then 195 | ngx.say("4: peek_ready failed, id not found ", id) 196 | return 197 | else 198 | ngx.say("4: peek_ready: ", data) 199 | end 200 | 201 | id, data = bean:reserve() 202 | if not id then 203 | ngx.say("5: failed to reserve: ", err) 204 | return 205 | else 206 | bean:delete(id) 207 | end 208 | id, data = bean:peek_ready() 209 | if not id then 210 | ngx.say("6: peek_ready failed, id not found ", id) 211 | return 212 | else 213 | ngx.say("6: peek_ready: ", data) 214 | end 215 | 216 | -- clean the tube 217 | id, data = bean:reserve() 218 | if not id then 219 | ngx.say("7: failed to reserve: ", err) 220 | return 221 | else 222 | bean:delete(id) 223 | end 224 | bean:close() 225 | '; 226 | } 227 | --- request 228 | GET /t 229 | --- response_body_like chop 230 | 3: peek_ready: hello 231 | 4: peek_ready: hello 232 | 6: peek_ready: world 233 | --- no_error_log 234 | [error] 235 | 236 | === TEST 4: peek-delayed 237 | --- http_config eval: $::HttpConfig 238 | --- config 239 | location /t { 240 | content_by_lua ' 241 | local beanstalkd = require "resty.beanstalkd" 242 | 243 | local bean, err = beanstalkd:new() 244 | 245 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 246 | if not ok then 247 | ngx.say("1: failed to connect: ", err) 248 | return 249 | end 250 | 251 | local ok, err = bean:use("test-peek-delayed") 252 | if not ok then 253 | ngx.say("2: failed to use tube: ", err) 254 | return 255 | end 256 | bean:watch("test-peek-delayed") 257 | bean:ignore("default") 258 | 259 | local id, err = bean:put("hello") 260 | if not id then 261 | ngx.say("3: failed to put: ", err) 262 | return 263 | end 264 | 265 | local id, data = bean:reserve() 266 | if not id then 267 | ngx.say("3: failed to reserve: ", err) 268 | return 269 | end 270 | 271 | local ok, err = bean:release(id, 2 ^ 32, 100) 272 | if not ok then 273 | ngx.say("3: failed to delay ", id) 274 | return 275 | else 276 | ngx.say("1: bury: ", id) 277 | local id, data = bean:peek_delayed() 278 | if not id then 279 | ngx.say("4: peek_delayed failed, id not found ", id) 280 | return 281 | else 282 | ngx.say("4: peek_delayed ", data) 283 | end 284 | 285 | bean:delete(id) 286 | end 287 | 288 | bean:close() 289 | '; 290 | } 291 | --- request 292 | GET /t 293 | --- response_body_like chop 294 | 1: bury: \d+ 295 | 4: peek_delayed hello 296 | --- no_error_log 297 | [error] 298 | -------------------------------------------------------------------------------- /t/032-delete.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: delete 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-delete") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-delete") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("5: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | local ok, err = bean:delete(id) 66 | if not ok then 67 | ngx.say("6: failed to delete: ", id) 68 | return 69 | end 70 | ngx.say("2: delete: ", id) 71 | end 72 | 73 | bean:close() 74 | '; 75 | } 76 | --- request 77 | GET /t 78 | --- response_body_like chop 79 | 1: reserve: \d+ 80 | 2: delete: \d+ 81 | --- no_error_log 82 | [error] 83 | 84 | -------------------------------------------------------------------------------- /t/04-release.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: release 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-release") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-release") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("5: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | local ok, err = bean:release(id) 66 | if not ok then 67 | ngx.say("6: failed to release, id:", id, " error:", err) 68 | return 69 | else 70 | ngx.say("2: release: ", id) 71 | end 72 | end 73 | 74 | bean:close() 75 | '; 76 | } 77 | --- request 78 | GET /t 79 | --- response_body_like chop 80 | 1: reserve: \d+ 81 | 2: release: \d+ 82 | --- no_error_log 83 | [error] 84 | 85 | -------------------------------------------------------------------------------- /t/05-bury.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: bury 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-bury") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-bury") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("5: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | local ok, err = bean:bury(id) 66 | if not ok then 67 | ngx.say("6: failed to bury, id:", id, " error:", err) 68 | return 69 | else 70 | ngx.say("2: bury: ", id) 71 | end 72 | end 73 | 74 | bean:close() 75 | '; 76 | } 77 | --- request 78 | GET /t 79 | --- response_body_like chop 80 | 1: reserve: \d+ 81 | 2: bury: \d+ 82 | --- no_error_log 83 | [error] 84 | 85 | -------------------------------------------------------------------------------- /t/06-kick.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: kick 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local ok, err = bean:use("hello-kick") 42 | if not ok then 43 | ngx.say("2: failed to use:", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local ok, err = bean:watch("hello-kick") 54 | if not ok then 55 | ngx.say("4: failed to watch: ", err) 56 | return 57 | end 58 | 59 | local id, data = bean:reserve() 60 | if not id then 61 | ngx.say("5: failed to reserve: ", id) 62 | return 63 | else 64 | ngx.say("1: reserve: ", id) 65 | local count, err = bean:kick(1) 66 | if not count then 67 | ngx.say("6: kick failed, error:", err) 68 | return 69 | else 70 | ngx.say("2: kick: ", count) 71 | end 72 | end 73 | 74 | bean:close() 75 | '; 76 | } 77 | --- request 78 | GET /t 79 | --- response_body_like chop 80 | 1: reserve: \d+ 81 | 2: kick: \d+ 82 | --- no_error_log 83 | [error] 84 | 85 | -------------------------------------------------------------------------------- /t/061-kick-job.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: kick-job 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua_block { 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | ok, err = bean:use("kick_job") 42 | if not ok then 43 | ngx.say("2: failed to use: ", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put ", err) 50 | return 51 | end 52 | 53 | ok, err = bean:kick_job(id) 54 | if not ok then 55 | ngx.say("1: failed to kick job: ", err) 56 | end 57 | 58 | ok, err = bean:watch("kick_job") 59 | if not ok then 60 | ngx.say("4: failed to watch: ", err) 61 | return 62 | end 63 | 64 | local id, data = bean:reserve() 65 | if not id then 66 | ngx.say("5: failed to reserve: ", id) 67 | return 68 | end 69 | 70 | ok, err = bean:bury(id) 71 | if not ok then 72 | ngx.say("6: failed to bury: ", err) 73 | return 74 | end 75 | 76 | ok, err = bean:kick_job(id) 77 | if not ok then 78 | ngx.say("7: kick failed, error:", err) 79 | return 80 | else 81 | ngx.say("2: kick job: ", ok) 82 | end 83 | 84 | local res, err = bean:stats_job(id) 85 | if not res then 86 | ngx.say("8: failed to stats: ", err) 87 | return 88 | end 89 | 90 | bean:close() 91 | ngx.say(res) 92 | } 93 | } 94 | --- request 95 | GET /t 96 | --- response_body_like chop 97 | 1: failed to kick job: NOT_FOUND 98 | 2: kick job: true 99 | .+ 100 | state: ready 101 | --- no_error_log 102 | [error] 103 | 104 | -------------------------------------------------------------------------------- /t/071-stats.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: stats 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local res, err = bean:stats() 42 | if not res then 43 | ngx.say("2: failed to stats: ", err) 44 | return 45 | end 46 | 47 | bean:close() 48 | 49 | ngx.say(res) 50 | '; 51 | } 52 | --- request 53 | GET /t 54 | --- response_body_like chop 55 | --- 56 | current-jobs-urgent: \d+ 57 | [\s\S]+total-jobs: \d+ 58 | [\s\S]+ 59 | --- no_error_log 60 | [error] 61 | 62 | -------------------------------------------------------------------------------- /t/072-list-tubes.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: list-tubes 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua_block { 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local res, err = bean:list_tubes() 42 | if not res then 43 | ngx.say("2: failed to stats: ", err) 44 | return 45 | end 46 | 47 | bean:close() 48 | 49 | ngx.say(res) 50 | } 51 | } 52 | --- request 53 | GET /t 54 | --- response_body_like chop 55 | --- 56 | [\s\S]*\- default[\s\S]* 57 | --- no_error_log 58 | [error] 59 | 60 | === TEST 2: list-tube-used 61 | --- http_config eval: $::HttpConfig 62 | --- config 63 | location /t { 64 | content_by_lua_block { 65 | local beanstalkd = require "resty.beanstalkd" 66 | 67 | local bean, err = beanstalkd:new() 68 | 69 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 70 | if not ok then 71 | ngx.say("1: failed to connect: ", err) 72 | return 73 | end 74 | 75 | ok, err = bean:use("list_tube_used") 76 | if not ok then 77 | ngx.say("2: failed to use tube: ", err) 78 | return 79 | end 80 | 81 | local res, err = bean:list_tube_used() 82 | if not res then 83 | ngx.say("3: failed to list used tube: ", err) 84 | return 85 | end 86 | 87 | bean:close() 88 | ngx.say(res) 89 | } 90 | } 91 | --- request 92 | GET /t 93 | --- response_body_like chop 94 | list_tube_used 95 | --- no_error_log 96 | [error] 97 | 98 | === TEST 3: list-tubes-watched 99 | --- http_config eval: $::HttpConfig 100 | --- config 101 | location /t { 102 | content_by_lua ' 103 | local beanstalkd = require "resty.beanstalkd" 104 | 105 | local bean, err = beanstalkd:new() 106 | 107 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 108 | if not ok then 109 | ngx.say("1: failed to connect: ", err) 110 | return 111 | end 112 | 113 | ok, err = bean:watch("list_tubes_watched") 114 | if not ok then 115 | ngx.say("2: failed to watch: ", err) 116 | return 117 | end 118 | 119 | local res, err = bean:list_tubes_watched() 120 | if not res then 121 | ngx.say("3: failed to stats: ", err) 122 | return 123 | end 124 | 125 | bean:close() 126 | 127 | ngx.say(res) 128 | '; 129 | } 130 | --- request 131 | GET /t 132 | --- response_body_like chop 133 | --- 134 | - default 135 | - list_tubes_watched 136 | --- no_error_log 137 | [error] 138 | 139 | -------------------------------------------------------------------------------- /t/073-stats-tubes.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: stats_tube 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua ' 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | local res, err = bean:stats_tube("default") 42 | if not res then 43 | ngx.say("2: failed to stats: ", err) 44 | return 45 | end 46 | 47 | bean:close() 48 | 49 | ngx.say(res) 50 | '; 51 | } 52 | --- request 53 | GET /t 54 | --- response_body_like chop 55 | --- 56 | name: default 57 | [\s\S]* 58 | total-jobs: \d+ 59 | [\s\S]* 60 | --- no_error_log 61 | [error] 62 | 63 | 64 | === TEST 2: stats_tube not found 65 | --- http_config eval: $::HttpConfig 66 | --- config 67 | location /t { 68 | content_by_lua ' 69 | local beanstalkd = require "resty.beanstalkd" 70 | 71 | local bean, err = beanstalkd:new() 72 | 73 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 74 | if not ok then 75 | ngx.say("1: failed to connect: ", err) 76 | return 77 | end 78 | 79 | local res, err = bean:stats_tube("_NOT_FOUND") 80 | if not res then 81 | ngx.say("2: failed to stats: ", err) 82 | return 83 | end 84 | 85 | bean:close() 86 | '; 87 | } 88 | --- request 89 | GET /t 90 | --- response_body 91 | 2: failed to stats: NOT_FOUND 92 | --- no_error_log 93 | [error] 94 | 95 | 96 | === TEST 3: stats_tube invalid tube name 97 | --- http_config eval: $::HttpConfig 98 | --- config 99 | location /t { 100 | content_by_lua ' 101 | local beanstalkd = require "resty.beanstalkd" 102 | 103 | local bean, err = beanstalkd:new() 104 | 105 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 106 | if not ok then 107 | ngx.say("1: failed to connect: ", err) 108 | return 109 | end 110 | 111 | local res, err = bean:stats_tube(nil) 112 | if not res then 113 | ngx.say("2: failed to stats: ", err) 114 | return 115 | end 116 | 117 | bean:close() 118 | '; 119 | } 120 | --- request 121 | GET /t 122 | --- response_body 123 | 2: failed to stats: invalid tube name, please check your input 124 | --- no_error_log 125 | [error] 126 | 127 | 128 | 129 | -------------------------------------------------------------------------------- /t/074-stats-job.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: stats_job 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua_block { 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | ok, err = bean:use("stats_job_tube") 42 | if not ok then 43 | ngx.say("2: failed to use tube: ", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local res, err = bean:stats_job(id) 54 | if not res then 55 | ngx.say("4: failed to stats: ", err) 56 | return 57 | end 58 | 59 | bean:close() 60 | ngx.say(res) 61 | } 62 | } 63 | --- request 64 | GET /t 65 | --- response_body_like chop 66 | --- 67 | id: \d+ 68 | tube: stats_job_tube 69 | state: ready 70 | --- no_error_log 71 | [error] 72 | 73 | -------------------------------------------------------------------------------- /t/08-pause-tube.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: reserve 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua_block { 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | ok, err = bean:pause_tube(nil, 15) 42 | if ok or err ~= "invalid tube name, please check your input" then 43 | ngx.say("2: failed to pause_tube: ", err) 44 | return 45 | end 46 | 47 | ok, err = bean:pause_tube("default") 48 | if ok or err ~= "invalid delay, please check your input" then 49 | ngx.say("3: failed to pause_tube: ", err) 50 | return 51 | end 52 | 53 | ok, err = bean:pause_tube("null", 15) 54 | if ok or err ~= "NOT_FOUND" then 55 | ngx.say("4: failed to pause_tube: ", err) 56 | return 57 | end 58 | 59 | ok, err = bean:pause_tube("default", 15) 60 | if not ok then 61 | ngx.say("5: failed to pause_tube: ", err) 62 | return 63 | end 64 | 65 | local stats, err = bean:stats_tube("default") 66 | if not stats then 67 | ngx.say("6: failed to stats tube: ", err) 68 | return 69 | end 70 | ngx.say(stats) 71 | bean:close() 72 | } 73 | } 74 | --- request 75 | GET /t 76 | --- response_body_like chop 77 | pause-time-left: \d\d 78 | --- no_error_log 79 | [error] 80 | 81 | -------------------------------------------------------------------------------- /t/09-touch.t: -------------------------------------------------------------------------------- 1 | # vim:set ft= ts=4 sw=4 et: 2 | 3 | use Test::Nginx::Socket; 4 | use Cwd qw(cwd); 5 | 6 | repeat_each(2); 7 | 8 | plan tests => repeat_each() * (3 * blocks()); 9 | 10 | my $pwd = cwd(); 11 | 12 | our $HttpConfig = qq{ 13 | lua_package_path "$pwd/lib/?.lua;;"; 14 | }; 15 | 16 | $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; 17 | $ENV{TEST_NGINX_BEANSTALKD_PORT} ||= 11300; 18 | 19 | no_long_string(); 20 | no_shuffle(); 21 | 22 | run_tests(); 23 | 24 | __DATA__ 25 | 26 | === TEST 1: touch 27 | --- http_config eval: $::HttpConfig 28 | --- config 29 | location /t { 30 | content_by_lua_block { 31 | local beanstalkd = require "resty.beanstalkd" 32 | 33 | local bean, err = beanstalkd:new() 34 | 35 | local ok, err = bean:connect("127.0.0.1", $TEST_NGINX_BEANSTALKD_PORT) 36 | if not ok then 37 | ngx.say("1: failed to connect: ", err) 38 | return 39 | end 40 | 41 | ok, err = bean:use("touch") 42 | if not ok then 43 | ngx.say("2: failed to use tube: ", err) 44 | return 45 | end 46 | 47 | local id, err = bean:put("hello") 48 | if not id then 49 | ngx.say("3: failed to put: ", err) 50 | return 51 | end 52 | 53 | local res, err = bean:touch(id) 54 | if not res then 55 | ngx.say("1: failed to touch: ", err) 56 | end 57 | 58 | ok, err = bean:watch("touch") 59 | if not ok then 60 | ngx.say("4: failed to watch tube: ", err) 61 | return 62 | end 63 | 64 | local id, data = bean:reserve() 65 | if not id then 66 | ngx.say("5: failed to reserve: ", data) 67 | return 68 | end 69 | 70 | res, err = bean:touch(id) 71 | if not res then 72 | ngx.say("6: failed to touch: ", err) 73 | return 74 | end 75 | 76 | bean:close() 77 | ngx.say("2: touch: ", res) 78 | } 79 | } 80 | --- request 81 | GET /t 82 | --- response_body_like chop 83 | 1: failed to touch: NOT_FOUND 84 | 2: touch: true 85 | --- no_error_log 86 | [error] 87 | 88 | -------------------------------------------------------------------------------- /util/build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # this script is for developers only. 4 | # dependent on the ngx-build script from the nginx-devel-utils repostory: 5 | # https://github.com/openresty/nginx-devel-utils/blob/master/ngx-build 6 | # the resulting nginx is located at ./work/nginx/sbin/nginx 7 | 8 | root=`pwd` 9 | version=${1:-1.4.1} 10 | home=~ 11 | force=$2 12 | 13 | # the ngx-build script is from https://github.com/agentzh/nginx-devel-utils 14 | 15 | #--add-module=$home/work/nginx_upload_module-2.2.0 \ 16 | 17 | #--without-pcre \ 18 | #--without-http_rewrite_module \ 19 | #--without-http_autoindex_module \ 20 | #--with-cc=gcc46 \ 21 | #--with-cc=clang \ 22 | #--without-http_referer_module \ 23 | #--with-http_spdy_module \ 24 | 25 | time ngx-build $force $version \ 26 | --with-ipv6 \ 27 | --with-cc-opt="-I$PCRE_INC -I$OPENSSL_INC" \ 28 | --with-http_v2_module \ 29 | --with-http_realip_module \ 30 | --with-http_ssl_module \ 31 | --add-module=$root/../ndk-nginx-module \ 32 | --add-module=$root/../set-misc-nginx-module \ 33 | --with-ld-opt="-L$PCRE_LIB -L$OPENSSL_LIB -Wl,-rpath,$PCRE_LIB:$LIBDRIZZLE_LIB:$OPENSSL_LIB" \ 34 | --without-mail_pop3_module \ 35 | --without-mail_imap_module \ 36 | --with-http_image_filter_module \ 37 | --without-mail_smtp_module \ 38 | --without-http_upstream_ip_hash_module \ 39 | --without-http_memcached_module \ 40 | --without-http_auth_basic_module \ 41 | --without-http_userid_module \ 42 | --with-http_auth_request_module \ 43 | --add-module=$root/../echo-nginx-module \ 44 | --add-module=$root/../memc-nginx-module \ 45 | --add-module=$root/../srcache-nginx-module \ 46 | --add-module=$root/../lua-upstream-nginx-module \ 47 | --add-module=$root/../headers-more-nginx-module \ 48 | --add-module=$root/../drizzle-nginx-module \ 49 | --add-module=$root/../rds-json-nginx-module \ 50 | --add-module=$root/../coolkit-nginx-module \ 51 | --add-module=$root/../redis2-nginx-module \ 52 | --add-module=$root/t/data/fake-module \ 53 | --add-module=$root/t/data/fake-shm-module \ 54 | --add-module=$root/t/data/fake-delayed-load-module \ 55 | --with-http_gunzip_module \ 56 | --with-http_dav_module \ 57 | --with-select_module \ 58 | --with-poll_module \ 59 | $opts \ 60 | --with-debug 61 | 62 | --------------------------------------------------------------------------------