Webasyst supports creation of special scripts that can be executed by a job scheduler such as cron. This opportunity is available for apps and plugins.
In the scripts executed by a job scheduler, all the app or plugin classes can be used just like in an ordinary controller.
Creation of a script
For each action which must be executed by a scheduler create a separate file:
- For apps
wa-apps/[app_id]/lib/cli/[app_id][Action_name].cli.php
Example:
wa-apps/myapp/lib/cli/myappDo.cli.php
- For plugins
wa-apps/[app_id]/plugins/[plugin_id]/lib/cli/[app_id][Plugin_id]Plugin[Action_name].cli.php
Example:
wa-apps/someapp/plugins/my/lib/cli/someappMyPluginDo.cli.php
Here action_name is an arbitrary script identifier, which should possibly tell a user the purpose of the script.
In that file, you have to declare a class extending waCliController
. The main logic to be executed by a job scheduler must be written within the execute()
method.
The class name must be built by the following rule:
- For apps
[app_id][Action_name]Cli
- For plugins
[app_id][Plugin_id]Plugin[Action_name]Cli
Example
<?php // for an app class myappDoCli extends waCliController { public function execute() { // here goes the code that will be executed // you can use models and other classes of an app waLog::log("Hello world!"); } } // for a plugin class someappMyPluginDoCli extends waCliController { public function execute() { // here goes the code that will be executed // you can use models and other classes of an app or a plugin waLog::log("Hello world!"); } }
Script execution command
In the cron scheduler configuration, a user needs to add a command to ensure regular script execution:
- For apps
php [path to Webasyst directory]/cli.php [app_id] [action_name]
Example:php /var/www/wa/cli.php myapp do
- For plugins
php [path to Webasyst directory]/cli.php [app_id] [plugin_id]Plugin[Action_name]
Example:php /var/www/wa/cli.php someapp myPluginDo
During the development, you can verify the functionality of your script by running the above command in the server console.
Adding a script command to job scheduler setup
Example of a job scheduler command in the cron configuration:
*/10 * * * * /usr/bin/php /var/www/webasyst/cli.php myapp do
Read more about configuring cron jobs in the Cron section.