Exercise 2. Task Scheduling and Dashboard Integration

In this exercise, you will create a simple RSS reader which displays the last feed item from the Plesk feed on the administrator's dashboard. While writing the extension, you will learn how to do the following:

  • Write installation and removal scripts.
  • Use the key-value storage.
  • Add scheduled tasks.
  • Create promotional blocks on the administrator's dashboard.

We assume that the extension ID is plesk-news, and that you start with the code inside the ex2 directory. This code is partly written by us to help you focus only on certain areas. Additionally, we have populated the directory with media content necessary for the exercise. To see the completed exercise, examine the ex2complete directory.

Step 1. Implement a helper to download the RSS feed

First of all, let us implement a class to download the RSS feed and store the most recent item in the local storage. For this purpose, open /plib/library/News.php. Pay attention to the class name: it follows the class naming convention, and will thus be loaded automatically. For auto-loading we rely on the Zend approach. Read more about it here.

Now, let us implement the load method:

public static function load()
{
	$url = 'http://kb.plesk.com/rss/gen.xml?p=PLESK';
	$content = @file_get_contents($url);
	if (false === $content) {
	return;
	}
	$xml = simplexml_load_string($content);
	if (false === $xml || !$xml->channel) {
	return;
	}
	$lastItem = $xml->channel->item;
	pm_Settings::set('news_text', $lastItem ? (string) $lastItem->title : '');
	pm_Settings::set('news_link', $lastItem ? (string) $lastItem->link : '');
}

The code demonstrates how the feed item is saved to the key-storage using pm_Settings::set by assigning the news_text and news_link variables. Now, we can get the variable values at any time with pm_Settings::get if the extension's context is initialized.

Step 2. Populate the storage with the last feed item.

The extension we are going to write will not have its own GUI. Instead, all its functions will be available from the promo block on the administrator's Home page. That is why we do not need to get the control flow as we did in Exercise 1, step 1. However, we need to populate the extension's storage with the last feed item. For this, we should write a script that fetches the item and saves it directly after the installation of our extension.

The extensions SDK provides the ability to run PHP scripts in this and some other situations. The following table summarizes which extension files are called in particular conditions.

File Condition

/plib/scripts/pre-install.php

Runs before extension files are copied. This script makes sure that the system meets the extension’s requirements, and should not do any modifications on the server. Extension files are not available at this stage.

/plib/scripts/post-install.php

Runs after extension files are copied. This script makes all changes required for the extension to work. For example, using this script, you can create databases or prepare the necessary directory structure.

/plib/scripts/pre-uninstall.php

Runs before the removal of an extension. This script removes files used by the extension, such as databases, templates, and user content.

So the system scans each extension for these scripts and runs the scripts as needed.

In our exercise, we are interested in fetching the feed item after the installation, so you should change /plib/scripts/post-install.php by adding the following line to the file:

Modules_PanelNews_News::load();

It will initialize the values in key-value storage.

Step 3. Display the feed item in the promo block.

To create a promotional block in Plesk, add the following lines to /plib/library/Promo/Home.php:

class Modules_PanelNews_Promo_Home extends pm_Promo_Home
{

    public function getTitle()
    {
        return 'Plesk News';
    }

    public function getText()
    {
        pm_Context::init($this->_moduleId);
        return pm_Settings::get('news_text');
    }

    public function getButtonText()
    {
        return 'View Article';
    }

    public function getButtonUrl()
    {
        return pm_Settings::get('news_link');
    }

    public function getIconUrl()
    {
        pm_Context::init('plesk-news');
        return pm_Context::getBaseUrl() . '/images/feed.png';
    }

}

As you can see, this class describes a promo block. It is not called from anywhere inside the extension itself. In fact, the platform loads all classes which extend pm_Promo_Home automatically when the Plesk administrator loads the Home page. To be loaded, the class name should adhere to the naming convention we discussed at step 1. If you package the extension and add it to Plesk, you will see a news block on the dashboard. When the administrator clicks the link, they will be forwarded to the news_link variable from the key-value storage.

Step 4. Schedule the periodic update of the last feed item

The variable news_link is assigned only once, specifically, when the extension is installed. We need to update its value periodically to follow the Plesk feed. For this, we will run a scheduled script, but first let us create the script itself.

Note: You must place all scripts in the /plib/scripts directory.

Assume our script name periodic-task.php, so its path within the extension will be /plib/scripts/periodic-task.php. Open the file and add the following code to it:

Modules_PanelNews_News::load();

Looks familiar? Yes, we used the same code in post-install.php. Now it's time to schedule the script run. The best place for this is the post-installation script to ensure that the task is safely scheduled just right after the installation of our extension. Open /plib/scripts/post-install.php and edit it to look as follows:

<?php
$task = new pm_Scheduler_Task();
$task->setSchedule(pm_Scheduler::$EVERY_DAY);
$task->setCmd('periodic-task.php');
pm_Scheduler::getInstance()->putTask($task);
Modules_PanelNews_News::load();

The code demonstrates that to schedule a script run (so-called task), we need to create an instance of pm_Scheduler_Task, set its period using setSchedule, and specify the target script using setCmd. Finally, add the task to the schedule interface using putTask.

When removing the extension, the scheduled task must be removed as well. For this purpose you need to add the following line to /plib/scripts/postpre-uninstall.php:

pm_Scheduler::getInstance()->removeAllTasks;

This code will remove the task from the schedule interface using removeAllTasks.

Step 5. Install and test the extension.

Congratulations! You have completed the exercise. To install your extension to Plesk:

  1. Add the contents of the ex2 directory (not the directory itself) to a ZIP archive using your favorite software.

    Important: Ensure that meta.xml resides in the root of the unpacked archive. This is a very important requirement, otherwise Plesk will return an error on adding your extension to Plesk.

  2. Log in to Plesk as the administrator and add the extension on the Server Management > Extensions page.

You should see the extension in the list. It is dimmed because it does not have its own GUI, but you can see the promo block on the Home page and can verify that the task was scheduled in Tools&Settings > Scheduled Tasks.

Re-check the implemented functionality to make sure everything is available in the extension archive. You can use the completed extension available here for reference.

 

Leave your feedback on this topic here

If you have questions or need support, please visit the Plesk forum or contact your hosting provider.
The comments below are for feedback on the documentation only. No timely answers or help will be provided.