Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Timed event within a script

by Marshall (Canon)
on Mar 09, 2017 at 01:13 UTC ( [id://1183973]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Timed event within a script
in thread Timed event within a script

I also wish it was a Linux machine so I can use cron

FYI, Windows does have a chron "equivalent". It is called Task Scheduler. It has both a Windows and a command line interface. Something like "run program X every 30 minutes" is possible. I've used Task Scheduler to periodically run a Perl script before without problems.

Update with a few comments and considerations: I wouldn't necessarily interpret the user request for "one program" as a hard and fast design implementation limitation. It appears that the main point appears to be something reliable. Simple often, but not always yields reliable.

For the "wait for emergency" function, you could have a simple loop that does blocking I/O on that port while waiting for the "Emergency" message. If you get messages fairly regularly that are not "emergencies", blocking I/O would fine fine.

For the once per 30 minute "watchdog on the monitor for emergency" function. In applications that I've worked on, more than the "process is running" is often required. The thing to be verified is: "is the thing that is monitoring for an emergency" really doing actual work(processing messages looking for an emergency)? You could have the main monitoring function do something that is observable by the "watchdog" process. Perhaps simply incrementing the name of a zero length file for every message it looks at? If the watchdog function determines that no messages are being processed by the monitoring function, it would conclude that something is amiss in addition to making sure that the monitor process is actually "running", i.e. in the process table.

Replies are listed 'Best First'.
Re^4: Timed event within a script
by bajangerry (Sexton) on Mar 09, 2017 at 20:18 UTC

    Thanks for your input. I have been trying to wrap my head around the best way to do this. The problem I have is that even though there has not been a message from the device for hours it doesn't mean there is actually a problem, it is more likely that there is NOT a problem in that case. It would be nice if there was a periodic "keep alive" type message from the device but I am not that lucky.

    Simply put, the device shoots out a message on a IP port whenever there is an error event and only then. It does not care if there is 1 or 100 devices monitoring the port either

      Geez. One can only hope that this "emergency message" doesn't mean something like "reactor coolant low"!

      To make this into a single process, you will need to determine whether or not the socket for the device has a line for you to process in it or not. I think that you will need IO::Socket. This sort of thing would be done in what is called a "select server". Don't read from the socket unless you know that it has something to say.

      I think this psuedo code would be fine in your application.

      # open file handles for reading and reporting # set $since_report_secs (seconds) = 0 while (sleep (1)) { my $emergency = check_for_emergency(); if ($emergency) { do something; } report_awake_status(); } sub check_for_emergency { while (device port ready to read) { read line and check it for "emergency" return 1 if emergency; } return 0; # "no error"; } sub report_awake_status { $last_report_time++; if (more than 30 minutes) { send awake message, set counter back to zero } }
      The above code essentially sets up a "polling loop". This is not as efficient as it "could be". But the logic for a simple single process is simple and I think that you will find that code like this doesn't take much CPU time.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1183973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found