diff --git a/developer_manual/app/index.rst b/developer_manual/app/index.rst index 4469e8789..f7301d3ca 100644 --- a/developer_manual/app/index.rst +++ b/developer_manual/app/index.rst @@ -35,6 +35,7 @@ backgroundjobs settings logging + repair testing publishing code_signing @@ -149,6 +150,12 @@ Log to the :file:`data/nextcloud.log`: * :doc:`logging` +Repair steps +------------ +Repair steps can be used to run code at various stages in app installation, uninstallation, migration ... + +* :doc:`repair` + Testing ------- Write automated tests to ensure stability and ease maintenance: diff --git a/developer_manual/app/repair.rst b/developer_manual/app/repair.rst new file mode 100644 index 000000000..3a71965df --- /dev/null +++ b/developer_manual/app/repair.rst @@ -0,0 +1,111 @@ +============ +Repair Steps +============ + +Repair steps are methods which are executed by Nextcloud on certain +events which directly affect the app. You can use these repair steps to run +code when your app is installed, uninstalled, upgraded etc. It's called repair +steps because they are frequently used to fix things automatically. + +.. note:: Don't use the ``install.php``, ``update.php`` and ``preupdate.php`` files anymore! This method is deprecated and is known to cause issues. + + +Creating a repair step +====================== +A repair step is an implementation of the ``OCP\Migration\IRepairStep`` interface. +By convention these classes are placed in the **lib/Migration** directory. +The following repairstep will log a message when executed. + +.. code-block:: php + + logger = $logger; + } + + /** + * Returns the step's name + */ + public function getName() { + return 'A demonstration repair step!'; + } + + /** + * @param IOutput $output + */ + public function run(IOutput $output) { + $this->logger->warning("Hello world from MyRepairStep!", ["app" => "MyApp"]); + } + } + +Outputting information +~~~~~~~~~~~~~~~~~~~~~ +A repair step can generate information while running, using the +``OCP\Migration\IOutput`` parameter to the ``run`` method. +Using the ``info`` and ``warning`` methods a message can be shown in the console. +In order to show a progressbar, firstly call the ``startProgress`` method. +The maximum number of steps can be adjusted by passing it as argument to the +``startProgress`` method. After every step run the ``advance`` method. Once all steps are finished run the ``finishProgress`` +method. + +The following function will sleep for 10 seconds and show the progress: + +.. code-block:: php + + info("This step will take 10 seconds."); + $output->startProgress(10); + for ($i = 0; $i < 10; $i++) { + sleep(1); + $output->advance(1); + } + $output->finishProgress(); + } + +Register a repair-step +====================== +To register a repair-step in Nextcloud you have to define the repair-setp in the ``info.xml`` +file. The following example registers a repair-step which will be executed after installation +of the app: + +.. code-block:: xml + + + + myapp + My App + A test app + ... + + + OCA\MyApp\Migration\MyRepairStep + + + + + +Repair-step types +================= +The following repair steps are available: + +* ``install`` This repair step will be executed when installing the app. This means it is executed every time the app is enabled (using the Web interface or the CLI). +* ``uninstall`` This repair step will be executed when uninstalling the app, and when disabling the app. +* ``pre-migration`` This repair step will be executed just before the database is migrated during an update of the app. +* ``post-migration`` This repair step will be executed just after the database is migrated during an update of the app. This repair step will also be executed when running the ``occ maintenance:repair`` command +* ``live-migration`` This repair step will be scheduled to be run in the background (e.g. using cron), therefore it is unpredictable when it will run. If the job isn't required right after the update of the app and the job would take a long time this is the best choice.