From 270d38e9096706aad07ad818a7f042da5cf625e4 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 27 May 2015 18:33:25 +0200 Subject: [PATCH 1/7] Don't store the `red` variable in redis:connect Apparently, doing so can result in errors like the ones I am seeing in the logs. More info here: https://github.com/openresty/lua-resty-redis/issues/28 (see answer about connect) --- lua/concurredis.lua | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lua/concurredis.lua b/lua/concurredis.lua index b1a98f3..4c01d8b 100644 --- a/lua/concurredis.lua +++ b/lua/concurredis.lua @@ -176,20 +176,11 @@ end concurredis.execute = function(f) - local first_connection = false - if not ngx.ctx.red then - ngx.ctx.red = concurredis.connect() - first_connection = true - end - - local red = ngx.ctx.red + local red = concurredis.connect() local result = { error_handler.execute(function() return f(red) end) } - if first_connection then - red:set_keepalive(KEEPALIVE_TIMEOUT, POOL_SIZE) - ngx.ctx.red = nil - end + red:set_keepalive(KEEPALIVE_TIMEOUT, POOL_SIZE) local ok, err = result[1], result[2] if ok then From ba5d6865d9f4bb7d778d2d9fc9e01e040588fa56 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 29 May 2015 20:22:39 +0200 Subject: [PATCH 2/7] check crontab reschedule status --- lua/crontab.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/crontab.lua b/lua/crontab.lua index 62f564e..7d06d66 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -205,8 +205,12 @@ crontab.schedule = function(timer, offset) local scheduled = dict:add(job_id, timer.id) if scheduled then - ngx.timer.at(delay, crontab.run_and_reschedule, timer, job_id) - ngx.log(ngx.INFO, '[cron] scheduled ' .. timer.id .. ' with as ' .. job_id .. ' in ' .. delay .. ' seconds') + local ok, err = ngx.timer.at(delay, crontab.run_and_reschedule, timer, job_id) + if ok then + ngx.log(ngx.INFO, '[cron] scheduled ' .. timer.id .. ' with as ' .. job_id .. ' in ' .. delay .. ' seconds') + else + ngx.log(ngx.ERR, '[cron] could not execute ngx.timer.at for ' .. timer.id .. ' error: ' .. err ) + end else ngx.log(ngx.ERR, '[cron] could not schedule ' .. timer.id .. ' with as ' .. job_id) end From 93e4ee5d7fd4867f143dda669d040ad890923d92 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 5 Jun 2015 12:54:23 +0200 Subject: [PATCH 3/7] Replace assert by if + ngx.log. Extract delay calculation to a function --- lua/crontab.lua | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lua/crontab.lua b/lua/crontab.lua index 7d06d66..458a855 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -189,30 +189,36 @@ local get_seconds_to_midnight = function() return next_midnight - ngx.now() end +local get_seconds_to_next_execution = function(timer, offset) + offset = offset or 0 + if timer.at == 'midnight' then + return get_seconds_to_midnight() + offset + else + return timer.every + offset + end +end + -- Public functions crontab.schedule = function(timer, offset) - assert(dict:get(timer.id), "can't schedule timer " .. timer.id .. " because it is not initialized") - - local at = 0 - if timer.at == 'midnight' then - at = get_seconds_to_midnight() - end - - local delay = timer.every + at + (offset or 0) -- randomizer offset - local job_id = crontab.uuid(timer) - local scheduled = dict:add(job_id, timer.id) + if dict:get(timer.id) then + local delay = get_seconds_to_next_execution(timer, offset) + local job_id = crontab.uuid(timer) + local scheduled = dict:add(job_id, timer.id) - if scheduled then - local ok, err = ngx.timer.at(delay, crontab.run_and_reschedule, timer, job_id) - if ok then - ngx.log(ngx.INFO, '[cron] scheduled ' .. timer.id .. ' with as ' .. job_id .. ' in ' .. delay .. ' seconds') + if scheduled then + local ok, err = ngx.timer.at(delay, crontab.run_and_reschedule, timer, job_id) + if ok then + ngx.log(ngx.INFO, '[cron] scheduled ' .. timer.id .. ' with as ' .. job_id .. ' in ' .. delay .. ' seconds') + else + ngx.log(ngx.ERR, '[cron] could not execute ngx.timer.at for ' .. timer.id .. ' error: ' .. err ) + end else - ngx.log(ngx.ERR, '[cron] could not execute ngx.timer.at for ' .. timer.id .. ' error: ' .. err ) + ngx.log(ngx.ERR, '[cron] could not schedule ' .. timer.id .. ' with as ' .. job_id) end else - ngx.log(ngx.ERR, '[cron] could not schedule ' .. timer.id .. ' with as ' .. job_id) + ngx.log(ngx.ERR, "can't schedule timer " .. timer.id .. " because it is not initialized") end end From 1574fa523443257dd9cf3da4c0f49715acb0e999 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 5 Jun 2015 13:04:38 +0200 Subject: [PATCH 4/7] Do not initialise crontab inside crontab (do it only in init_worker_by_lua instead) --- lua/crontab.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/crontab.lua b/lua/crontab.lua index 458a855..6e3dc5a 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -393,6 +393,4 @@ crontab.stats = function() } end -crontab.initialize() - return crontab From 41a389df86e039e6751657348397c98529a18b88 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 5 Jun 2015 16:21:23 +0200 Subject: [PATCH 5/7] attempt to make travis happy by adding back crontab.initialize --- lua/crontab.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/crontab.lua b/lua/crontab.lua index 6e3dc5a..458a855 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -393,4 +393,6 @@ crontab.stats = function() } end +crontab.initialize() + return crontab From 6b2e56d2da9fd6d71242c3ffdc6e4bbcc23889e9 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 5 Jun 2015 16:32:44 +0200 Subject: [PATCH 6/7] Revert "attempt to make travis happy by adding back crontab.initialize" This reverts commit 41a389df86e039e6751657348397c98529a18b88. --- lua/crontab.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/lua/crontab.lua b/lua/crontab.lua index 458a855..6e3dc5a 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -393,6 +393,4 @@ crontab.stats = function() } end -crontab.initialize() - return crontab From 9edd6f63383faf16b63f6d0dd6a3c23a0689d433 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 10 Jun 2015 18:21:32 +0200 Subject: [PATCH 7/7] Make the crontab active by default. Remove disable cron and force cron Do not require slug name to run cron tasks --- config/env.conf | 3 --- lua/crontab.lua | 43 +++++++------------------------------------ lua/models/system.lua | 2 +- 3 files changed, 8 insertions(+), 40 deletions(-) diff --git a/config/env.conf b/config/env.conf index 5a7dca4..4875fb9 100644 --- a/config/env.conf +++ b/config/env.conf @@ -4,11 +4,8 @@ env SLUG_ENV; env SLUG_LOGFILE; -env SLUG_DISABLE_CRON; env SLUG_DISABLE_CACHE; -env SLUG_CRON_FORCED; - env SLUG_CSRF_PROTECTION; env SLUG_BRAIN_HOST; diff --git a/lua/crontab.lua b/lua/crontab.lua index 6e3dc5a..25bc24d 100644 --- a/lua/crontab.lua +++ b/lua/crontab.lua @@ -144,7 +144,7 @@ local TIMERS = { } local TIMERS_DICT = {} -for _,timer in ipairs(TIMERS) do TIMERS_DICT[timer.id] = true end +for _,timer in ipairs(TIMERS) do TIMERS_DICT[timer.id] = timer end local dict = ngx.shared.crontab @@ -296,10 +296,6 @@ crontab.randomizer = function(timer) end crontab.initialize = function() - crontab.enable() - - if crontab.disabled then return end - -- it wont run initialize when locked crontab.block(function() ngx.log(ngx.INFO, '[cron] initializing') @@ -327,35 +323,18 @@ crontab.flush = function() end) end -crontab.timer = function(id) - for _,timer in ipairs(TIMERS) do - if timer.id == id then return timer end - end -end - -local has_slug_name = function() - local Config = require 'models.config' - return Config.get_slug_name() -end - -crontab.enabled = function() - return not crontab.disabled and (crontab.forced or has_slug_name()) +crontab.get_timer = function(id) + return TIMERS_DICT[id] end crontab.run = function(timer, job_id) ngx.log(ngx.INFO, '[cron] running ' .. timer.id .. ' job ' .. job_id) - local forced = job_id == 'forced' or job_id == 'manual' - - if forced or crontab.enabled() then - crontab.lock(function() - statsd.timer('cron.' .. timer.id, function() - error_handler.execute(timer.action) - end) + crontab.lock(function() + statsd.timer('cron.' .. timer.id, function() + error_handler.execute(timer.action) end) - else - ngx.log(ngx.INFO, '[cron] skipping run of ' .. timer.id .. ' job ' .. job_id .. ': slug name not set' ) - end + end) ngx.log(ngx.INFO, '[cron] finished ' .. timer.id .. ' job ' .. job_id) @@ -368,17 +347,10 @@ crontab.shutdown = function() end crontab.halt = function() - crontab.disabled = true - crontab.forced = false dict:flush_all() dict:flush_expired() end -crontab.enable = function() - crontab.disabled = os.getenv('SLUG_DISABLE_CRON') - crontab.forced = os.getenv('SLUG_CRON_FORCED') -end - crontab.reset = function() ngx.log(ngx.INFO, 'crontab reset') crontab.halt() @@ -387,7 +359,6 @@ end crontab.stats = function() return { - disabled = crontab.disabled and 1 or 0, timers = #TIMERS, jobs = get_jobs() } diff --git a/lua/models/system.lua b/lua/models/system.lua index f7500ed..e83801a 100644 --- a/lua/models/system.lua +++ b/lua/models/system.lua @@ -71,7 +71,7 @@ System.log = function(block) end System.run_timer = function(timer_id) - local timer = crontab.timer(timer_id) + local timer = crontab.get_timer(timer_id) if timer then crontab.run(timer, 'manual')