2K Views

How to Refresh Mailbox Using Cron Job in UVdesk OpenSource

In this blog we will discuss how you can automate the periodic execution of UVDesk Mailbox refresh commands. To implement this, we will utilise cron job scheduling.

To start off, go inside the the mailbox-component bundle of your Open-Source UVDesk project, and open the Console sub-directory, in which you will find the file; RefreshMailboxCommand.php (located at project_root/vendor/uvdesk/mailbox-component/Console/RefreshMailboxCommand.php).

class RefreshMailboxCommand extends Command
{
    private $container;
    private $entityManager;

    public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager)
    {
        $this->container = $container;
        $this->entityManager = $entityManager;

        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('uvdesk:refresh-mailbox');
        $this->setDescription('Check if any new emails have been received and process them into tickets');

        $this->addArgument('emails', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, "Email address of the mailboxes you wish to update");
        $this->addOption('timestamp', 't', InputOption::VALUE_REQUIRED, "Fetch messages no older than the given timestamp");
    }

Now, override the parent class function execute, this is where all the logic will be written and executed.

protected function execute(InputInterface $input, OutputInterface $output)
{
//Your logic inside here...
}

Now, to schedule commands to be executed periodically, run the following command:

crontab -e

Add the following cron expression to the crontab for automatic Mailbox Refreshing, (in the command below, the command will execute every 30 minute):

30 * * * * php /project_root_forlder_path uvdesk:refresh-mailbox && php  
bin/console uvdesk:refresh-mailbox your_mailbox_id
Now, if you are new to cron scheduling, below is how you can set the periodic execution timing to your exact requirement://The format is as follows:

+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed

I have explored this while contributing to the open-source Symfony based project UVDesk, there are a lot more things to learn and develop, feel free to contribute to an enterprise-level open-source helpdesk.

Category(s) Symfony UVdesk
. . .

Comment

Add Your Comment

Be the first to comment.

css.php