Update: I've solved the issue. Everything defined/created in the app that is NOT referenced within at least one route goes out of scope when the app is started. If something is referenced within a route even just once, it lives. Even my forking code works now :)

I'm still working on my Dancer2 application, but now I'm at the point where I need it to periodically to something, even without client interaction. Essentially, I want the Dancer2 app to behave like a normal always-running background script while a client browser is not attached.

So, what I need to do is have this app periodically do something without tying up the main application. I wrote a *very* basic async timed event module Async::Event::Interval (yeah, for fun, mostly). It works, and does what I need it to do (the event is a forked process). Here's an example:

use warnings; use strict; use Async::Event::Interval; my $event = Async::Event::Interval->new( 1, # interval, in seconds sub { print "event...\n"; }, ); $event->start; sleep 1; # wait for first event to fire print "main is going to sleep...\n"; sleep 5; print "done\n";

Output:

event... main is going to sleep... event... event... event... event... event... done

I fetch the PID of the event process, and it shows up just dandy as running in the proc list:

$ ps ax | grep 22834 22834 pts/2 S+ 0:00 perl t/evt.pl

However, no matter where it is I create and start the event in a Dancer2 app, it fires after the first interval is expired, but then in the proc list I see:

$ps ax | grep 22732 22732 pts/2 Z+ 0:00 [/home/spek/repo] <defunct>

Can anyone explain what Dancer2 is doing that forces a child process to become defunct, or how I can get around/avoid this problem without having to have a second background app running to do the side-work that is needed? If anyone thinks a different async type software will work, I'll ditch my own, as it was just for playing around anyways.

As a side note, I have no idea why it's showing /home/spek/repo in the proc entry, as my current working dir is ~/repo/app-envui when I start the webapp.


In reply to [SOLVED]: Async timed events with Dancer2 by stevieb

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.