Page 1 of 1

cron doesn't run

Posted: Thu May 23, 2019 9:41 pm
by MarkDHamill
Here's my services.yml file:

Code: Select all

imports:
    - { resource: parameters.yml }
services:
    phpbbservices.filterbycountry.listener:
        class: phpbbservices\filterbycountry\event\main_listener
        arguments:
            - '@language'
            - '@request'
            - '%core.root_path%'
            - '%core.php_ext%'
            - '@config'
            - '@log'
            - '@user'
        tags:
            - { name: event.listener }
    phpbbservices.filterbycountry.common:
        class: phpbbservices\filterbycountry\core\common
        arguments:
            - '@language'
            - '%core.root_path%'
            - '@config'
            - '@log'
    phpbbservices.filterbycountry.controller.acp:
        class: phpbbservices\filterbycountry\controller\acp_controller
        arguments:
            - '@config'
            - '@language'
            - '@log'
            - '@request'
            - '@template'
            - '@user'
    phpbbservices.filterbycountry.service:
        class: phpbbservices\filterbycountry\service
        arguments:
            - '@user'
            - '%phpbbservices.filterbycountry.tables.filterbycountry_table%'
    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [cron.task.cron_task]]
        tags:
            - { name: cron.task }
and my cron program in /cron/task/update_country_database.php:

Code: Select all

<?php
/**
 *
 * Filter by country. An extension for the phpBB Forum Software package.
 *
 * @copyright (c) 2019, Mark D. Hamill, https://www.phpbbservices.com
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 */
namespace phpbbservices\filterbycountry\cron\task;

class update_country_database extends \phpbb\cron\task\base
{

	protected $config;
	protected $phpbb_root_path;
	protected $helper;
	protected $phpbb_log;

	/**
	 * Constructor.
	 *
	 * @param \phpbb\config\config 							$config 	The config
	 * @param $phpbb_root_path								$string		Relative path to phpBB root
	 * @param \phpbbservices\filterbycountry\core\common 	$helper		Extension's helper object
	 * @param \phpbb\log\log 								$phpbb_log 	phpBB log object
	 */

	public function __construct(\phpbb\config\config $config, $phpbb_root_path, \phpbbservices\filterbycountry\core\common $helper, \phpbb\log\log $phpbb_log)
	{

		$this->config = $config;
		$this->phpbb_root_path = $phpbb_root_path;
		$this->helper = $helper;
		$this->phpbb_log = $phpbb_log;

	}

	/**
	 * Indicates to phpBB's cron utility if this task should be run.
	 *
	 * @return true if it should be run, false if it should not be run.
	 */

	public function should_run()
	{
		// The Maxmind country database is updated weekly on Tuesdays. For our purposes, we'll assume a fresh database exists on Wednesdays.
		// So to update the database, it must be on or after Wednesday and at least 7 days must have elapsed since the database was last updated.

		$todays_dow = date('w');	// 0 = Sunday, 6 = Saturday
		$days_since_last_wednesday = (int) (4 + $todays_dow);

		$debug_string = 'Todays DOW = ' . $todays_dow . ' Days since last Wednesday = ' . $days_since_last_wednesday;

		// Debugging
		$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ACP_FBC_DEBUG', false, array('Debugging: ' . $debug_string . ' - ' . (bool) ($days_since_last_wednesday >= 7)));

		return (bool) ($days_since_last_wednesday >= 7);
	}

	public function run()
	{

		// Updates the MaxMind country database via a cron

		// Destroy current database, then download, ungzip, untar and stage an updated database.
		// Debugging
		$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ACP_FBC_DEBUG', false, array('run() function attempted.'));
		$this->helper->download_maxmind(true);
		return true;

	}

}
To figure this out as you can see I write to the log. The should_run() function writes well enough to the log. I get log entries like this:
Debugging: Todays DOW = 4 Days since last Wednesday = 8 - 1
with the - 1 at the end telling me that the function is returning true.

But clearly run() is not getting executed because it's not getting trapped in the log.

I've installed the Check Cron Status extension and it doesn't show up in the list of crons.
Cron Status.png
Any ideas?

Note: system cron is disabled. The way I've tested this in the past is to go to the index. Sometimes I have to log out and in to get the cron going.

Re: cron doesn't run

Posted: Fri May 24, 2019 2:54 pm
by mrgoldy
You have the following:

