Jump to content
Biorn

[Q]can lazycron og cron

Recommended Posts

Hi guys..

Long time no see... :)

Well, Im back to some web programming again, I have been on tour with a Odense symphony orchestra..

Ok, I havent played with cron or lazycron before, so I was wondering, can it be used to auto refresh a website?

I startet a digital signage project, where Im planning to use processwire as the core, and then I need it to refresh or update the player browser every day at midnight, and maybe doing the day, when some of the items are done..

Hope you can give me an answer...

  // Biorn

Share this post


Link to post
Share on other sites

Not an expert on this, but if what you want is to refresh the page in the browser (is it?) the indicated would be javascript.

Maybe something like this would work (very rough and written in the browser... just to illustrate a point)

// checks every hour if the hour of the day is 0, and if so, reloads the page
 
setTimeout(function(){
    var d = new Date();
    if( d.getHours() == 0 ){
        window.location.reload();
    };
}, 3600000);

Share this post


Link to post
Share on other sites

Hi Bjorn,

not exactly sure if get what you want to do :)

From the docs:

It is called 'lazy' because it's triggered by a pageview, so the interval is guaranteed to be at least the time requested (and maybe more) rather than exactly the time requested. The more pageviews your site gets, the closer it is. This is fine for most cases, but if you need it to be fully accurate I'll describe how you can make it not-lazy a little further down.

http://modules.processwire.com/modules/lazy-cron/

So if you need your refresh exactly at midnight, a Cronjob would be the better solution. See the last paragraph, "How to make it not lazy" in the link above.

Cheers

Share this post


Link to post
Share on other sites

HI guys..

I want to update the "player" every 5 mins for what event that are finish, and at midtnight for the next days events.

and maybe a "focus" thing that can be shown every XX mins..

I have looked a bit into lazy-cron, but Im not getting it.. :( maybe im just blinded by myself??

Share this post


Link to post
Share on other sites

Biorn, here's some more info about LazyCron:

http://processwire.com/api/modules/lazy-cron/

Ultimately, it's lazy, which you will notice if you don't have consistent traffic to your site. So if you need something to run every 5 mins consistently, then you should either use regular cron (from your server) or use regular cron to ping a non-cached page on your website (thus triggering Lazycron). 

Share this post


Link to post
Share on other sites

I have looked into it, and I cant get it to work.. Maybe Im going blind, og just loosing my mind and brains.. :P

In this case there is not a lot of trafic, Its a info screen with, times, locations and classes. But its the proof-of-concept, that a digital signage system can be made, and run from processwire..

And in the future, the "player" client must be known by the master server..

have a peek: http://sign.chrisb.dk/

this is powered by: google calender loader, Color picker, Repeater, LazyCron (in the future) and Page Render IP restriction.

And the player is a Raspberry Pi, running Raspbian and booting Chromium into kios mode, and it runs very smooth.. Except the Cron stuff.. :)

But, maybe I chould have a look into the regular cron ping solution.. Hmm..

Ill be back...

PS..

Im allways getting a smile on my face, and a lot happyer, when I see your big smile in your profile pic Ryan.. And thanks for that..

// Cheers...

  • Like 1

Share this post


Link to post
Share on other sites
Ok, I havent played with cron or lazycron before, so I was wondering, can it be used to auto refresh a website?

I startet a digital signage project, where Im planning to use processwire as the core, and then I need it to refresh or update the player browser every day at midnight, and maybe doing the day, when some of the items are done..

Just to make sure: are you actually trying to run code under the hood or perhaps just refresh the browser window at specified interval?

If your intention is just to reload / refresh the browser window, you'd probably want to use JavaScript or meta refresh tag for that. Cron (whether lazy or regular) is intended to run code under the hood at regular interval, but it won't directly affect browsers / clients viewing the content. If your application needs to fetch content from somewhere (such as Google Calendar), you could use a combination of lazy cron and JavaScript to achieve this, though lazy cron part would be somewhat irrelevant as long as this is just a proof of concept. For the time being you could also have your page check for new content on each page load.

