Giter Club home page Giter Club logo

ngx_tasks's Introduction

ngx_tasks

The management support features:

  • preemption single worker tigger task in all nginx workers.
  • per-worker/per-request drive mode in worker processes.
  • re-write reponse of request in task.
  • custom keys/key generator in dictionary.

Installation

download the ngx_tasks.lua file and put into lua search path or current directory.

load it as a file module from lua(in nginx only).

the module need shared_dict support in ngxin.conf.

Base usage

tasks = require('ngx_tasks')
tasks.dict = 'your_shared_dict_config_in_<nginx.conf>'
tasks:push(rule)
...
tasks:run()

Rules import

tasks = setmetatable(require('rules'), {
    __index = require('ngx_tasks')
})
tasks.dict = 'your_shared_dict_config_in_<nginx.conf>'
tasks:run()

Rule/rules

a rules is array(table) of rule. a rule is a record(table) with callback() method:

rule = {
    name = 'test ding',
    identifier = 'testDing',
    interval = 60,      -- second
    typ = 'normal',     -- normal/preemption
    callback = function(self)
		ngx.log(ngx.ALERT, 'DING')
    end
}

rules = {
    rule,
    ...
}

-- push rule into tasks
tasks = require('ngx_tasks')
for _, rule in ipairs(rules) do
    tasks.push(rule)
end
...

Task types: 'normal'/'preemption'

if typ is 'normal', tigger task by interval time for each worker. else 'preemption', tigger task for first active worker only(on interval timeout).

for sample case, if you want DING per-worker at interval 10s, and DONG once at interval 1m. next is task rules:

function dingdong(self)
    ngx.log(ngx.ALERT, self.identifier)
end

rules = {
    {
        identifier = 'TRYDING',
        interval = 10,
        typ = 'normal',
        callback = dingdong
    },
    {
        identifier = 'TRYDONG',
        interval = 60,
        typ = 'preemption',
        callback = dingdong
    }
}

How to drive these tasks?

the task manager need a drive cycle/behavior. you need update nginx.conf to implement it.

  1. case 1, create per-worker reoccurring timers
http {
	lua_shared_dict PreemptionTasks 10M;
	## see: http://wiki.nginx.org/HttpLuaModule#init_worker_by_lua
    init_worker_by_lua '
        tasks = require('ngx_tasks');
        ngx.timer.at(60, tasks.run, tasks);
    ';
  1. cast 2, (or, ) create per-request tasks scheduling
http {
	lua_shared_dict PreemptionTasks 10M;
    init_worker_by_lua '
        tasks = require('ngx_tasks');
    ';
	# trigger on request's response time
	rewrite_by_lua '
	    tasks:run();
    ';

choice per-request or per-worker?

for per-request task trigger, you can rewrite reponse body of request, but do not in per-worker mode. because per-worker mode drive by ngx.timer.at:

timer callbacks run in the background and their running time will not add to any client request's response time, ... see: http://wiki.nginx.org/HttpLuaModule#ngx.timer.at

what's keys/key generator and how do

ngx_tasks will write shared dictionary with the key/keys. the management include a default generator. but you can do better. there are a example:

tasks = require('ngx_tasks')
-- tasks.dict = 'PreemptionTasks'  -- default
tasks.keys = function(self, task, now)
    return math.floor((now - self.base_time)/task.interval), task.identifier .. 'Preemption'
end
tasks.base_time = global_start_time
tasks:run()

the generator is faster. but you need update nginx.conf (insert a lua code line into init_by_lua content):

http {
	lua_shared_dict PreemptionTasks 10M;
    init_by_lua '
        global_start_time = os.time()
    ';

ngx_tasks's People

Contributors

aimingoo avatar

Watchers

mingfeng.zhang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.