Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Question about communicating with a running perl script

by Amblikai (Scribe)
on Jul 06, 2015 at 17:44 UTC ( [id://1133400]=perlquestion: print w/replies, xml ) Need Help??

Amblikai has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks, i'm not even sure where to begin on this one (which makes it hard to search for solutions)

Basically i have a perl script which runs in the background performing various operations on an SQL database using DBI (love that module!).

Separately, i have a website which is essentially just displaying the information in the sql database. It can however, modify the contents of the SQL database.

The perl script running in the background has various "steps" which it goes through. I'd like to be able to control these steps through the web front end.

The obvious first thought i had was polling a table in the SQL database but i wondered if there was a simpler or more elegant solution?

Can anyone point me in the right direction?

Thank you all!!

  • Comment on Question about communicating with a running perl script

Replies are listed 'Best First'.
Re: Question about communicating with a running perl script
by GotToBTru (Prior) on Jul 06, 2015 at 18:01 UTC

    Personally, I like the idea of using the website to update a control table in the database, which the perl script consults to determine its behavior.

    Dum Spiro Spero

      Thanks for your reply!

      So you reckon dedicating a table in the DB where sending "status" values or something and getting the background script to continue polling it?

      What's a realistic polling frequency?

        What's a realistic polling frequency?

        That's a question only the users of the website can answer, I would think. How often is the website refreshing? How long does it take the perl program to run thru the list of tasks?

        Dum Spiro Spero
Re: Question about communicating with a running perl script
by mhearse (Chaplain) on Jul 06, 2015 at 20:35 UTC
      Also, In addition to the other suggested solutions, classic UNIX dogma includes signals. You could add a signal handler to your code. That re-reads and applies the configuration info stored in db. The hangup signal is, by tradition, usually chosen for reloading configs.
Re: Question about communicating with a running perl script
by kcott (Archbishop) on Jul 08, 2015 at 02:04 UTC

    G'day Amblikai,

    You might consider using Named Pipes for this task.

    The basic idea would be to have your SQL (background) script set up the named pipe. It would check if there were any directives (i.e. specific steps to be executed) to be read from the pipe. If so, it would read, and then perform, these in whatever order they arrived (FIFO). If not (or when all piped directives had been exhausted), it would go back to its default list of steps.

    Your WEB script would be writing these directives to the named pipe; I assume on a somewhat ad hoc basis. The directives, themselves, could be step numbers or step names; or, potentially, something more complex like commands with options and arguments.

    I've put together a couple of test scripts to give you an idea of the techniques involved. There's no DBI or CGI code here; really just ideas for you to modify and adapt to whatever code you currently have. Also, in total, they're quite lengthy, so I've put each in spoiler tags.

    First, pm_1133400_sim_sql.pl, which is intended to simulate your SQL (background) script.

    Next, pm_1133400_sim_web.pl, simulates your WEB script.

    You'll see that both scripts use a PM_1133400_Shared module. That was a convenience for me; you may want something entirely different (e.g. a config file). However, you will, at the very least, need some mechanism that allows both scripts to know the filename of the named pipe (and I recommend you store that information once). Here's PM_1133400_Shared.pm:

    Now, if you save those three files in one directory, and make the scripts executable, you can test it like this (to run the SQL script in the background and the WEB script in the foreground):

    pm_1133400_sim_sql.pl & pm_1133400_sim_web.pl

    The WEB script operates randomly. In some cases, it might start issuing directives before the named pipe is created, so it will put those in a queue and write them to the pipe later:

    WEB_SIM: FIFO Unavailable! Queueing step [5]. SQL_SIM: Executing normal step: Step 0 SQL_SIM: Executing normal step: Step 1 WEB_SIM: FIFO Available! Piping from queue [5] ... SQL_SIM: Executing web request: Step 5

    Other times, the SQL script may have performed some of its normal steps before any WEB script directive is issued:

    SQL_SIM: Executing normal step: Step 0 SQL_SIM: Executing normal step: Step 1 WEB_SIM: FIFO Available! Piping new step [4] SQL_SIM: Executing web request: Step 4 SQL_SIM: Executing normal step: Step 5 SQL_SIM: Executing normal step: Step 6 ...

    And, as you can see from that last sample extract, WEB can cause the steps to be performed out-of-order ([0, 1, 4, ...]) but, when those are completed, SQL will continue on in-order ([..., 4, 5, 6]).

    I've only used builtin functions, modules and pragmata; you can find documentation for everything via perldoc. Of course, if you have other questions, feel free to ask.

    -- Ken

Re: Question about communicating with a running perl script
by sundialsvc4 (Abbot) on Jul 06, 2015 at 18:09 UTC

    A fairly common technique is to use an SQL database to contain a job-queue, as well as step-control, time-stamping and status logging related to each job.   As the workers complete a step, they (in an SQL transaction ...) update the status table for the job that they are working on and perhaps alter their flow in some way.   For user status-reporting, a simple polling loop will usually suffice (the reads also being done within a transaction so that the data being read is atomically correct).

    There are a number of batch job scheduling frameworks available in CPAN for Perl.

      Thanks for the info, could you point me to one of the frameworks you mentioned?

        Just search "job queue" on http://metacpan.org. I have used TheSchwartz with some success before but it has been quite a long time and there are lots of new ones in the search results I do not know and might be worth trying/comparing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1133400]
Approved by GotToBTru
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found