Code: Select all

calls:
    - [set_name, [cron.task.cron_task]]
Which I believe has to be:

Code: Select all

calls:
    - [set_name, ['phpbbservices.filterbycountry.cron.task.cron_task']]

Re: cron doesn't run

Posted: Fri May 24, 2019 3:38 pm
by MarkDHamill
Thanks for the suggestion. Unfortunately, it didn't work. Not sure what set_name has to do with this and how it is used.

Re: cron doesn't run

Posted: Fri May 24, 2019 5:14 pm
by 3Di
Will be hard it can be executed if it has no reference to the two configurations needed.

Re: cron doesn't run

Posted: Fri May 24, 2019 5:49 pm
by MarkDHamill
3Di, is there something I am missing?

Re: cron doesn't run

Posted: Fri May 24, 2019 6:36 pm
by AbaddonOrmuz
I'm pretty sure it needs to set the right cron task name for it to work. At least I have something like this in the cron task from my Auto-lock topics extension.

Code: Select all

    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [cron.task.update_country_database]]
        tags:
            - { name: cron.task }
You could debug if it's actually working by commenting the following line in cron.php

Code: Select all

output_image();
And visiting the URL:

Code: Select all

http://example.com/cron.php?cron_type=cron.task.update_country_database

Re: cron doesn't run

Posted: Fri May 24, 2019 7:57 pm
by MarkDHamill
Thanks for the suggestions. I changed services.yml as follows:

Code: Select all

    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [phpbbservices.filterbycountry.update_country_database]]
        tags:
            - { name: cron.task }
Commenting out the image in cron.php and using the suggested URL:

http://127.0.0.1/phpbb/cron_type=cron.t ... y_database

does render:
The requested page could not be found.

Re: cron doesn't run

Posted: Fri May 24, 2019 8:01 pm
by AbaddonOrmuz
The URL is wrong, it should be:

Code: Select all

http://127.0.0.1/phpbb/cron.php?cron_type=cron.task.update_country_database

Re: cron doesn't run

Posted: Fri May 24, 2019 8:04 pm
by 3Di
set_name is wrong, see the above suggestion..
prepend cron.task and remove vendor.ext

Re: cron doesn't run

Posted: Fri May 24, 2019 8:12 pm
by MarkDHamill
Thanks for the suggestions. Relevant part of services.yml is now:

Code: Select all

    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [update_country_database]]
        tags:
            - { name: cron.task }
Commenting out output_image(); in cron.php and using http://127.0.0.1/phpbb/cron.php?cron_ty ... y_database renders a blank page.

Re: cron doesn't run

Posted: Fri May 24, 2019 8:21 pm
by 3Di
3Di wrote:
Fri May 24, 2019 8:04 pm
set_name is wrong, see the above suggestion..
prepend cron.task and remove vendor.ext
- [set_name, [cron.task.update_country_database]]

Re: cron doesn't run

Posted: Fri May 24, 2019 8:40 pm
by MarkDHamill
Thank you, 3Di. This time I got the proper debug message in my error log:
run() function attempted.
Originally, services.yml was:

Code: Select all

    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [cron.task.cron_task]]
        tags:
            - { name: cron.task }
and now is:

Code: Select all

    phpbbservices.filterbycountry.cron.task.cron_task:
        class: phpbbservices\filterbycountry\cron\task\update_country_database
        arguments:
            - '@config'
            - '%core.root_path%'
            - '@phpbbservices.filterbycountry.common'
            - '@log'
        calls:
            - [set_name, [cron.task.update_country_database]]
        tags:
            - { name: cron.task }
So it looks like changing to update_country_database is what I needed to do. It is kind of strange though because for my digests extension I use:

Code: Select all

        calls:
- [set_name, [cron.task.cron_task]]
which calls /cron/task/digests.php and it worked. I'm guessing it has something to do with the cron program name being different than the extension name.

Re: cron doesn't run

Posted: Fri May 24, 2019 8:52 pm
by 3Di
Seems like the digests' one is wrong IMO.

Still, you are using the config service - '@config but's unused.
You should code a migration with 2 configurations and use them with should_run() and run() instead of a custom timelapse.

Re: cron doesn't run

Posted: Fri May 24, 2019 8:57 pm
by MarkDHamill
Right you are. They were used at one point but the code was since simplified. I would have removed this and '%core.root_path%' at some point during clean up.