Page 1 of 2

Cron task in extension

Posted: Mon Jul 21, 2014 6:43 pm
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

Re: Cron task in extension

Posted: Mon Jul 21, 2014 7:15 pm
by John P

Re: Cron task in extension

Posted: Tue Jul 22, 2014 7:09 am
by Morabis
I'll give it a try. Thanks John. :)

Re: Cron task in extension

Posted: Tue Jul 22, 2014 9:02 am
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'];
    }
}

Re: Cron task in extension

Posted: Tue Jul 22, 2014 9:33 am
by John P
Did you register your task?

You named your class test instead of elite_apps and your log construct isn't ok

Re: Cron task in extension

Posted: Tue Jul 22, 2014 9:54 am
by Morabis
What do you mean by register? It was added to my services.yml file.

Re: Cron task in extension

Posted: Tue Jul 22, 2014 10:15 am
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;
}


Re: Cron task in extension

Posted: Sat Jul 26, 2014 1:22 pm
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?

Re: Cron task in extension

Posted: Sat Jul 26, 2014 1:44 pm
by Dr.Death
Yes, use the DEV-RC3 Version.

I had the same problems with RC1 / RC2...... viewtopic.php?f=466&t=2252316

Re: Cron task in extension

Posted: Sat Jul 26, 2014 1:56 pm
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?

Re: Cron task in extension

Posted: Sat Jul 26, 2014 2:45 pm
by Dr.Death
I think so.... my cron task are now working again (as in Beta-4 before)

Re: Cron task in extension

Posted: Mon Jul 28, 2014 12:20 pm
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.

Re: Cron task in extension

Posted: Mon Jul 28, 2014 12:32 pm
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

Re: Cron task in extension

Posted: Mon Jul 28, 2014 12:38 pm
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.

Re: Cron task in extension

Posted: Wed Jul 30, 2014 9:24 am
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?