1

We have a custom module for which we need to run on a daily basis. I am not sure it is working, as I am unable to store the time correctly.

Does the following code allow me to create a variable, and use it to store the last time cron ran?

function mymodule_cron() {
  if (variable_get('mymodule_last_run', 0) <= strtotime('-1 day')) {
    // Omissis.
    variable_set('mymodule_last_run', time());
  }
}

function mymodule_info($options) {
  $variable['mymodule_last_run'] = array(
    'title' => t('Last Run', array(), $options),
    'type' => 'datetime',
    'default' => t(time(), array(), $options),
  );
}
  • @NoSssweat i have modified the question so that is more clear what i am asking for. I believe Shawn's answer lead me in the correct direction, but i want to verify that when i call hook_info i am using the correct steps. I noticed that the time() was not storing in the variable i am using mymodule_last_run so i want to validate that the hook_info is correct. – sf_admin819 Jul 27 '16 at 17:23
  • The questions seems clearer, now. The problem is not how to run a cron hook every 3 months, but implementing the correct hook required from a third-party module. – kiamlaluno Jul 27 '16 at 18:08
  • To make it clear to future close-voters: This question is not a duplicate because it came out to asking how to use the Variable module for the persistent variables used by a module; the fact the persistent variable is used in hook_cron() is irrelevant. The answer for the other question doesn't explain how a module should use the API exposed from the Variable module. – kiamlaluno Jul 29 '16 at 9:06
1

You are implementing the wrong hook. What you are trying to implement is hook_variable_info(), used from the Variable module. Hook name apart, that hook implementation seems correct.

Instead of using variable_set(), you should use variable_set_value(). The equivalent of variable_get() is variable_get_value().

See the project page for more information about how to use the module API, Variable API for a list of available functions, and variable.api.php for a list of hooks used from the Variable module.

The code you want to use should be similar to the following one.

function mymodule_cron() {
  if (variable_get_value('mymodule_last_run') <= strtotime('-1 day')) {
    // Omissis.
    variable_set_value('mymodule_last_run', REQUEST_TIME);
  }
}

function mymodule_variable_info($options) {
  $variable['mymodule_last_run'] = array(
    'title' => t('Last Run', array(), $options),
    'type' => 'datetime',
    'default' => 0,
  );
}

Notice that variable_get_value() doesn't need the default value, since it gets it from the hook_variable_info() implementation done from your module. If you need to use a different default, you call the function as variable_get_value('mymodule_last_run', array('default' => REQUEST_TIME).

As for your original code, you don't call t() as t(time(), array(), $options) because:

  • The first argument of t() must be a literal string, not the value returned from a function
  • time() returns an integer; even if it would be casted to a string, t() doesn't change an integer in any way, for example returning '1,000' when receiving 1000 as first argument.

If the Variable module is expecting a string (e.g. '19700318T19:00') as default value for a variable of type datetime, you need to use a different function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.