LazyCron
Run scheduled jobs at fixed intervals without setting up a system cron job
Lazy Because execution is piggy-backed on regular page views, intervals are guaranteed to be
at least as long as specified — never shorter. Accuracy depends on site traffic. For
precise timing, trigger Lazy Expand all Collapse all API reference When Lazy If the module's CLI mode only setting is enabled ( Attach a hook to one of the interval methods in your module's Use All interval methods are hookable. The first time Lazy Run all pending Lazy Return all supported interval methods keyed by seconds. Return the interval method whose period is closest to the given number of seconds. Return the number of seconds associated with an interval method name. The method name is
case-sensitive. Return the CLI commands provided by this module (used by ProcessWire's CLI router). Dispatch a CLI command. Usually called by Lazy When As a convenience, Lazy This is useful for quick, template-level cron jobs that do not warrant a custom module. It is called 'lazy' because it's triggered by a pageview, so its accuracy
executing at specific times will depend upon how many pageviews your site gets.
So when a specified time is triggered, it's guaranteed to have been that length
of time OR longer. This is fine for most cases.
But here's how you make it NOT lazy: Setup a real CRON job to pull a page from your site once per minute.
Here is an example of a command that you could schedule to execute once per
minute: wget --quiet --no-cache -O - http://www.your-site.com > /dev/null In your own module or template, add the function you want executed:
public function myFunc(HookEvent $e) { echo "30 Minutes have passed!"; } Then add the hook to it in your module's init() function:
$this->addHook('Lazy create your hook function
function myHook(HookEvent $e) { echo "30 Minutes have passed!"; } add a hook to it:
$wire->addHook('Lazy every30Seconds
everyMinute
every2Minutes
every3Minutes
every4Minutes
every5Minutes
every10Minutes
every15Minutes
every30Minutes
every45Minutes
everyHour
every2Hours
every4Hours
every6Hours
every12Hours
everyDay
every2Days
every4Days
everyWeek
every2Weeks
every4Weeks Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the In addition to the methods and properties above, Lazy$lazyCron = $modules->get('LazyProcessPageView::finished and runs
pending jobs after the page response has been sent. This keeps cron overhead from slowing
down the visitor experience.cliOnly), Lazyphp index.php lazycron run or
with a system cron job such as:wget --quiet --no-cache -O - https://www.example.com/ > /dev/nullIn a module
init() or ready() method:public function init() {
$this->addHook('LazyIn templates or site hooks
$wire->addHook() (or wire()->addHook()) with a globally defined callback function:$wire->addHook('LazyMethod Seconds Approximate interval every30Seconds30 30 seconds everyMinute60 1 minute every2Minutes120 2 minutes every3Minutes180 3 minutes every4Minutes240 4 minutes every5Minutes300 5 minutes every10Minutes600 10 minutes every15Minutes900 15 minutes every30Minutes1800 30 minutes every45Minutes2700 45 minutes everyHour3600 1 hour every2Hours7200 2 hours every4Hours14400 4 hours every6Hours21600 6 hours every12Hours43200 12 hours everyDay86400 1 day every2Days172800 2 days every4Days345600 4 days everyWeek604800 1 week every2Weeks1209600 2 weeks every4Weeks2419200 4 weeks execute()
execute(): voidcliOnly is enabled. You can also call it directly if you have a reference to the
module instance.$modules->get('LazygetTimeFuncs()
getTimeFuncs(): array$funcs = $modules->get('LazygetTimeFuncName($seconds)
getTimeFuncName(int $seconds): string$name = $modules->get('LazygetTimeFuncSeconds($timeFuncName)
getTimeFuncSeconds(string $timeFuncName): int$seconds = $modules->get('LazygetCliCommands()
getCliCommands(): array$commands = $modules->get('LazyexecuteCli(array $args)
executeCli(array $args): voidphp index.php lazycron <command>.$modules->get('LazyProperty Type Description cliOnlyboolWhen true, Lazy lastTimeintUnix timestamp of the most recent Lazy lastResultstringSummary output from the most recent execution. lazycron CLI namespace:# Run pending jobs immediately
php index.php lazycron run
# List hooks currently attached to LazycliOnly is enabled, run is the only way (other than a real cron job hitting the
site) to trigger Lazy/site/templates/Lazy whose name matches an interval method. For example,
/site/templates/Lazy runs with the everyDay interval. All API
variables ($pages, $config, etc.) are extracted into the file's scope.// /site/templates/Lazy/site/assets/cache/Lazy) to prevent concurrent
runs. The lock expires automatically after one hour if a previous run failed fatally./site/assets/cache/Lazy.$modules->get('Lazy.wire/modules/System/Lazy.API reference: methods, properties, hooks
USAGE IN YOUR MODULES:
PROCEDURAL USAGE (i.e. in templates):
FUNCTIONS YOU CAN HOOK:
Lazy class also inherits all the methods and properties of: WireData and Wire.Common
Properties
Name Return Summary Lazy int bool When non-empty disallow running during http requests Lazy string Last run result Lazy int Unix timestamp when Lazy Additional methods and properties
API reference based on ProcessWire core version 3.0.269