0

We have a new requirement where we need to run a job on a daily basis. We have setup cron to run every 1 hour. The goal is to have cron ron every 1 hour, but we want a specific job to run only if 1 day (24hours) has past since last run.

We used to use the Variables table to store the time the job was last run, but we need to change this to use the cache table. Below is the code (mymodule.module) we were reviewing, but it is not creating a cache table and not storing the appropriate date. So we need assistance in figuring out if we are using the correct logic so the job runs once everyday.

Note: $cache->data below is actually the last time the job ran.

mymodule_cron() {
     $data = '';
     if($cache = cache_get('last-run')) {
         $data = $cache->data;
         if($data <= strtotime('-1 day')) {
               //call helper function to perform some logic
               _some_func();
               $data = time();

               //set update date in cache
               cache_set('last-run', $data, 'cache'));
         }
    }
 }
1

I don't think there is something wrong on cache_get() and cache_set(). It seems a logic problem. In your code the cache is never set if a cache is not found.

/**
 * Implements hook_cron().
 */
function mymodule_cron() {
  $data = '';

  if ($cache = cache_get('last-run')) {
    $data = $cache->data;
    if($data <= strtotime('-1 day')) {
      //call helper function to perform some logic
      _some_func();
      //set update date in cache
      cache_set('last-run', time(), 'cache'));
    }
  }
  else {
    // set cache if there is no cache.
    cache_set('last-run', time(), 'cache'));
  }
}
|improve this answer|||||
0

I would suggest storing this sensitive data to Drupal variables instead of cache because cache can be wiped by arbitrary code (which probably happens in your case).

Instead of cache_get and cache_set use variable_get and variable_set accordingly.

|improve this answer|||||
  • Yes we have the logic for the variable, but we found that the variable table has gotten very big so we thought the cache route would be good. The way the helper function runs is that it will update data only when there is a certain criteria met. It will not hurt us if the data is wiped so we still need assistance in figuring out how to get it store correctly in cache. – sf_admin819 Aug 8 '16 at 23:07
  • @sf_admin819 instead of variables get/set you. Many contrib modules are using a separate table to store their settings. But if you only have only less then 5 values, please don't feel hesitate to use variable table. – Jimmy Ko Aug 9 '16 at 8:31
  • 1
    @JimmyKo so i just need to store the time the job last ran and would prefer not to create a variable in the variable table for this. We are okay with the data being cleaned after cache clear. Considering that i still need some help with the code provided above to make sure it is the correct code. – sf_admin819 Aug 10 '16 at 17:35
  • @sf_admin819 I posted in answer. Please check. – Jimmy Ko Aug 11 '16 at 10:13
  • @JimmyKo yes that seemed to work! i was missing that portion to create it if it wasn't there. – sf_admin819 Aug 11 '16 at 23:38

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.