{"action":"created","issue":{"url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/734","repository_url":"https://api.github.com/repos/openresty/lua-nginx-module","labels_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/734/labels{/name}","comments_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/734/comments","events_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/734/events","html_url":"https://github.com/openresty/lua-nginx-module/issues/734","id":147211976,"number":734,"title":"Sharing data within a worker, with a timer","user":{"login":"lightpriest","id":351445,"avatar_url":"https://avatars.githubusercontent.com/u/351445?v=3","gravatar_id":"","url":"https://api.github.com/users/lightpriest","html_url":"https://github.com/lightpriest","followers_url":"https://api.github.com/users/lightpriest/followers","following_url":"https://api.github.com/users/lightpriest/following{/other_user}","gists_url":"https://api.github.com/users/lightpriest/gists{/gist_id}","starred_url":"https://api.github.com/users/lightpriest/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lightpriest/subscriptions","organizations_url":"https://api.github.com/users/lightpriest/orgs","repos_url":"https://api.github.com/users/lightpriest/repos","events_url":"https://api.github.com/users/lightpriest/events{/privacy}","received_events_url":"https://api.github.com/users/lightpriest/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-10T10:25:24Z","updated_at":"2016-04-11T07:32:59Z","closed_at":null,"body":"I'm having a small issue which rarely happens, but I'm not sure I understand why it happens. I'm using OpenResty 1.7.10.1 with Lua 0.9.16.\r\n\r\nI'm using a timed function (timer.at) to share data to incoming requests. The timer periodically reads a JSON file and updates a module. At the worker's requests, I'm using set_by_lua to set a variable that determines which proxy upstream to choose. My problem is that at some point in time, which I haven't figured out when, the workers are seeing an old copy of the data - i.e. they adhere to rules that are no longer in the file. Reloading NGINX solves the issue.\r\n\r\nAt first I thought that this was a problem with the rule logic, but we have a few NGINX servers that only a some of them are having this problem. Any ideas on why this even happens? Should I just use a shared.DICT and update it from a single worker (or from an endpoint request?).\r\n\r\nThe timer is initiated by this init_by_lua:\r\n```\r\ninit_worker_by_lua '\r\n  local timed_function\r\n  timed_function = function(pre)\r\n    -- Run in \"protected call (pcall)\" to catch errors\r\n    if not pre then\r\n      local status, err = pcall(\r\n        function()\r\n          local cjson = require \"cjson\"\r\n          local rules_module = require \"rules_module\"\r\n\r\n          rules_module.set(cjson.decode(io.open(\"/path/to/rule.json\", \"r\"):read(\"*all\")))\r\n        end\r\n      )\r\n\r\n      if not status then\r\n        ngx.log(ngx.ERR, \"error in timed_function: \", err)\r\n      end\r\n    end\r\n\r\n    ngx.timer.at(30, timed_function)\r\n  end\r\n\r\n  ngx.timer.at(0, timed_function)\r\n';\r\n```\r\n\r\nThis is the module:\r\n```\r\nlocal _M = {}\r\n\r\nlocal rules = {}\r\n\r\nfunction _M.set(data)\r\n  local cjson = require(\"cjson\")\r\n  local new_rules = {}\r\n\r\n  for id, rule in pairs(data) do\r\n    new_rules[id] = cjson.decode(rule)\r\n  end\r\n\r\n  rules = new_rules\r\nend\r\n\r\nfunction _M.get()\r\n  return rules\r\nend\r\n\r\nreturn _M\r\n```\r\n\r\nAnd this is what the worker runs:\r\n```\r\nlocation / {\r\n  ..\r\n  ..\r\n  \r\n  set_by_lua $backend_proxy '\r\n    local rules_module = require \"rules_module\"\r\n    local rules = rules_module.get()\r\n\r\n    for _, rule in pairs(rules) do\r\n      -- Some rule logic that sets the returned variable\r\n    end\r\n\r\n    return ngx.var.default_backend\r\n  ';\r\n}\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/comments/208202249","html_url":"https://github.com/openresty/lua-nginx-module/issues/734#issuecomment-208202249","issue_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/734","id":208202249,"user":{"login":"lightpriest","id":351445,"avatar_url":"https://avatars.githubusercontent.com/u/351445?v=3","gravatar_id":"","url":"https://api.github.com/users/lightpriest","html_url":"https://github.com/lightpriest","followers_url":"https://api.github.com/users/lightpriest/followers","following_url":"https://api.github.com/users/lightpriest/following{/other_user}","gists_url":"https://api.github.com/users/lightpriest/gists{/gist_id}","starred_url":"https://api.github.com/users/lightpriest/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lightpriest/subscriptions","organizations_url":"https://api.github.com/users/lightpriest/orgs","repos_url":"https://api.github.com/users/lightpriest/repos","events_url":"https://api.github.com/users/lightpriest/events{/privacy}","received_events_url":"https://api.github.com/users/lightpriest/received_events","type":"User","site_admin":false},"created_at":"2016-04-11T07:32:59Z","updated_at":"2016-04-11T07:32:59Z","body":"@qlee001 The data is JSON serialized, but that's beside the point. How will not serializing help here? I mean, since I'm translating a file to a Lua table, I must serialize it somehow."}}