If you're interested in taking this concept a step further, you could also take a look at something like https://github.com/viljamis/Remote-Preview/ as a reference project. It has certain similar features to what you're building here (unless I'm completely mistaken) - primarily that it updates client machines browser window on an interval.

The thing that makes Remote Preview feel more sophisticated is that refresh is only toggled if an AJAX query to a text file containing an URL confirms that it has changed since previous reload. Difference between that approach and a plain meta tag / JavaScript refresh isn't huge, but it definitely makes it seem more elegant  :)

  • Like 1

Share this post


Link to post
Share on other sites

You might find the attached LazyCronTest.module helpful. This basically just demonstrates LazyCron in action. Install this module after LazyCron is installed, and it'll record a log entry every 5 minutes (or so) to /site/assets/logs/lazytest.txt. That's assuming the site is getting non-cached pageviews so that LazyCron gets a chance to run. 

LazyCronTest.module

<?php

class LazyCronTest extends WireData implements Module {
  public static function getModuleInfo() {
    return array(
      'title' => 'Lazy Cron Test', 
      'version' => 100, 
      'summary' => 'Tests lazy cron by writing to a log file in /site/assets/logs/lazytest.txt',
      'singular' => true, 
      'autoload' => true, 
      );
  }
  public function init() {
    $this->addHookAfter('LazyCron::every5Minutes', $this, 'lazyTest'); 
  }
  public function lazyTest($event) {
    $seconds = $event->arguments[0]; 
    $log = new FileLog($this->config->paths->logs . 'lazytest.txt'); 
    $log->save('LazyCron 5 minute test - ' . date('Y-m-d H:i:s') . " - $seconds seconds"); 
  }
}

Here's an example of the log file it generates:

2013-02-02 11:19:02:LazyCron 5 minute test - 2013-02-02 11:19:02 - 356 seconds
2013-02-02 11:24:04:LazyCron 5 minute test - 2013-02-02 11:24:04 - 302 seconds
2013-02-02 11:31:52:LazyCron 5 minute test - 2013-02-02 11:31:52 - 468 seconds
2013-02-02 11:37:22:LazyCron 5 minute test - 2013-02-02 11:37:22 - 330 seconds
2013-02-02 11:43:14:LazyCron 5 minute test - 2013-02-02 11:43:14 - 352 seconds
2013-02-02 11:49:13:LazyCron 5 minute test - 2013-02-02 11:49:13 - 359 seconds
2013-02-02 11:54:37:LazyCron 5 minute test - 2013-02-02 11:54:37 - 324 seconds
2013-02-02 12:00:07:LazyCron 5 minute test - 2013-02-02 12:00:07 - 330 seconds
2013-02-02 12:05:07:LazyCron 5 minute test - 2013-02-02 12:05:07 - 300 seconds
2013-02-02 12:12:42:LazyCron 5 minute test - 2013-02-02 12:12:42 - 455 seconds
2013-02-02 12:18:35:LazyCron 5 minute test - 2013-02-02 12:18:35 - 353 seconds
2013-02-02 12:23:37:LazyCron 5 minute test - 2013-02-02 12:23:37 - 302 seconds
2013-02-02 12:30:54:LazyCron 5 minute test - 2013-02-02 12:30:54 - 437 seconds
2013-02-02 12:36:22:LazyCron 5 minute test - 2013-02-02 12:36:22 - 328 seconds
2013-02-02 12:41:57:LazyCron 5 minute test - 2013-02-02 12:41:57 - 335 seconds
2013-02-02 12:50:48:LazyCron 5 minute test - 2013-02-02 12:50:48 - 531 seconds
2013-02-02 12:56:58:LazyCron 5 minute test - 2013-02-02 12:56:58 - 370 seconds
2013-02-02 13:03:03:LazyCron 5 minute test - 2013-02-02 13:03:03 - 365 seconds
2013-02-02 13:15:03:LazyCron 5 minute test - 2013-02-02 13:15:03 - 720 seconds
2013-02-02 13:26:03:LazyCron 5 minute test - 2013-02-02 13:26:03 - 660 seconds
2013-02-02 13:31:46:LazyCron 5 minute test - 2013-02-02 13:31:46 - 343 seconds
2013-02-02 13:37:37:LazyCron 5 minute test - 2013-02-02 13:37:37 - 351 seconds

Notice how the number of elapsed seconds is always more than 5 minutes (and sometimes a lot more). That's just because it has to be triggered by a PageView. In most cases, this is okay because a lack of pageviews usually means there's nobody there to see what was updated anyway. :)

 
  • Like 2

Share this post


