Forums / General Questions / Admin custom cron job

Admin custom cron job

Views: 1,549

Results 1 to 14 of 14
5 Mar 2022, 2:44 AM
#1
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Admin custom cron job

I have followed the directions given in the documentation here but am getting the following error message when executing my custom cron job:

[04-Mar-2022 15:30:00 America/New_York] Request URI: cron, IP address: cron
#1 mysqli_connect() called at [/path to site/includes/classes/db/mysql/query_factory.php:63]
#2 queryFactory->connect() called at [/path to site/renamed_admin/includes/init_includes/init_database.php:18]
#3 require(/path to site/renamed_admin/includes/init_includes/init_database.php) called at [/path to site/renamed_admin/includes/application_bootstrap.php:162]
#4 require_once(/path to site/renamed_admin/includes/application_bootstrap.php) called at [/path to site/renamed_admin/includes/application_top.php:15]
#5 require(/path to site/renamed_admin/includes/application_top.php) called at [/path to site/renamed_admin/my_custom_cron.php:55]
--> PHP Warning: mysqli_connect(): (HY000/2002): No such file or directory in /path to site/includes/classes/db/mysql/query_factory.php on line 63.

I assume it can't find the file where function mysqli_connect is and I'm not sure where it is. A search of my site failed to find it and I don't know where it is. I have uncommented the define for DB_SOCKET but no difference. The cron file is in the admin directory and the cron job is executing but failing at line 55 where the code is require(includes/application_top.php).

What am I doing wrong?

zc1.5.7c, PHP 8.0.8, mysql 5.7.34

5 Mar 2022, 3:11 AM
#2
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

I found the solution to the problem above but have another problem. First the solution. I was running on my development system, MAMP Pro on a Mac. Looking in the admin tools menu under Server/Version Info, I found under the mysqli topic an entry for mysqli.default_socket. It is essentially a path to mysql.sock. Copying that entry and pasting it into the line in the my_custom_cron.php file that defines DB_SOCKET fixed the problem.

// Some servers' PHP configuration doesn't know where to find the mysql socket correctly (evidenced by getting errors about mysqli and mysql.sock, esp when running cron or command-line scripts, such as this one)
// uncomment the following line ONLY if your server's configuration requires it and you don't already have this in your configure.php file
define('DB_SOCKET', '/tmp/mysql.sock');

Now the new problem. The function_exists(my_custom_function) is failing to find the called function. I have no clue why it can't find the function. It's located in admin/includes/functions/extra_functions/my_functions.php. I checked the function name in both places; it's the same. I followed the guidance given in the above-cited documentation and can see no difference between the model code for currency exchange rate and my code. The relevant code follows:

// main execution area
if (function_exists('my_cron_function'))
{
  if (IS_CLI == 'VERBOSE' && $is_browser) echo '<br><pre>' . "\n";
  if (IS_CLI == 'VERBOSE') echo 'Processing... ' . "\n";
  my_cron_function();
  if (IS_CLI == 'VERBOSE') echo 'Done.' . "\n\n";
  exit(0); // returns 0 status code, which means successful
} else {
  echo "Error: Function not found: my_cron_function.\nMake sure you have placed the my_custom_cron.php file in your (renamed) Admin folder.\n\n";
  exit(1);
}

Any help or suggestions would be greatly appreciated.

5 Mar 2022, 12:35 PM
#3
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Admin custom cron job

You could use /admin/currency_cron.php as an example.

5 Mar 2022, 2:02 PM
#4
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

Thanks lat9 but I am using that code as a model. I've uncommented

define('STRICT_ERROR_REPORTING', TRUE); // commented out for normal use
define('DEBUG_AUTOLOAD', TRUE);         // commented out for normal use

with no information being reported. If I call the function when normal admin is running, my function works perfectly. It seems like /includes/functions/extra_functions is not being loaded when run from a cron job.

Really weird.

Dave

5 Mar 2022, 2:09 PM
#5
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,109
Plugin Contributions:
56

Re: Admin custom cron job

