generated from your-land/yl_template
Fix mistakes luacheck found
This commit is contained in:
parent
d5d117e14a
commit
6073a060c8
22
api.lua
22
api.lua
@ -40,10 +40,10 @@ local function set_task(at, func, params, owner, notes)
|
||||
|
||||
-- func
|
||||
task.func = func
|
||||
|
||||
|
||||
-- params, optional
|
||||
task.params = params or {}
|
||||
|
||||
|
||||
-- owner
|
||||
task.owner = owner
|
||||
|
||||
@ -97,7 +97,7 @@ local function find_id(UUID)
|
||||
end
|
||||
end
|
||||
|
||||
function yl_scheduler.find_task(UUID) return find_task(UUID) end
|
||||
function yl_scheduler.find_id(UUID) return find_id(UUID) end
|
||||
|
||||
-- yl_scheduler.find_task(UUID)
|
||||
|
||||
@ -146,9 +146,9 @@ local function clean_past_tasks()
|
||||
local amount_deleted = 0
|
||||
local current_utc_time = os.time(os.date("!*t")) -- utc
|
||||
|
||||
for i = #task, 1, -1 do
|
||||
if task[i].at > current_utc_time then
|
||||
local UUID = task[i].id
|
||||
for i = #tasks, 1, -1 do
|
||||
if tasks[i].at > current_utc_time then
|
||||
local UUID = tasks[i].id
|
||||
table.remove(tasks, i)
|
||||
local success, errormsg = yl_scheduler.remove_file(UUID)
|
||||
if (success == false) then
|
||||
@ -178,9 +178,9 @@ local function clean_executed_tasks()
|
||||
local tasks = yl_scheduler.list_all_tasks()
|
||||
local amount_deleted = 0
|
||||
|
||||
for i = #task, 1, -1 do
|
||||
if task[i].done > 0 then
|
||||
local UUID = task[i].id
|
||||
for i = #tasks, 1, -1 do
|
||||
if tasks[i].done > 0 then
|
||||
local UUID = tasks[i].id
|
||||
table.remove(tasks, i)
|
||||
local success, errormsg = yl_scheduler.remove_file(UUID)
|
||||
if (success == false) then
|
||||
@ -224,8 +224,8 @@ local function execute_task(UUID)
|
||||
yl_scheduler.save_json(UUID, task)
|
||||
|
||||
-- Change the done value in the table
|
||||
for _, task in ipairs(yl_scheduler.tasks) do
|
||||
if task.id == UUID then task.done = current_utc_time end
|
||||
for _, t_task in ipairs(yl_scheduler.tasks) do
|
||||
if t_task.id == UUID then t_task.done = current_utc_time end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,13 +1,17 @@
|
||||
local chatcommand_cmd = "scheduler_add"
|
||||
local chatcommand_params = "<time$function$[param1, param2, param3, ...][$notes]>"
|
||||
local chatcommand_description = "Adds a new task that executes a function with params at the given time. See /".. chatcommand_cmd .. " help"
|
||||
local chatcommand_params =
|
||||
"<time$function$[param1, param2, param3, ...][$notes]>"
|
||||
local chatcommand_description =
|
||||
"Adds a new task that executes a function with params at the given time. See /" ..
|
||||
chatcommand_cmd .. " help"
|
||||
|
||||
local chatcommand_definition = {
|
||||
params = chatcommand_params,
|
||||
description = chatcommand_description,
|
||||
privs = yl_scheduler.get_privs(chatcommand_cmd),
|
||||
func = function(name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name,
|
||||
params)
|
||||
if success then
|
||||
return true, message
|
||||
else
|
||||
|
@ -1,13 +1,16 @@
|
||||
local chatcommand_cmd = "scheduler_clean"
|
||||
local chatcommand_params = ""
|
||||
local chatcommand_description = "Cleans up the past tasks. Old tasks are permanently deleted. See /".. chatcommand_cmd .. " help"
|
||||
local chatcommand_description =
|
||||
"Cleans up the past tasks. Old tasks are permanently deleted. See /" ..
|
||||
chatcommand_cmd .. " help"
|
||||
|
||||
local chatcommand_definition = {
|
||||
params = chatcommand_params,
|
||||
description = chatcommand_description,
|
||||
privs = yl_scheduler.get_privs(chatcommand_cmd),
|
||||
func = function(name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name,
|
||||
params)
|
||||
if success then
|
||||
return true, message
|
||||
else
|
||||
|
@ -1,13 +1,16 @@
|
||||
local chatcommand_cmd = "scheduler_list"
|
||||
local chatcommand_params = "[[part of] UUID]"
|
||||
local chatcommand_description = "Lists all tasks, optionally only those that match the given (part of a) UUID. See /".. chatcommand_cmd .. " help"
|
||||
local chatcommand_description =
|
||||
"Lists all tasks, optionally only those that match the given (part of a) UUID. See /" ..
|
||||
chatcommand_cmd .. " help"
|
||||
|
||||
local chatcommand_definition = {
|
||||
params = chatcommand_params,
|
||||
description = chatcommand_description,
|
||||
privs = yl_scheduler.get_privs(chatcommand_cmd),
|
||||
func = function(name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name,
|
||||
params)
|
||||
if success then
|
||||
return true, message
|
||||
else
|
||||
|
@ -1,13 +1,16 @@
|
||||
local chatcommand_cmd = "scheduler_remove"
|
||||
local chatcommand_params = "<UUID>"
|
||||
local chatcommand_description = "Removes the given UUID from the task list. See /".. chatcommand_cmd .. " help"
|
||||
local chatcommand_description =
|
||||
"Removes the given UUID from the task list. See /" .. chatcommand_cmd ..
|
||||
" help"
|
||||
|
||||
local chatcommand_definition = {
|
||||
params = chatcommand_params,
|
||||
description = chatcommand_description,
|
||||
privs = yl_scheduler.get_privs(chatcommand_cmd),
|
||||
func = function(name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name, params)
|
||||
local success, message = yl_scheduler["cmd_" .. chatcommand_cmd](name,
|
||||
params)
|
||||
if success then
|
||||
return true, message
|
||||
else
|
||||
|
@ -28,8 +28,8 @@ What's more efficient? Have an index with UUID? We often query those, but the qu
|
||||
- Remove Task via chatcommand
|
||||
- List Task via chatcommand
|
||||
- Clean Tasks via chatcommand
|
||||
- API parameter defense
|
||||
- Check UUID collisions
|
||||
- API parameter defense OK
|
||||
- Check UUID collisions OK
|
||||
- Support other time formats
|
||||
- Write scheduler :P
|
||||
- Investigate AspireMint's solution of having a moving next target, recalculate when a new task is aded or executed
|
||||
@ -42,10 +42,3 @@ Example UUID
|
||||
11a47d48-4d0c-47ab-a5ef-4f56781cae03
|
||||
|
||||
###
|
||||
|
||||
TODAY:
|
||||
|
||||
|
||||
- finish API
|
||||
- defend API
|
||||
|
||||
|
@ -2,7 +2,7 @@ local timer = 0
|
||||
|
||||
local gs = function(dtime)
|
||||
timer = timer + dtime
|
||||
if timer <= yl_template.config.interval then
|
||||
if timer <= yl_scheduler.config.interval then
|
||||
return
|
||||
end
|
||||
timer = 0
|
||||
|
57
internal.lua
57
internal.lua
@ -25,10 +25,9 @@ end
|
||||
local function is_uuid_duplicate(UUID)
|
||||
for i, task in ipairs(yl_scheduler.tasks) do
|
||||
-- HERE
|
||||
core.log("action","task.id="..dump(task.id)..", UUID="..dump(UUID))
|
||||
if task.id == UUID then
|
||||
return true
|
||||
end
|
||||
core.log("action",
|
||||
"task.id=" .. dump(task.id) .. ", UUID=" .. dump(UUID))
|
||||
if task.id == UUID then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
@ -36,7 +35,7 @@ end
|
||||
local function create_uuid()
|
||||
local max_attempts = 10
|
||||
|
||||
local UUID = ""
|
||||
local UUID
|
||||
|
||||
repeat
|
||||
UUID = generate_uuid()
|
||||
@ -78,7 +77,7 @@ local function ends_with(str, suffix) return str:sub(-suffix:len()) == suffix en
|
||||
-- Validate values
|
||||
|
||||
local function validate_at(at)
|
||||
-- Let's assume the calcuation already happened and we're dealing with a
|
||||
-- Let's assume the calcuation already happened and we're dealing with a
|
||||
-- unix epoch timestamp in utc. We can't detect utc though.
|
||||
if (at == nil) then
|
||||
return false, "at: No time given"
|
||||
@ -92,8 +91,8 @@ local function validate_at(at)
|
||||
end
|
||||
|
||||
local function validate_func(func)
|
||||
-- Should we check existence of the function at set time?? No.
|
||||
-- Functions may be retrofitted. We need to check their existance
|
||||
-- Should we check existence of the function at set time?? No.
|
||||
-- Functions may be retrofitted. We need to check their existance
|
||||
-- at runtime, not during storing
|
||||
if (func == nil) then
|
||||
return false, "func: No func given"
|
||||
@ -107,7 +106,7 @@ end
|
||||
local function validate_params(params)
|
||||
-- We don't know much about params.
|
||||
-- Could be nil, could be a table
|
||||
if (type(params) ~= "nil") or (type(params) ~= "table") then
|
||||
if (type(params) ~= "nil") and (type(params) ~= "table") then
|
||||
return false, "params: Wrong type"
|
||||
else
|
||||
return true, "params: all good"
|
||||
@ -128,7 +127,7 @@ end
|
||||
local function validate_notes(notes)
|
||||
-- Notes are optional
|
||||
-- Could be nil, could be a string
|
||||
if (type(notes) ~= "nil") or (type(notes) ~= "string") then
|
||||
if (type(notes) ~= "nil") and (type(notes) ~= "string") then
|
||||
return false, "notes: Wrong type"
|
||||
else
|
||||
return true, "notes: all good"
|
||||
@ -138,25 +137,15 @@ end
|
||||
local function validate(at, func, params, owner, notes)
|
||||
|
||||
local at_succes, at_message = validate_at(at)
|
||||
if (at_succes == false) then
|
||||
return false, at_message
|
||||
end
|
||||
if (at_succes == false) then return false, at_message end
|
||||
local func_succes, func_message = validate_func(func)
|
||||
if (func_succes == false) then
|
||||
return false, func_message
|
||||
end
|
||||
if (func_succes == false) then return false, func_message end
|
||||
local params_succes, params_message = validate_params(params)
|
||||
if (params_succes == false) then
|
||||
return false, params_message
|
||||
end
|
||||
if (params_succes == false) then return false, params_message end
|
||||
local owner_succes, owner_message = validate_owner(owner)
|
||||
if (owner_succes == false) then
|
||||
return false, owner_message
|
||||
end
|
||||
if (owner_succes == false) then return false, owner_message end
|
||||
local notes_succes, notes_message = validate_notes(notes)
|
||||
if (notes_succes == false) then
|
||||
return false, notes_message
|
||||
end
|
||||
if (notes_succes == false) then return false, notes_message end
|
||||
|
||||
return true, "All good"
|
||||
end
|
||||
@ -316,15 +305,8 @@ function yl_scheduler.remove_file(UUID) return remove_file(UUID) end
|
||||
|
||||
-- ### scheduler_add ###
|
||||
|
||||
local function cmd_scheduler_add(name, params)
|
||||
-- Defense: Overlap with yl_scheduler.set_task(at, func, params, owner, notes)
|
||||
-- Params
|
||||
-- Time, required: translate various time formats into utc timestamp
|
||||
-- Function, required: Should we check existence and that it's a function?
|
||||
-- Params, optional
|
||||
-- Notes, optional
|
||||
-- Store
|
||||
-- Re-sort tasks table
|
||||
local function cmd_scheduler_add(name, c_params)
|
||||
|
||||
end
|
||||
|
||||
function yl_scheduler.cmd_scheduler_add(name, params)
|
||||
@ -356,12 +338,11 @@ end
|
||||
|
||||
-- ### scheduler_clean ###
|
||||
|
||||
local function cmd_scheduler_list(name, params)
|
||||
local function cmd_scheduler_clean(name, params)
|
||||
-- Just do it and at best return how many were removed, how many remain and how many the list had before.
|
||||
-- Use yl_scheduler.clean_executed_tasks() and yl_scheduler.clean_past_tasks()
|
||||
end
|
||||
|
||||
function yl_scheduler.cmd_scheduler_list(name, params)
|
||||
return cmd_scheduler_list(name, params)
|
||||
function yl_scheduler.cmd_scheduler_clean(name, params)
|
||||
return cmd_scheduler_clean(name, params)
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user