0

Yesterday i finally got my Typo3 Scheduler working the way i want. Mostly it was the implementation of the CommandController into my extension that was a little bit "problematic".

Now i have another question regarding the Scheduler and the CommandController specifically. I have the following code. It's an Action i have implemented in the controller of a class of my extension:

public function simpleCommand()
{
    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    $apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
    $hosts = $apprep->findAll();

    $objectManager2 = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    $apprep2 = $objectManager2->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
    $services = $apprep2->findAll();

    foreach($hosts as $host)
    {
    $name = $host->getUid();
    $address = $host->getIpv4();
    $file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
    $code_a = 'object Host "';
    $code_b = '" {
    import "generic-host"
    address = "';
    $code_c = '"
    vars.notification["mail"] = {
        groups = [ "icingaadmins" ]
    }
    }';
    $fp = fopen("{$file}", 'wb');
    fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
    fclose($fp);
    mkdir('/etc/icinga2/conf.d/hosts/' . $name);

    foreach($services as $service)
    {
        if($service->getHost() == $name)
        {
            $name = $host->getUid();
            $chkcmd = 'http';
            $file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
            $code_a = 'object Service "';
            $code_b = '" {
            import "generic-service"
            host_name = "';
            $code_c = '"
            check_command = "http"
            }';
            $fp = fopen("{$file}", 'wb');
            fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
            fclose($fp);
        }
    }

    exec('sudo /etc/init.d/icinga2 restart');

    }

}

This is the way i implemented the code in the CommandController, but in a similar way it is also implementd in my Action in the Class Controller. Now, what this function does is simply generating a specific file i need to use in another application. The function gets the repsitory of the class "Host" and then finds all objects of it. Then it just uses the properties of each object to generate the beforementioned files. It does the same with the class "services".

In the frontend through the Action the code works perfectly and generates the files, but in the CommandController, executed automatically through the Scheduler it simply doesn't work.

Is there a missunderstanding on my side? Can't i access each class repository via a command or rather: "Are the repositories only accessable via an Action?"

Or is there another error?

Mondblut
  • 199
  • 1
  • 14

2 Answers2

1

I guess the reason here, is the difference between frontend and backend context.This answer here, from a different context, sums it up very nice and is worth a read

Basically, in the frontend context, you have the typoscript configuration, telling the repository where to store and find records. That is not present in the backend context. That is explained in the answer above with this code

module.tx_yourext.persistence < plugin.tx_yourext.persistence
Soren Malling
  • 522
  • 2
  • 10
  • Sry for the stupid question: But where do i put this line? I'm pretty new to Typo3. Sry. – Mondblut Nov 07 '17 at 10:51
  • If you go and read the answer I linked to, it tells you: `The TypoScript needs to be present in the root page of your website for backend modules/CommandControllers to use them. I suggest you add the stuff to myext/Configuration/TypoScript/setup.txt and add the static template of your ext to the root page.` – Soren Malling Nov 07 '17 at 11:42
  • The interesting thing is: I don't have a setup.txt, but a setup.ts instead. Putting the line in there didn't do anything. – Mondblut Nov 07 '17 at 11:52
  • The location of the file can differ, it can be called ext_typoscript_setup.txt or something else. It's usually included as a static in the backend sys_template – Soren Malling Nov 07 '17 at 12:05
  • Actually there is no txt file at all in my extension folder. Maybe it is the setup.ts? I'm using Typo3 7.6 btw. – Mondblut Nov 07 '17 at 12:13
  • Yes, that will be setup.ts in your case then :-) – Soren Malling Nov 07 '17 at 14:27
0

As it didn't work with the CommandController for my specific case and i had no access to the persistence layer with with it, not even with dependency injection, i decided to not use CommandContoller tasks at all, but rather the normal Scheduler task for it. The biggest problem i encountered was to actually use the findAll() function for the repositories (it worked with findByUid(). This was because the repository expects a storage page by default. As i don’t have a module nor a plugin, my typoscript settings were ignored in that case. So i had to set the repository to disrespect the storage page in my task. Here the code:

<?php

namespace Cjk\Icingaconfgen\Tasks;

class TestTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask
{

public function execute() {
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');

/** @var CustomerRepository $apprep */
$apprep = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\HostRepository::class);
/** @var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep->setDefaultQuerySettings($querySettings);
$hosts = $apprep->findAll();

/** @var CustomerRepository $apprep2 */
$apprep2 = $objectManager->get(\Cjk\Icingaconfgen\Domain\Repository\ServicesRepository::class);
/** @var Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$apprep2->setDefaultQuerySettings($querySettings);
$services = $apprep2->findAll();

foreach($hosts as $host)
        {
        $name = $host->getUid();
        $address = $host->getIpv4();
        $file = '/etc/icinga2/conf.d/hosts/' . $name . '.conf';
        $code_a = 'object Host "';
        $code_b = '" {
        import "generic-host"
        address = "';
        $code_c = '"
        vars.notification["mail"] = {
            groups = [ "icingaadmins" ]
        }
        }';
        $fp = fopen("{$file}", 'wb');
        fwrite($fp, $code_a . $name . $code_b . $address . $code_c);
        fclose($fp);
        mkdir('/etc/icinga2/conf.d/hosts/' . $name);

        foreach($services as $service)
        {
            if($service->getHost() == $name)
            {
                $name = $host->getUid();
                $chkcmd = 'http';
                $file = '/etc/icinga2/conf.d/hosts/'.$name.'/' . $name . '-service.conf';
                $code_a = 'object Service "';
                $code_b = '" {
                import "generic-service"
                host_name = "';
                $code_c = '"
                check_command = "http"
                }';
                $fp = fopen("{$file}", 'wb');
                fwrite($fp, $code_a . $name.'-service'. $code_b . $name . $code_c);
                fclose($fp);
            }
        }

        exec('sudo /etc/init.d/icinga2 restart');

        }

return TRUE;

}

}
Mondblut
  • 199
  • 1
  • 14