Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to pass an exit code when stopping the application #203

Open
DanieliMi opened this issue Nov 20, 2020 · 11 comments
Open

Comments

@DanieliMi
Copy link

Currently the exit code is always 1 when calling \TechDivision\Import\ApplicationInterface::stop. It would be nice to have an option to stop the application with exit code 0.

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

would exit(0) work :D ?

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

We could either introduce a new type of exception or replace
https://github.com/techdivision/import-app-simple/blob/master/src/Simple.php#L667

by

return $ase->getCode() ?? 1;

so we can throw such an exception with a custom code

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

Actually it might be better to introduce a new exception type, because in such cases of allowed stops, we do not need to print the full exception.

Our use case is that we have empty imports in some cases - which are okay - and we do not need to generate an artefact. That's why we simply need to stop the process.

@DanieliMi
Copy link
Author

I think it would be nice to be able to pass an exit code to \TechDivision\Import\ApplicationInterface::stop and we could then pass the code to \TechDivision\Import\Exceptions\ApplicationStoppedException which is used as exit code.

@DanieliMi
Copy link
Author

I image it something like this:
https://github.com/techdivision/import-app-simple/blob/master/src/Simple.php#L638-L668

        } catch (ApplicationStoppedException $ase) {
            // rollback the transaction, if single transaction mode has been configured
            if ($this->getConfiguration()->isSingleTransaction()) {
                $this->getImportProcessor()->getConnection()->rollBack();
            }

            // invoke the event that has to be fired after the application rollbacked the
            // transaction (if single transaction mode has been activated)
            $this->getEmitter()->emit(EventNames::APP_PROCESS_TRANSACTION_FAILURE, $this, $ase);

            // finally, if a PID has been set (because CSV files has been found),
            // remove it from the PID file to unlock the importer
            $this->unlock();

            // track the time needed for the import in seconds
            $endTime = microtime(true) - $startTime;

            // If the exit code is above 0 it's an error that we have to handle like it else it is an expected stop and we gracefully end
            if ($ase->getCode() > 0) {
                // log a message that the file import failed
                foreach ($this->systemLoggers as $systemLogger) {
                    $systemLogger->error($ase->__toString());
                }

                // log a message that import has been finished
                $this->getSystemLogger()->warning(sprintf('Can\'t finish import with serial %s in %f s', $this->getSerial(), $endTime));

                // log the exception message as warning
                $this->log($ase->getMessage(), LogLevel::WARNING);

                // return the exit code of the exception
                return $ase->getCode();
            } else {
                // log a info message that import has been finished
                $this->getSystemLogger()->info($ase->getMessage());
                $this->getSystemLogger()->info(sprintf('Execution time for operation with serial %s in %f s', $this->getSerial(), $endTime));

                // invoke the event that has to be fired before the application has the transaction
                // committed successfully (if single transaction mode has been activated)
                $this->getEmitter()->emit(EventNames::APP_PROCESS_TRANSACTION_SUCCESS, $this);
            }
        }

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

Made some pull requests to have something to discuss, but we should also avoid the rollback on finish. Or we find another way to exit the app if nothing is to do, for example with a series of return statements? @DanieliMi

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

I think this is a better approach here - so we do not need a PR to the Pacemaker-Core

We define a new LocalFinishedException()

@DanieliMi

public function process()
{
    try {
        $this->export(time(), 1);
    } catch (LocalFinishedException $e) {
        echo $e->getMessage();
        return;
    }
}


public function getArtefacts() {

   ....

    if (empty($artefacts)) {
        throw new LocalFinishedException('Found no simple products to delete.');
    }
}

@DanieliMi
Copy link
Author

I don't think this will work because after this there will be following operations executed which will result in Exceptions in this case so we have to stop the whole application.

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

true, understood

@amenk
Copy link
Contributor

amenk commented Feb 2, 2021

@wagnert what do you think?

We have process chains which should be stopped if initial jobs do not produce output, but this should have a success exit-code.
Is there a better way to solve that or do you think our approach is generally feasible?

Currently we use vendor/bin/import-simple .... || true in our bash script to avoid stopping the general chain, but this also covers up other problems.

wagnert added a commit that referenced this issue Feb 3, 2021
wagnert added a commit that referenced this issue Feb 3, 2021
…when-stopping-application

Refactoring for issue #203
wagnert added a commit to techdivision/import-cli-simple that referenced this issue Feb 3, 2021
wagnert added a commit that referenced this issue Feb 4, 2021
wagnert added a commit to techdivision/import-app-simple that referenced this issue Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants