Jump to content

how create cron job from custom module ? all by code


rafapas22

Recommended Posts

Hello.

 

I am creating a custom module for import products and if execute from BO all perfect, but i like that import will be automatically, i know that for this the module must create an entry in cron of system, but, how i do this in Prestashop??

 

This module is for a lot of users, so that the create cron must be a method of source code, that is to be created by code when install the module in prestashop, someone can help me?

 

Thanks

Link to comment
Share on other sites


There are examples in native PrestaShop modules.  Currencies  for example, but you  should find suitable example in native ps codebase.

 

$this->cron_url = _PS_BASE_URL_._MODULE_DIR_.'youmodulename/youmodulename-cron.php?token='.substr(Tools::encrypt('yourmodulename/cron'), 0, 10);

 

 

youmodulename/youmodulename-cron.php

 

/*

* This file called using a cron to do 'something'

*/

include(dirname(__FILE__).'/../../config/config.inc.php');

include(dirname(__FILE__).'/../../init.php');

/* Check security token */

if (substr(Tools::encrypt('yourmodulename/cron'), 0, 10) != Tools::getValue('token') || !Module::isInstalled('youmodulename'))

die('Bad token');

//v1.8 START

if (!defined('_PS_MODE_DEMO_'))

define('_PS_MODE_DEMO_', false);

//END v1.8

 

include(dirname(__FILE__).'/yourmodulename.php');

$some_name = new yourmodulename();

 

 

Edited by El Patron (see edit history)
  • Like 1
Link to comment
Share on other sites

Good morning.

 

I understand what you mean, but how and where to put the first line? in which file?

reviewing the native modules prestashop all I've seen is using the module "cronjobs", I should use that module? perhaps, it is made for this?

 

I have more questions than before .. :)

Link to comment
Share on other sites

 

Good morning.
 
I understand what you mean, but how and where to put the first line? in which file?
reviewing the native modules prestashop all I've seen is using the module "cronjobs", I should use that module? perhaps, it is made for this?
 
I have more questions than before .. :)

 

 

the first line is displayed in module configuration,  this is copied by shop manager to add to cron.

 

you cannot  dictate what cron shop manager will use, module cron is not best choice, they may use hosting cron, you job is to create link they can copy.

Link to comment
Share on other sites

but then I can not create a cron job directly? all I can do is put a php link that the client should put in his cron?

For me is more easy, but for the customer of module is more difficult...

 

how would you  communicate this to cron?  Which cron?  Please forgive if I do not understand directly who you are targeting for module...if for one shop you can code for one shop, if you code for 'unkown' shops, then one cannot make assumption they use cron module and not hosting cron.

 

it would not be possible for generic module to add hosting cron, I suppose for module cron one could attempt a  solution. :)

Link to comment
Share on other sites

Thanks for your response.

 

but there must be some method to do what I want, in magento exists! , Magento for example, has a cron.php file in your root and through that file can periodically run third-party scripts, because the client only has been responsible for put in hosting the cron.php the core and not all tasks of each module of other companies.

 
I explain?
Link to comment
Share on other sites

I think you can create a function inside your module like this:
public function hookActionCrobJob($params)
{

}

When your module is installed into that hook, it will be registered in the Cron Jobs module.

 

I've never actually tried it though and none of the core modules use it, so you'll have to investigate how it works. Maybe there's a reference to it in the PrestaShop documentation somewhere?

  • Like 3
Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...