Skip to content
Browse files

Consolidated config commands into one.

  • Loading branch information...
1 parent 125b582 commit e9c6ab0fde5f10cafd6594fc92d67120be3a4376 Dan Lecocq committed May 14, 2012
Showing with 44 additions and 47 deletions.
  1. +44 −0 config.lua
  2. +0 −28 getconfig.lua
  3. +0 −19 setconfig.lua
View
44 config.lua
@@ -0,0 +1,44 @@
+-- config(0, 'get', [option])
+-- config(0, 'set', option, value)
+-- config(0, 'unset', option)
+-- -------------------------------------------------------------------------------------------------------
+-- This script provides an interface to get, set, and unset configuration
+-- options.
+--
+-- Args:
+-- 1) [option]
+
+if #KEYS > 0 then error('Config(): No keys should be provided') end
+
+local command = ARGV[1]
+
+local defaults = {
+ ['application'] = 'qless',
+ ['heartbeat'] = 60,
+ ['stats-history'] = 30,
+ ['histogram-history'] = 7,
+ ['jobs-history-count'] = 50000,
+ ['jobs-history'] = 604800
+}
+
+if command == 'get' then
+ if ARGV[2] then
+ return redis.call('hget', 'ql:config', ARGV[2]) or defaults[ARGV[2]]
+ else
+ -- Inspired by redis-lua https://github.com/nrk/redis-lua/blob/version-2.0/src/redis.lua
+ local reply = redis.call('hgetall', 'ql:config')
+ for i = 1, #reply, 2 do defaults[reply[i]] = reply[i + 1] end
+ return cjson.encode(defaults)
+ end
+elseif command == 'set' then
+ local option = assert(ARGV[2], 'Config(): Arg "option" missing')
+ local value = assert(ARGV[3], 'Config(): Arg "value" missing')
+ redis.call('hset', 'ql:config', option, value)
+elseif command == 'unset' then
+ local option = assert(ARGV[2], 'Config(): Arg "option" missing')
+ redis.call('hdel', 'ql:config', option)
+else
+ error('Config(): Unrecognized command ' .. command)
+end
+
+
View
28 getconfig.lua
@@ -1,28 +0,0 @@
--- This scripts gets the requested configuration option, or in the
--- absence of a requested option, all options.
---
--- NOTE: This serves as at the very least, a reference implementation.
--- It does not require any complex locking operations, or to
--- ensure consistency, and so it is a good candidate for getting
--- absorbed into any client library.
---
--- Args:
--- 1) [option]
-
-if #KEYS > 0 then error('GetConfig(): No Keys should be provided') end
-
-local defaults = {
- ['stats-history'] = 30,
- ['histogram-history'] = 7,
- ['jobs-history-count'] = 50000,
- ['jobs-history'] = 604800
-}
-
-if ARGV[1] then
- return redis.call('hget', 'ql:config', ARGV[1]) or defaults[ARGV[1]]
-else
- -- Inspired by redis-lua https://github.com/nrk/redis-lua/blob/version-2.0/src/redis.lua
- local reply = redis.call('hgetall', 'ql:config')
- for i = 1, #reply, 2 do defaults[reply[i]] = reply[i + 1] end
- return cjson.encode(defaults)
-end
View
19 setconfig.lua
@@ -1,19 +0,0 @@
--- This scripts sets the configuration option for the provided
--- option.
---
--- NOTE: This serves as at the very least, a reference implementation.
--- It does not require any complex locking operations, or to
--- ensure consistency, and so it is a good candidate for getting
--- absorbed into any client library.
---
--- Args:
--- 1) option
--- 2) value
-
-if #KEYS > 0 then error('SetConfig(): No Keys should be provided') end
-
-if ARGV[2] then
- redis.call('hset', 'ql:config', ARGV[1], ARGV[2])
-else
- redis.call('hdel', 'ql:config', ARGV[1])
-end

0 comments on commit e9c6ab0

Please sign in to comment.
Something went wrong with that request. Please try again.