Anyone know if JApplicationDaemon works? Topic is solved

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 103
Joined: Wed Jan 20, 2016 10:58 pm

Anyone know if JApplicationDaemon works?

Post by david0058 » Sat Dec 31, 2022 12:40 pm

I'm assuming it probably does, but I can't find any information on how to use it.

From reading the source code it's not a million miles from JApplicationCli, which works perfectly, but there must be some secret sauce to be applied to get it working.

Here's my simple test programme:

Code: Select all

<?php

const _JEXEC = 1;

if (file_exists(dirname(__DIR__) . '../../defines.php'))
    require_once dirname(__DIR__) . '../../defines.php';

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', dirname(__DIR__) . "/../..");
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';

class LaunchCLI extends JApplicationCli
{
    public function doExecute()
    {   
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('COUNT(*) AS count')->from($db->quoteName('#__content'));
        $db->setQuery($query);
        $result = $db->loadObject();
        $this->out("pages: " . $result->count);
    }
}

class LaunchDaemon extends JApplicationDaemon
{
    public $name='test';

    public function doExecute()
    {   
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('COUNT(*) AS count')->from($db->quoteName('#__content'));
        $db->setQuery($query);
        $result = $db->loadObject();
        $this->out("pages: " . $result->count);
    }
}

$options = getopt("d");

try {
    if (array_key_exists('d', $options))
        JApplicationDaemon::getInstance('LaunchDaemon')->execute();
    else
        JApplicationCli::getInstance('LaunchCLI')->execute();
}
catch (Exception $e){
    echo $e->getMessage() . "\n";
}

?>
The JApplicationCli invocation works fine:

Code: Select all

$ php test.php
pages: 1
$ 
but the JApplicationDaemon one isn't working:

Code: Select all

$ php test.php -d
PHP Warning:  pcntl_signal(): Specified handler "DaemonApplication::signal" is not callable (class 'DaemonApplication' not found) in /var/www/game.conlucra.com/public_html/libraries/src/Application/DaemonApplication.php on line 886
Running Joomla 3.10.11

Any ideas gratefully received :)

David

david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 103
Joined: Wed Jan 20, 2016 10:58 pm

Re: Anyone know if JApplicationDaemon works?

Post by david0058 » Sat Dec 31, 2022 6:19 pm

Just in case it's any help ...

The "missing class" warning is generated in the pcntlSignal() method of the DaemonApplication class in JPATH_BASE/libraries/src/Application/DaemonApplication.php.

So essentially the attempt to attach the static function DaemonApplication::signal() as the signal handler for any signal fails, because it can't find it's own parent class ??? I don't even understand how this can be ...

However, this is the most important function for my application - I want to send the daemon SIGUSR1 and SIGUSR2 to get it to invoke the corresponding signal handlers in my own daemon class.

Code: Select all

<?php
namespace Joomla\CMS\Application;

[snip]

use Joomla\Registry\Registry;

/**
 * Class to turn CliApplication applications into daemons.  It requires CLI and PCNTL support built into PHP.
 *
 * @link   https://www.php.net/manual/en/book.pcntl.php
 * @link   https://www.php.net/manual/en/features.commandline.php
 * @since  1.7.0
 */
class DaemonApplication extends CliApplication
{

[snip]

    /**
     * Method to attach the DaemonApplication signal handler to the known signals.  Applications
     * can override these handlers by using the pcntl_signal() function and attaching a different
     * callback method.
     *
     * @return  boolean
     *
     * @since   1.7.0
     * @see     pcntl_signal()
     */
    protected function setupSignalHandlers()
    {   
        // We add the error suppression for the loop because on some platforms some constants are not defined.
        foreach (self::$signals as $signal)
        {   
            // Ignore signals that are not defined.
            if (!defined($signal) || !is_int(constant($signal)) || (constant($signal) === 0))
            {   
                // Define the signal to avoid notices.
                \JLog::add('Signal "' . $signal . '" not defined. Defining it as null.', \JLog::DEBUG);
                define($signal, null);

                // Don't listen for signal.
                continue;
            }

            // Attach the signal handler for the signal.
            if (!$this->pcntlSignal(constant($signal), array('DaemonApplication', 'signal')))
            {   
                \JLog::add(sprintf('Unable to reroute signal handler: %s', $signal), \JLog::EMERGENCY);

                return false;
            }
        }

        return true;
    }

[snip]

    /**
     * Method to install a signal handler.
     *
     * @param   integer   $signal   The signal number.
     * @param   callable  $handler  The signal handler which may be the name of a user created function,
     *                              or method, or either of the two global constants SIG_IGN or SIG_DFL.
     * @param   boolean   $restart  Specifies whether system call restarting should be used when this
     *                              signal arrives.
     *
     * @return  boolean  True on success.
     *
     * @see     pcntl_signal()
     * @since   1.7.3
     */
    protected function pcntlSignal($signal, $handler, $restart = true)
    {   
        return pcntl_signal($signal, $handler, $restart);    <<<<<<<<<< FAIL
    }

[snip]

}

david0058
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 103
Joined: Wed Jan 20, 2016 10:58 pm

[SOLVED] Re: Anyone know if JApplicationDaemon works?

Post by david0058 » Tue Feb 07, 2023 11:07 am

Appears to be a bug, and also in J4.0.

"DemonApplication sets incorrect class as signal handler." https://issues.joomla.org/tracker/joomla-cms/39533

Maybe there'll be a fix in some future version.

David

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 24986
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Anyone know if JApplicationDaemon works?

Post by pe7er » Tue Feb 07, 2023 11:59 am

Thanks for your follow-up with reference to the issue!
Kind Regards,
Peter Martin, Global Moderator
Company website: https://db8.nl/en/ - Joomla specialist, Nijmegen, Netherlands
The best website: https://the-best-website.com


Locked

Return to “Joomla! 3.x Coding”