I have a POE job server that I wrote using POE's POE::Filter::Reference and POE::Wheel::Run. (Note: This is my first attempt with POE). Everything runs just fine with regard to grabbing the next task from the MySQL server and starting the task, running the task, and so on.

The issue I am having is that every hour (or even after X processes), I want to run some cleanup methods. I am currently running them as cronjobs through the shell. This method works well enough, but I want some of the information from the parent process to be accessible to the cleanup code.

How can I create a session that runs off the parent that will either inject itself every X processes and run the cleanup code OR create a separate session that runs the cleanup code, sleeps, repeat OR create a separate session off the parent that runs every hour. All of this needs to happen in a non-blocking state of the other jobs.

Here is some relevant code (everything below works):

sub handle_task_result { my ($result) = $_[ARG0]; $LOG->debug("Result: $result"); } sub handle_task_debug { my ($result) = $_[ARG0]; $LOG->debug("Debug: $result"); } sub handle_task_done { my ($kernel, $heap, $task_id) = @_[ KERNEL, HEAP, ARG0 ]; delete $heap->{task}->{$task_id}; $kernel->yield("next_task"); } sub Run_Job { my ($task) = shift; my ($filter) = POE::Filter::Reference->new(); # ... stuff ... return; } sub start_tasks { my ($heap) = $_[HEAP]; while ( keys( %{ $heap->{task} } ) < MAX_CONCURRENT_TASKS ) { # Returns an array_ref from the select call to the job table my $next_task = Get_Next_Task(); # If we don't have a task, sleep and one will come do { sleep 10; next; } unless exists $next_task->{'id'}; $LOG->debug("Starting task for $next_task->{'url'}...\n"); my ($task) = POE::Wheel::Run->new( Program => sub { Run_Job($next_task) }, StdoutFilter => POE::Filter::Reference->new(), StdoutEvent => "task_result", StderrEvent => "task_debug", CloseEvent => "task_done", ); $heap->{task}->{ $task->ID } = $task; } } # Setup the POE session if we are in daemon mode POE::Session->create( inline_states => { _start => \&start_tasks, next_task => \&start_tasks, task_result => \&handle_task_result, task_done => \&handle_task_done, task_debug => \&handle_task_debug, } ); POE::Kernel->run();

In reply to POE Sleep Process by madbombX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.