local cqueues = require 'cqueues' local cq_notify = require 'cqueues.notify' local posix_dirent = require 'posix.dirent' local server = require 'r.web.server' local app = require 'app' local db = require 'r.web.db' local pprint = require'pprint' -- should happen on code reload too probably app:run_hooks('pre_start') print'NEW CQUEUE' local cq = cqueues.new() server.run(cq, app) local function reload(modname) local oldmod = require(modname) assert(type(oldmod) == "table","reloading only works on modules that return tables") package.loaded[modname] = nil local newmod = require(modname) package.loaded[modname]=oldmod assert(type(newmod) == "table","new module doesn't seem to be a table, cancelling") print('','r',modname,oldmod.version,newmod.version) for k in pairs(oldmod) do oldmod[k] = nil end for k,v in pairs(newmod) do oldmod[k] = v end end local function reloader() print('NEW RELOADER') local path = '.' local function modname(filename) return filename:match"^([a-z_]+)%.lua$" end local notifier = cq_notify.opendir(path) local seen = {} local function add(filename) if not seen[filename] then seen[filename]=true print('adding',filename) notifier:add(filename) end end for filename in posix_dirent.files(path) do local m = modname(filename) if m and m ~= 'main' then add(filename) end end for changes, filename in notifier:changes() do pprint('changes',changes,filename) local m = modname(filename) if m then print('!!! reloading ['..m..']') print(pcall(reload, m)) end end end cq:wrap(reloader) assert(cq:loop())