elements-at / ProcessManager

Manage processes in Pimcore
Other
45 stars 31 forks source link

HTTP 500 error on job execution: "Typed property Configuration::$executorSettings must not be accessed before initialization" #204

Open APochmann opened 7 months ago

APochmann commented 7 months ago

I run into this issue since migration to pimcore 11 (and therefore ProcessManager 5.x). It seems it's an issue that I do not use predefined configuration but start the job directly and fill some data in the callback function. With ProcessManager 4.x I used parameter $configId = null which gave an error in new version so I use empty string now:

        $jobResult = Helper::executeJob(
                '',                               // $configId
                [],                               // $callbackSettings = []
                $this->getPimcoreUser()->getId(), // $userId = 0
                $metadata,                        // $metaData = '[]'
                null,                             // $parentMonitoringItemId = null
                'App\Helper\ProcessHelper::processCallback');

executeJob recognizes there is no Configuration and creates a new one but with this one getExecutorClassObject fails:

    public static function executeJob(string $configId, array $callbackSettings = [], int $userId = 0, mixed $metaData = [], mixed $parentMonitoringItemId = null, ?callable $callback = null)
    {
        try {
            $config = Configuration::getById($configId);

            if(!$config instanceof \Elements\Bundle\ProcessManagerBundle\Model\Configuration) {
                $config = new Configuration();
                $config->setExecutorClass(Executor\PimcoreCommand::class);
            }

            $executor = $config->getExecutorClassObject();

getExecutorClassObject (method of Configuration class) is failing on calling setDataFromResource by passing its own instance created in executeJob:

    public function getExecutorClassObject(): AbstractExecutor
    {
        if (!isset($this->executorClassObject)) {

            $className = $this->getExecutorClass();
            $class = new $className();
            $class->setDataFromResource($this);

setDataFromResource in AbstractExecuter class tries to get Executer settings

    public function setDataFromResource(Configuration $configuration): Configuration
    {
        $settings = $configuration->getExecutorSettings();

getExecutorSettings (method of Configuration class) finally raises the exception as global variable $this->executorSettings is not initialized yet:

    public function getExecutorSettings(): string
    {
        return $this->executorSettings;
    }

I assume, having no predefined configuration is still a valid use case as executeJob handles that by creating a new Configuration instance. It just seems that this new instance is not initialized in a functional way

APochmann commented 7 months ago

Just as a hint as it is for sure not the correct solution: If I enhance the original code

            if(!$config instanceof \Elements\Bundle\ProcessManagerBundle\Model\Configuration) {
                $config = new Configuration();
                $config->setExecutorClass(Executor\PimcoreCommand::class);
            }

in Helper::executeJob like this

            if(!$config instanceof \Elements\Bundle\ProcessManagerBundle\Model\Configuration) {
                $config = new Configuration();
                $config->setExecutorClass(Executor\PimcoreCommand::class);
                $config->setName('');
                $config->setExecutorSettings('{"values":{"group":""}}');
            }

al least my code from previous version is running again

APochmann commented 6 months ago

Hi @ctippler, would the change described in my last comment be an acceptable fix of the issue? If so, I could create a PR. If not, do you have a better proposal to support start of jobs without predefined configurations?