Link to post
Share on other sites

As I told, I'm not an expert, but for this I will stick to my answer :)

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MarkE
      LazyCron operates on an elapsed time interval, but you may want it to operate at specified times.
      Assuming your website is going to be visited sufficiently frequently (either by real users or by a Cron job), you can do this by editing the file /assets/cache/LazyCron.cache. This file contains 21 timestamps – one for each of the LazyCron specified intervals (e.g. “everyMinute”). The timestamp denotes when that interval was last triggered. To make the trigger happen at the required time, change the timestamp in the matching row to be when the previous trigger should have occurred.
      To make this easier, I have done an Excel spreadsheet (“Lazycron_fill.xlsx”) to simplify the process – just enter the required benchmark timings and copy the result into the LazyCron.cache file.
      Lazycron_fill.xlsx
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By Ken Muldrew
      I'm trying to get a short routine to run once per day that will look at some pages and send a reminder email when that customer's subscription (yearly) is about to expire. When I run the code in a template then it works without issue, but inside my lazycron service routine, I get an "Error: Uncaught Error: Call to a member function get() on null" as if the database cannot be found. My autoload module is just the sample HelloWorld module included with ProcessWire, editted to perform this task. The whole of it is included below (I've stripped out the code that generates the email because it never gets past $pages->find):
       
      <?php namespace ProcessWire;
      /**
       * ProcessWire 'LazyCronLoad'  module
       *
       */
      class LazyCronLoad extends WireData implements Module {
          public static function getModuleInfo() {
              return array(
                  'title' => 'LazyCronLoad', 
                  'version' => 1, 
                  'summary' => 'Just loads a lazy cron callback.',
                  'singular' => true, 
                  'autoload' => true, 
                  );
          }
          public function init() {
              // initialize the hook in the AutoLoad module
              $this->addHook('LazyCron::everyDay', $this, 'myHook');
              
          }
          public function myHook(HookEvent $e) {
              // called once per day
              wire('log')->save('user_activities',' lazy cron service routine');
                  $transport_pages = $pages->find("template=aggregate-entry, aggregate_type.title='Transport'");
                  foreach ($transport_pages as $page) {
                      if (($page->purchase_date + 30325800 < time()) && ($page->purchase_date + 30412600 > time())) { // between 351 and 352 days
                          wire('log')->save('user_activities', $page->id . ' email reminder sent');
                          // send email
                  }
              }
          }        
          
      }
      The first wire('log') shows up but the second one doesn't (the purchase_date condition is met (as demonstrated by running the code in a template close in time to when the lazycron routine executes)). The error log gives the Uncaught Error shown above. 
      I think this is a beginner's mistake with something obvious being missed and would be grateful for any assistance in fixing it.
    • By benbyf
      Hello, having trouble getting PW to save my user or field in cron hook.
      It seems to report the same field info each time unchanged. It should be counting down in seconds.
      public function init() { // set to every30Seconds in settings $this->defaultInterval = $this->cron_check; // add hooks to CRON $this->addHookAfter("LazyCron::{$this->defaultInterval}", $this, 'checkUserSubscription'); } public function ___checkUserSubscription(HookEvent $e){ // seconds since last lazycron $seconds = $e->arguments[0]; $log = wire('log'); $field = $this->exclusiveRole; // users with specific role and field $users = $this->users->find("roles=$field"); foreach ($users as $user) { // get stored time $currentTime = $user->get($this->fieldName); // if time not null if($currentTime){ // evalute stored time minus time past $resultTime = intval($currentTime) - intval($seconds); $log->message("result ".$resultTime); // save user page $user->set($this->fieldName, $resultTime); $this->users->save($user); } } }  
    • By cosmicsafari
      Hi all,
      I have noticed that LazyCorn seems to run maybe once or twice and then just stops.
      Has anybody else encountered this, I have the following within 2 different modules.
      public function init(){ $this->addHookAfter('ProcessModule::executeEdit', $this, 'configurableButtons'); wire()->addHook('LazyCron::every15Minutes', $this, 'autoGenerate'); } public function init(){ wire('forms')->addHookAfter('ProcessModule::executeEdit', $this, 'configurableButtons'); wire()->addHook('LazyCron::everyHour', $this, 'getExport'); } Does having multiple LazyCrons setup cause any issues?
×
×
  • Create New...