GithubHelp home page GithubHelp logo

guoyu07 / lua-resty-worker-manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kong/lua-resty-worker-manager

0.0 0.0 0.0 20 KB

Tracks worker processes and nodes starting/restarting/reloading/stopping

Home Page: https://kong.github.io/lua-resty-worker-manager/topics/readme.md.html

License: Apache License 2.0

Makefile 1.06% Lua 27.19% Perl 71.75%

lua-resty-worker-manager's Introduction

lua-resty-worker-manager

Tracks workers and nodes starting/restarting/reloading/stopping.

Status

This library is still under early development, and currently lacks tests.

Synopsis

http {
    lua_shared_dict my_shm 5m;

    init_by_lua_block {
        require("resty.worker.manager").on_init_by_lua({
                shm = "my_shm",
                interval = 10,  -- worker heartbeat interval in seconds
                failed = 5, -- max heartbeat overrun to detect crashed worker, in seconds
            },
            function(master_id, n_prev, n_mine, n_next)
                ngx.log(ngx.ERR, "================================= Master start =================================")
                if n_prev == nil then
                    -- no previous master, so this is nginx start or restart
                else
                    -- previous master, so we are reloading
                    if n_prev == 0 then
                        -- all old workers have already exited
                    else
                        -- stil 'n_prev' old workers are running
                    end
                end
            end,
            function(master_id, n_prev, n_mine, n_next)
                ngx.log(ngx.ERR, "================================= Master exit ==================================")
                if n_next == nil then
                    -- we are stopping this nginx node
                else
                    -- we are only reloading, all our workers have already exited, this code
                    -- runs on the last worker.
                    if n_next > 0 then
                        -- new workers have already been initialized
                    end
                end
            end)
        kong = require 'kong'
        kong.init()
    }

    init_worker_by_lua_block {
        require("resty.worker.manager").on_init_worker_by_lua(
            function(worker_id, n_prev, n_mine, n_next)
                ngx.log(ngx.ERR, "--------------------------------- Worker start ---------------------------------")
                if n_mine == 1 then
                    -- I'm the very first worker to start
                elseif n_mine > ngx.worker.count() then
                    -- a worker failed and exited, this a respawned worker.
                    -- After the failed worker heart-beat times out then
                    -- n_mine == ngx.worker.count() again
                else
                    -- other workers have started before me
                end
            end,
            function(worker_id, n_prev, n_mine, n_next)
                ngx.log(ngx.ERR, "--------------------------------- Worker exit ----------------------------------")
                if n_next then
                    -- we are reloading nginx
                end
                if n_mine == 1 then
                    -- I'm the very last worker to exit
                else
                    -- I'm not the last worker, there are more
                end
            end)
        kong.init_worker()
    }
}

Description

Within Nginx/OpenResty it is not always easy to detect workers starting/stopping and to distinguish between starting/restarting/reloading/stopping the server. That is what this module does. It detects those, and allows to run synchonized code (protected within a lock) when it does

See the online LDoc documentation for the complete API.

History

0.1 (xx-Nov-xx) Initial release

  • Initial upload

Copyright and License

Copyright 2017 Kong Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

lua-resty-worker-manager's People

Contributors

tieske 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.