storeJobScheduler.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. --[[
  2. Function to store a job scheduler
  3. ]]
  4. local function storeJobScheduler(schedulerId, schedulerKey, repeatKey, nextMillis, opts,
  5. templateData, templateOpts)
  6. rcall("ZADD", repeatKey, nextMillis, schedulerId)
  7. local optionalValues = {}
  8. if opts['tz'] then
  9. table.insert(optionalValues, "tz")
  10. table.insert(optionalValues, opts['tz'])
  11. end
  12. if opts['limit'] then
  13. table.insert(optionalValues, "limit")
  14. table.insert(optionalValues, opts['limit'])
  15. end
  16. if opts['pattern'] then
  17. table.insert(optionalValues, "pattern")
  18. table.insert(optionalValues, opts['pattern'])
  19. end
  20. if opts['startDate'] then
  21. table.insert(optionalValues, "startDate")
  22. table.insert(optionalValues, opts['startDate'])
  23. end
  24. if opts['endDate'] then
  25. table.insert(optionalValues, "endDate")
  26. table.insert(optionalValues, opts['endDate'])
  27. end
  28. if opts['every'] then
  29. table.insert(optionalValues, "every")
  30. table.insert(optionalValues, opts['every'])
  31. end
  32. if opts['offset'] then
  33. table.insert(optionalValues, "offset")
  34. table.insert(optionalValues, opts['offset'])
  35. else
  36. local offset = rcall("HGET", schedulerKey, "offset")
  37. if offset then
  38. table.insert(optionalValues, "offset")
  39. table.insert(optionalValues, tonumber(offset))
  40. end
  41. end
  42. local jsonTemplateOpts = cjson.encode(templateOpts)
  43. if jsonTemplateOpts and jsonTemplateOpts ~= '{}' then
  44. table.insert(optionalValues, "opts")
  45. table.insert(optionalValues, jsonTemplateOpts)
  46. end
  47. if templateData and templateData ~= '{}' then
  48. table.insert(optionalValues, "data")
  49. table.insert(optionalValues, templateData)
  50. end
  51. table.insert(optionalValues, "ic")
  52. table.insert(optionalValues, rcall("HGET", schedulerKey, "ic") or 1)
  53. rcall("DEL", schedulerKey) -- remove all attributes and then re-insert new ones
  54. rcall("HMSET", schedulerKey, "name", opts['name'], unpack(optionalValues))
  55. end