Software /
code /
prosody
Changeset
8600:96f20cf92b84
util.async: Add per-runner ids and add runner:log() method
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 16 Mar 2018 14:59:41 +0000 |
parents | 8599:62bfc85a53c8 |
children | 8601:ce5b16e13573 |
files | util/async.lua |
diffstat | 1 files changed, 10 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/util/async.lua Fri Mar 16 14:51:24 2018 +0000 +++ b/util/async.lua Fri Mar 16 14:59:41 2018 +0000 @@ -1,4 +1,5 @@ local log = require "util.logger".init("util.async"); +local new_id = require "util.id".short; local function checkthread() local thread, main = coroutine.running(); @@ -99,13 +100,14 @@ local empty_watchers = {}; local function runner(func, watchers, data) return setmetatable({ func = func, thread = false, state = "ready", notified_state = "ready", - queue = {}, watchers = watchers or empty_watchers, data = data } + queue = {}, watchers = watchers or empty_watchers, data = data, id = new_id() } , runner_mt); end -- Add a task item for the runner to process function runner_mt:run(input) if input ~= nil then + self:log("debug", "queued new work item, %d items queued", #self.queue); table.insert(self.queue, input); end if self.state ~= "ready" then @@ -116,6 +118,7 @@ local q, thread = self.queue, self.thread; if not thread or coroutine.status(thread) == "dead" then + self:log("debug", "creating new coroutine"); -- Create a new coroutine for this runner thread = runner_create_thread(self.func, self); self.thread = thread; @@ -124,6 +127,7 @@ -- Process task item(s) while the queue is not empty, and we're not blocked local n, state, err = #q, self.state, nil; self.state = "running"; + self:log("debug", "running main loop"); while n > 0 and state == "ready" do local consumed; -- Loop through queue items, and attempt to run them @@ -156,6 +160,7 @@ -- Runner processed all items it can, so save current runner state self.state = state; if err or state ~= self.notified_state then + self:log("debug", "changed state from %s to %s", self.notified_state, err and "error" or state); if err then state = "error" else @@ -172,4 +177,8 @@ table.insert(self.queue, input); end +function runner_mt:log(level, fmt, ...) + return log(level, "[runner %s] "..fmt, self.id, ...); +end + return { waiter = waiter, guarder = guarder, runner = runner };