Lua Looping Script

36 views
Skip to first unread message

Don Bozarth

unread,
Sep 16, 2016, 9:01:43 PM9/16/16
to openresty-en
How can I have a Lua script continuously loop.

Example:

I am building a system that will monitor the state of external switches from devices. In order to catch the state change I would need to create an event loop that every so often would check the state. If the state changes then do something.

I may want a sequence of things to happen if the state changes. Then return and the loop continues. Like scan.

I may need several loops all running simultaneously watching various things. Returning variables etc.

I have never run Lua as server app. But know Lua quite well. I use it now for controlling industrial machines like a PLC.

Any help is appreciated.

Josh Enders

unread,
Sep 16, 2016, 9:35:21 PM9/16/16
to openresty-en
This is a great question but it may be better answered by a general purpose Lua mailing list. Is there some interaction you're planning with OpenResty in particular? 

bjoe2k4

unread,
Sep 17, 2016, 4:38:37 AM9/17/16
to openresty-en
You can use nginx.lua timers for that. Please have a look into the documentation, it is really well documented. Also look at https://github.com/openresty/lua-resty-upstream-healthcheck for an example.

Don Bozarth

unread,
Sep 17, 2016, 7:30:20 AM9/17/16
to openresty-en
Actually no. Lua is not the issue here. Lua can do this kind of function very easily. The question relates to running this type function where I continually loop a program every 1ms to check the status of physical world switches and have action callad against the switch state. 

Don Bozarth

unread,
Sep 17, 2016, 7:33:59 AM9/17/16
to openresty-en
bjoe2k4,

I looked at it. It seems like this could work except this is a configuration file. So, what you are saying is I would need to create a new configuration that spawns this worker? I may 500 of these working and then one checking the 500. Would one need 500 config settings? Please excuse my ignorance. I am a robotics engineer. I am learning servers. :-)

Don Bozarth

unread,
Sep 17, 2016, 7:42:53 AM9/17/16
to openresty-en
Example of a check of the state. I use this in my current application but it runs locally on a PC not as part of a server. 

[mc.ISIG_INPUT1] = function (state)
 if (state == 1) then   
       CycleStart()
    --else
        --mc.mcCntlFeedHold (0
 end

end,

On Saturday, September 17, 2016 at 3:38:37 AM UTC-5, bjoe2k4 wrote:

Don Bozarth

unread,
Sep 17, 2016, 7:48:18 AM9/17/16
to openresty-en
Sorry,

Better example:

SigLib = {
   --Enable
    [mc.OSIG_MACHINE_ENABLED] = function (state)
        machEnabled = state;
    end,
    [mc.ISIG_INPUT1] = function (on_off)--mc.ISIG_INPUT1
        if( on_off==1 ) then
            -- On
        else
            -- Off
        end
    end,
    [mc.ISIG_INPUT2] = function (state)--mc.ISIG_INPUT2
        if( state == 1 ) then
            -- On 
        else
            -- Off Call A function here if you wanted
        end
    end,
--The user can add additional Signal events if they wish by following the same format as above.
--[SIGNAL NAME] = function(state)
--if(state is active) then
-- do active code
--else
-- do inactive code
--end
--end,
}


On Saturday, September 17, 2016 at 3:38:37 AM UTC-5, bjoe2k4 wrote:
Reply all
Reply to author
Forward
0 new messages