|
| 1 | +local expirationd = require("expirationd") |
| 2 | +local role_name = "expirationd" |
| 3 | +local started = require("cartridge.vars").new(role_name) |
| 4 | + |
| 5 | +local function load_function(func_name) |
| 6 | + if func_name == nil or type(func_name) ~= 'string' then |
| 7 | + return nil |
| 8 | + end |
| 9 | + |
| 10 | + local func = rawget(_G, func_name) |
| 11 | + if func == nil or type(func) ~= 'function' then |
| 12 | + return nil |
| 13 | + end |
| 14 | + return func |
| 15 | +end |
| 16 | + |
| 17 | +local function get_param(param_name, value, types) |
| 18 | + local types_map = { |
| 19 | + b = {type = "boolean", err = "a boolean"}, |
| 20 | + n = {type = "number", err = "a number"}, |
| 21 | + s = {type = "string", err = "a string"}, |
| 22 | + f = {type = "string", transform = load_function, err = "a function name in _G"}, |
| 23 | + t = {type = "table", err = "a table"}, |
| 24 | + any = {err = "any type"}, |
| 25 | + } |
| 26 | + |
| 27 | + local found = false |
| 28 | + for _, t in ipairs(types) do |
| 29 | + local type_opts = types_map[t] |
| 30 | + if type_opts == nil then |
| 31 | + error(role_name .. ": unsupported type option") |
| 32 | + end |
| 33 | + if not type_opts.type or type(value) == type_opts.type then |
| 34 | + if type_opts.transform then |
| 35 | + local tmp = type_opts.transform(value) |
| 36 | + if tmp then |
| 37 | + value = tmp |
| 38 | + found = true |
| 39 | + break |
| 40 | + end |
| 41 | + else |
| 42 | + found = true |
| 43 | + break |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + if not found then |
| 49 | + local err = role_name .. ": " .. param_name .. " must be " |
| 50 | + for i, t in ipairs(types) do |
| 51 | + err = err .. types_map[t].err |
| 52 | + if i ~= #types then |
| 53 | + err = err .. " or " |
| 54 | + end |
| 55 | + end |
| 56 | + return false, err |
| 57 | + end |
| 58 | + return true, value |
| 59 | +end |
| 60 | + |
| 61 | +local function get_task_options(opts) |
| 62 | + local opts_map = { |
| 63 | + args = {"any"}, |
| 64 | + atomic_iteration = {"b"}, |
| 65 | + force = {"b"}, |
| 66 | + force_allow_functional_index = {"b"}, |
| 67 | + full_scan_delay = {"n"}, |
| 68 | + full_scan_time = {"n"}, |
| 69 | + index = {"n", "s"}, |
| 70 | + iterate_with = {"f"}, |
| 71 | + iteration_delay = {"n"}, |
| 72 | + iterator_type = {"n", "s"}, |
| 73 | + on_full_scan_complete = {"f"}, |
| 74 | + on_full_scan_error = {"f"}, |
| 75 | + on_full_scan_start = {"f"}, |
| 76 | + on_full_scan_success = {"f"}, |
| 77 | + process_expired_tuple = {"f"}, |
| 78 | + process_while = {"f"}, |
| 79 | + start_key = {"f", "t"}, |
| 80 | + tuples_per_iteration = {"n"}, |
| 81 | + vinyl_assumed_space_len_factor = {"n"}, |
| 82 | + vinyl_assumed_space_len = {"n"}, |
| 83 | + } |
| 84 | + if opts == nil then |
| 85 | + return |
| 86 | + end |
| 87 | + |
| 88 | + for opt, val in pairs(opts) do |
| 89 | + if type(opt) ~= "string" then |
| 90 | + error(role_name .. ": an option must be a string") |
| 91 | + end |
| 92 | + if opts_map[opt] == nil then |
| 93 | + error(role_name .. ": unsupported option '" .. opt .. "'") |
| 94 | + end |
| 95 | + local ok, res = get_param("options." .. opt, val, opts_map[opt]) |
| 96 | + if not ok then |
| 97 | + error(res) |
| 98 | + end |
| 99 | + opts[opt] = res |
| 100 | + end |
| 101 | + |
| 102 | + return opts |
| 103 | +end |
| 104 | + |
| 105 | +local function get_task_config(task_conf) |
| 106 | + -- setmetatable resets __newindex write protection on a copy |
| 107 | + local conf = setmetatable(table.deepcopy(task_conf), {}) |
| 108 | + local params_map = { |
| 109 | + space_id = {required = true, types = {"n", "s"}}, |
| 110 | + is_expired = {required = true, types = {"f"}}, |
| 111 | + is_master_only = {required = false, types = {"b"}}, |
| 112 | + options = {required = false, types = {"t"}}, |
| 113 | + } |
| 114 | + for k, _ in pairs(conf) do |
| 115 | + if type(k) ~= "string" then |
| 116 | + error(role_name .. ": param must be a string") |
| 117 | + end |
| 118 | + if params_map[k] == nil then |
| 119 | + error(role_name .. ": unsupported param " .. k) |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + for param, opts in pairs(params_map) do |
| 124 | + if opts.required and conf[param] == nil then |
| 125 | + error(role_name .. ": " .. param .. " is required") |
| 126 | + end |
| 127 | + if conf[param] ~= nil then |
| 128 | + local ok, res = get_param(param, conf[param], opts.types) |
| 129 | + if not ok then |
| 130 | + error(res) |
| 131 | + end |
| 132 | + conf[param] = res |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + conf.options = get_task_options(conf.options) |
| 137 | + return conf |
| 138 | +end |
| 139 | + |
| 140 | +local function init() |
| 141 | + |
| 142 | +end |
| 143 | + |
| 144 | +local function validate_config(conf_new) |
| 145 | + local conf = conf_new[role_name] or {} |
| 146 | + |
| 147 | + for task_name, task_conf in pairs(conf) do |
| 148 | + local ok, res = get_param("task name", task_name, {"s"}) |
| 149 | + if not ok then |
| 150 | + error(res) |
| 151 | + end |
| 152 | + local ok, res = get_param("task params", task_conf, {"t"}) |
| 153 | + if not ok then |
| 154 | + error(res) |
| 155 | + end |
| 156 | + get_task_config(task_conf) |
| 157 | + end |
| 158 | + |
| 159 | + return true |
| 160 | +end |
| 161 | + |
| 162 | +local function apply_config(conf_new, opts) |
| 163 | + local conf = conf_new[role_name] or {} |
| 164 | + |
| 165 | + -- finishes tasks from an old configuration |
| 166 | + for i=#started,1,-1 do |
| 167 | + local task_name = started[i] |
| 168 | + local ok, _ = pcall(expirationd.task, task_name) |
| 169 | + if ok then |
| 170 | + if conf[task_name] then |
| 171 | + expirationd.task(task_name):stop() |
| 172 | + else |
| 173 | + expirationd.task(task_name):kill() |
| 174 | + end |
| 175 | + end |
| 176 | + table.remove(started, i) |
| 177 | + end |
| 178 | + |
| 179 | + for task_name, task_conf in pairs(conf) do |
| 180 | + task_conf = get_task_config(task_conf) |
| 181 | + |
| 182 | + local skip = task_conf.is_master_only and not opts.is_master |
| 183 | + if not skip then |
| 184 | + local task = expirationd.start(task_name, task_conf.space_id, |
| 185 | + task_conf.is_expired, |
| 186 | + task_conf.options) |
| 187 | + if task == nil then |
| 188 | + error(role_name .. ": unable to start task " .. task_name) |
| 189 | + end |
| 190 | + table.insert(started, task_name) |
| 191 | + end |
| 192 | + end |
| 193 | +end |
| 194 | + |
| 195 | +local function stop() |
| 196 | + for _, task_name in pairs(expirationd.tasks()) do |
| 197 | + local task = expirationd.task(task_name) |
| 198 | + task:stop() |
| 199 | + end |
| 200 | +end |
| 201 | + |
| 202 | +return setmetatable({ |
| 203 | + role_name = role_name, |
| 204 | + init = init, |
| 205 | + validate_config = validate_config, |
| 206 | + apply_config = apply_config, |
| 207 | + stop = stop, |
| 208 | +}, { __index = expirationd }) |
0 commit comments