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

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.

Replies are listed 'Best First'.
Re: Async timed events with Dancer2
by Anonymous Monk on Sep 25, 2016 at 02:17 UTC
      Thanks. I'll give that a try. If that doesn't work, I'll need to get a better understanding of how Dancer2 works under the hood.
Re: Async timed events with Dancer2
by stevieb (Canon) on Sep 25, 2016 at 14:32 UTC

    OK, so an update here. I have removed the fork() stuff. The problem is that when I create an object in my webapp, it goes out of scope immediately as the webapp is presented with plackup. Here's a much trimmed down repro example:

    # Scope.pm in webapp root dir package Scope; sub new { return bless {}, shift } sub DESTROY { print "going out of scope...\n"; } # webapp package MyApp; use lib '.'; use Scope; use Dancer2; use Dancer2::Plugin::Database; our $VERSION = '0.1'; my $obj = Scope->new;

    Now run it:

    plackup -R lib,bin bin/app.pl

    Here's what happens when I run it:

    Watching lib bin bin/lib bin/app.pl for file updates. going out of scope HTTP::Server::PSGI: Accepting connections at http://0:5000/

    I've also asked for guidance on the #dancer IRC channel. I'm hoping someone with experience with Dancer2 (or perhaps web frameworks in general) can help me get a grasp on getting around this issue (as I pour through resources I'm finding online elsewhere).