Cron task in extension

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Morabis
Registered User
Posts: 22
Joined: Thu May 23, 2013 11:19 am
Location: Hungary
Name: Peter
Contact:

Cron task in extension

Post by Morabis »

Greetings!

I'm running a board mainly for testing purposes with phpBB 3.1 RC2 and I would like to ask how can I make a cron task to run in an extension? A simple task which would just update a config variable in a given time. Is there any documentation or tutorial for cron tasks?

Regards,

Morabis
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Cron task in extension

Post by John P »

Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
User avatar
Morabis
Registered User
Posts: 22
Joined: Thu May 23, 2013 11:19 am
Location: Hungary
Name: Peter
Contact:

Re: Cron task in extension

Post by Morabis »

I'll give it a try. Thanks John. :)
User avatar
Morabis
Registered User
Posts: 22
Joined: Thu May 23, 2013 11:19 am
Location: Hungary
Name: Peter
Contact:

Re: Cron task in extension

Post by Morabis »

Well it doesn't seem to work. I used your file as reference and the task doesn't running at all. The is_runnable() function returns true and I checked the should_run() function as well which also returns true. My task should run in every 5 minutes maybe that's the problem?

Here is the code:

Code: Select all

<?php

namespace eff\elite_apps\cron\task\core;

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
    exit;
}

class test extends \phpbb\cron\task\base
{
    protected $phpbb_root_path;
    protected $php_ext;
    protected $config;
    protected $log;

    /**
    * Constructor.
    *
    * @param string $phpbb_root_path The root path
    * @param string $php_ext The PHP extension
    * @param phpbb_config $config The config
    * @param phpbb_log $log object
    */
    public function __construct($phpbb_root_path, $php_ext, \phpbb\config\config $config, \phpbb\log\log $log)
    {
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
        $this->config = $config;
        $this->log = $log;

    }

    /**
    * Runs this cron task.
    *
    * @return null
    */
    public function run()
    {
        $this->log->add('admin','2','127.0.0.1','Running cron task',time());
        $this->config->set('elite_apps_last_gc',time());
    }

    /**
    * Returns whether this cron task can run, given current board configuration.
    *
    * @return bool
    */
    public function is_runnable()
    {
        return true;
    }

    /**
    * Returns whether this cron task should run now, because enough time
    * has passed since it was last run.
    *
    * @return bool
    */
    public function should_run()
    {
        //elite_apps_gc value is 300 (5 minutes)
        return $this->config['elite_apps_last_gc'] < time() - $this->config['elite_apps_gc'];
    }
}
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Cron task in extension

Post by John P »

Did you register your task?

You named your class test instead of elite_apps and your log construct isn't ok
Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
User avatar
Morabis
Registered User
Posts: 22
Joined: Thu May 23, 2013 11:19 am
Location: Hungary
Name: Peter
Contact:

Re: Cron task in extension

Post by Morabis »

What do you mean by register? It was added to my services.yml file.
nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Cron task in extension

Post by nicofuma »

It's unrelated, but in 3.1 you should not add this test in the class files:

Code: Select all

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
    exit;
}

Member of phpBB Development-Team
No Support via PM
LavIgor
Registered User
Posts: 545
Joined: Mon Apr 28, 2014 1:47 pm

Re: Cron task in extension

Post by LavIgor »

I'm unable to run cron tasks of extensions on my board.
Current board version RC2.

Standard cron tasks can sometimes be found in the bottom of board pages, but extensions' tasks can be run only manually, when I type 'cron.php' in the address bar.
Setting 'Run periodic tasks from system cron' is turned off as it should be.

Even this example can't be found in the cron image src, when I set is_runnable() and should_run() to true.

Any ideas?
User avatar
Dr.Death
Registered User
Posts: 400
Joined: Fri Apr 25, 2003 10:04 am
Location: Germany
Contact:

Re: Cron task in extension

Post by Dr.Death »

Yes, use the DEV-RC3 Version.

I had the same problems with RC1 / RC2...... viewtopic.php?f=466&t=2252316
Doc.
LavIgor
Registered User
Posts: 545
Joined: Mon Apr 28, 2014 1:47 pm

Re: Cron task in extension

Post by LavIgor »

Dr.Death wrote:Yes, use the DEV-RC3 Version.

I had the same problems with RC1 / RC2...... viewtopic.php?f=466&t=2252316
Thank you. I've already read that topic.
Did the new dev version fix problems with cron that were in RC2?
User avatar
Dr.Death
Registered User
Posts: 400
Joined: Fri Apr 25, 2003 10:04 am
Location: Germany
Contact:

Re: Cron task in extension

Post by Dr.Death »

I think so.... my cron task are now working again (as in Beta-4 before)
Doc.
LavIgor
Registered User
Posts: 545
Joined: Mon Apr 28, 2014 1:47 pm

Re: Cron task in extension

Post by LavIgor »

Dr.Death wrote:I think so.... my cron task are now working again (as in Beta-4 before)
Not working even now.
I've installed the latest dev version of RC3, no extensions' cron tasks can be run automatically.
User avatar
John P
Registered User
Posts: 1237
Joined: Mon Jan 21, 2008 3:55 pm
Location: Netherlands
Name: John
Contact:

Re: Cron task in extension

Post by John P »

Think there is a bug in the cron running from web.
I asked already at area51 http://area51.phpbb.com/phpBB/viewtopic ... 81&t=45997 but no answer yet.

Before the garbage collection there should be a cron_release in cron.php
Image
Webhosting, Custom MODs, Technical management, MOD installation and Webdesign
LavIgor
Registered User
Posts: 545
Joined: Mon Apr 28, 2014 1:47 pm

Re: Cron task in extension

Post by LavIgor »

John P wrote:Think there is a bug in the cron running from web.
I asked already at area51 http://area51.phpbb.com/phpBB/viewtopic ... 81&t=45997 but no answer yet.

Before the garbage collection there should be a cron_release in cron.php
Oh, a new bug.
Thank you.

I am working with phpBB only from web (do not work locally), and cron tasks in extensions still don't work.
User avatar
Morabis
Registered User
Posts: 22
Joined: Thu May 23, 2013 11:19 am
Location: Hungary
Name: Peter
Contact:

Re: Cron task in extension

Post by Morabis »

With rc3-dev I tink my cron task in extension is running but it requires a user to be on forum as cron tasks are managed in page_footer function. Is this intended to work like this? I mean if I would like a task to run every 10 minutes then there should be a user on forum on that given time?
Post Reply

Return to “Extension Writers Discussion”