Introduction

Faveo Application has various jobs which needs to be scheduled and run using cron or task schedulers. All these jobs can be processed using individual Artisan command as well as single Artisan command for running scheduled jobs.

Admin can select frequency and time interval for running all these jobs from cron settings. There admins can see all the available cron jobs which needs to be set on the server and choose how frequently they want to run these jobs for their system. For example Faveo needs to fetch mails from mail server to generate tickets which are coming to users support email address. Server Admin can do two things

  1. Set multiple crons for individual artisan commands in crontab which fetches mail and specify frequency of cron job.
  2. Set single cron command “schedule:run” to run all jobs using single cron and specify frequency to run single cron on the server. And select frequency to run jobs from Application itself.

Please note in 2nd case execution of individual commands may not take place as the execution of main command is set on the server and might conflict in the execution settings set in the application by admin.

Eg1. If cron is running every 5 minutes and admin has set up email fetching job to run every 1 minute from cron settings. Email fetching will only happen in 5 minutes instead of every minute as main cron will only execute in the interval of 5 minutes.

Eg2. If cron is running daily at 10:00AM on the server and admin has set up some jobs to run daily at 5:00PM those jobs will never run as main cron execution time and execution time set by admin for these jobs are different. (Need to discuss how to handle this case and show user logs like WHMCS cron shown by Bhanu the other day).

Development of scheduled jobs for custom plugins

If we are to implement some module or feature using custom plugin or package; which requires to run some scheduled command then we should not do any change in Core Faveo code. Rather we should create our custom console/Kernel.php class and register all custom commands in our plugin directory itself. An example of which has been implemented in LDAP plugin. Console/Kernel class will only execute system’s active jobs excluding plugin’s jobs. It will ensure the jobs for Plugins will only run when plugins are active.

Developers do not have to worry about how to add settings for their plugin jobs in Faveo cron settings. It will be visible in cron settings when the plugin is active. All they have to do is create an entry for all their custom jobs in `conditions` table specifying following attributes.

  1. Job : ldap (single word name of you job)
  2. Value: everyMinute (default value for execution time for their command)
  3. Icon: fa fa-clock (Font awesome icon class to show in cron settings page for their job)
  4. Command: ldap:sync (artisan command for their job)
  5. Job_info: ldap-sync-info (Single-word for job description which will be fetched from language to show as a tooltip in cron settings page)
  6. Active: 1 (active/inactive status of the job )
  7. Plugin_job: 1 (Must be set as 1 to tell system the job is custom plugin’s job otherwise)
  8. Plugin_name: Ldap (Must contain your plugin Directory name as it will be used to fetch language from your plugins lang directory, null otherwise)

Job and job_info word must be included in the lang directory of your plugin and plugin_name must be given as your plugin directory name as it will be used to render translation for Job and job_info in cron settings page.

Things developer needs to complete

  • Create an entry for their jobs only when plugin is activated first time.
  • Create command in your plugin directory and register it in plugin’s ServiceProvider
  • Write Kernel.php in your plugin and extend it from Console/Kernel to use its properties and methods
  • Use checkActiveJobsOfPlugin() passing their plugin name as parameter to get all Active jobs of their plugin.
  • Use getTaskCommand() to get execution preference of their plugin jobs
  • Use getCondition() to schedule jobs

Please check App\Model\MailJob\Condition and App\Console\Kernel classes to check available useful methods.

Leave a Reply

Your email address will not be published. Required fields are marked *