Agreed on the weirdness. /admin/auto_loaders/currency_cron.core.php (assuming that you're re-using that loader-prefix) brings in the init_general_functions.php which, in turn, loads any/all .php files present in the admin's /functions/extra_functions sub-directory.

5 Mar 2022, 3:13 PM
#6
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

Thank you for the clue lat9. It would really help if I read and followed ALL directions in the documentation, especially:

Copy the file admin/includes/auto_loaders/currency_cron.core.php to admin/includes/auto_loaders/my-script_cron.core.php Change the inclusion of localization.php to your own functions file.. :censored:

On to the next loading error within my function.

Very embarrassed Dave:oops:

5 Mar 2022, 4:28 PM
#7
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,718
Plugin Contributions:
56

Re: Admin custom cron job

Dave, as you work your way through these issues, perhaps you'd like to add a "troubleshooting" section to our documentation to make it easier for the next person?

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

6 Mar 2022, 6:04 PM
#8
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

BTW, the initial error I encountered, PHP Warning: mysqli_connect(): (HY000/2002): No such file or directory in /path to site/includes/classes/db/mysql/query_factory.php on line 63, went away on my MAMP development system when I added the file I missed in post #6.

swguy, I will take a crack at a troubleshooting section for the documentation.

Dave

9 Mar 2022, 7:41 PM
#9
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

Continuing cron job troubleshooting...are directory and file references in a cron script relative to the script location, or must absolute references be used, or should some other command be used to set the working directory? Assume unix server and php script initiated by cron. I am seeing cases where the script works great called from a browser but fails when executed by a cron job. It appears that the script can not find certain files used by the script. Is this another auto-loading issue?

10 Mar 2022, 12:12 AM
#10
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,967
Plugin Contributions:
8

Re: Admin custom cron job

Dave224:

Continuing cron job troubleshooting...are directory and file references in a cron script relative to the script location, or must absolute references be used, or should some other command be used to set the working directory? Assume unix server and php script initiated by cron. I am seeing cases where the script works great called from a browser but fails when executed by a cron job. It appears that the script can not find certain files used by the script. Is this another auto-loading issue?

dave,
it sounds like all files needs to be explicit stated where they exist in your cron job.

that said, if what you want to do executes fine in a browser, have you thought about using wget? i have some cronjobs set up like:

0 1 * * * wget --no-check-certificate --delete-after 'https://myzencartsite.com/dosomething_cool.php?token=onlyRunifThisTokenisIntheGet'

might that get around some of the problems you are running into?

https://www.gnu.org/software/wget/
https://linuxize.com/post/wget-command-examples/

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

19 May 2022, 7:16 PM
#11
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

Continuing the discussion on my custom admin cron job, I found out debugging a problem that the notifiers in function zen_mail are not triggered when the zen_mail function is called from a cron job. They are triggered during normal store operation. To review, the cron job is on the admin side. It calls a function in admin/functions/extra_functions. That function calls zen_mail which is located in includes/functions/functions_email.php on the store side. A notifier in zen_mail triggers an observer located in includes/classes/observers on the store side.

I have placed trigger_error(' some string') statements in zen_mail before and after a notifier and in the observer associated with the notifier. When the cron job is running, the two statements in zen_mail are executed and logs generated, but the statement inside the observer is not executed. When operating normally all three statements generate logs.

I suspected that something was not being loaded that should be. Thinking it was associated with observers on the store side, I added the following code (in red) to the cron job autoloader file to pick up the observer class notifier from the store side and instantiate it:

<?php
/**
 * @copyright Copyright 2003-2020 Zen Cart Development Team
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: Scott C Wilson 2020 May 14 Modified in v1.5.7 $
 */
if (!defined('USE_PCONNECT')) define('USE_PCONNECT', 'false');
/**
 * autoloader array for admin currency_cron.php
 */
  $autoLoadConfig[0][] = array('autoType'=>'require',
                               'loadFile'=> DIR_FS_CATALOG . DIR_WS_INCLUDES .  'version.php');
  $autoLoadConfig[0][] = array('autoType'=>'class',
                               'loadFile'=>'class.notifier.php');
  $autoLoadConfig[0][] = array('autoType'=>'classInstantiate',
                               'className'=>'notifier',
                               'objectName'=>'zco_notifier');
  $autoLoadConfig[0][] = array('autoType'=>'class',
                                'loadFile'=>DIR_FS_CATALOG . DIR_WS_INCLUDES .  'class.notifier.php');
  $autoLoadConfig[0][] = array('autoType'=>'classInstantiate',
                               'className'=>'notifier',
                               'objectName'=>'zco_notifier');
	$autoLoadConfig[0][] = array('autoType'=>'class',
                               'loadFile'=>'sniffer.php');
  $autoLoadConfig[0][] = array('autoType'=>'class',
                               'loadFile'=>'object_info.php',
                               'classPath'=>DIR_WS_CLASSES);
  $autoLoadConfig[0][] = array('autoType'=>'class',
                               'loadFile'=>'class.phpmailer.php');
  $autoLoadConfig[20][] = array('autoType'=>'init_script',
                                'loadFile'=> 'init_db_config_read.php');
  $autoLoadConfig[30][] = array('autoType'=>'classInstantiate',
                                'className'=>'sniffer',
                                'objectName'=>'sniffer');
  $autoLoadConfig[40][] = array('autoType'=>'init_script',
                                'loadFile'=> 'init_general_funcs.php');
  $autoLoadConfig[60][] = array('autoType'=>'require',
                                'loadFile'=> DIR_FS_CATALOG . DIR_WS_FUNCTIONS . 'sessions.php');
  $autoLoadConfig[70][] = array('autoType'=>'init_script',
                                'loadFile'=> 'init_languages.php');
  $autoLoadConfig[120][] = array('autoType'=>'init_script',
                                 'loadFile'=> 'init_special_funcs.php');
//   $autoLoadConfig[140][] = array('autoType'=>'init_script',
//                                  'loadFile'=> 'init_errors.php');
  $autoLoadConfig[170][] = array('autoType'=>'init_script',
                                 'loadFile'=> 'init_admin_history.php');

  $autoLoadConfig[1][] = array('autoType'=>'class',
                               'loadFile'=>'class.admin.zcObserverLogEventListener.php',
                               'classPath'=>DIR_WS_CLASSES);
  $autoLoadConfig[40][] = array('autoType'=>'classInstantiate',
                               'className'=>'zcObserverLogEventListener',
                               'objectName'=>'zcObserverLogEventListener');

But no change. No error logs other than the zen_mail trigger_error logs. Running the cron job with VERBOSE showed normal expected output and nothing associated with the problem.

Although warned not to do it in the admin cron job model script, I moved the cron job to the store side and set the cron autoloader file identical to the store side includes/auto_loaders/config.core.php. The result was lots of already defined constant error messages, a few undefined array key error messages, but the trigger_error statements were executed in the zen_mail observer when the cron job ran.

So I conclude I'm missing one or more files or not including them correctly in the admin cron job auto_loader file.

Any help would be greatly appreciated.

Dave

19 May 2022, 7:55 PM
#12
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

Well that was stupid. There is only one class.notifier file and it's on the store side. The existing autoloader statement above my addition picks it up correctly. In addition my statement should have been DIR_WS_CLASSES, not DIR_WS_INCLUDES.

So there must be some other file I need. But which?

BTW, zc v1.5.7c, PHP 8.0.2, mysql 8.0, lots of plugins and customization.

20 May 2022, 2:06 PM
#13
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

I strongly suspect that the init script init_observers.php needs to be loaded. It auto-loads and instantiates all /includes/classes/observers/auto.xxxxxxxxx.php classes. But adding the following code to the cron job auto_loaders file in admin/includes/auto_loaders/ directory fails to load the file.

  $autoLoadConfig[175][] = array('autoType'=>'init_script',
                                 'loadFile'=> 'init_observers.php');

The file can't be found. The above code came from includes/auto_loaders/config.core.php.

20 May 2022, 3:36 PM
#14
dave224 avatar

dave224

Zen Follower

Join Date:
Jun 2012
Posts:
481
Plugin Contributions:
0

Re: Admin custom cron job

I think I have it working now. The init script includes/init_includes/init_observers.php and the observer(s) must be copied over to the admin side. Ugh! :(