Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache crawler - run within a certain range of time only #621

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/base.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class Base extends Root
## -------------- Crawler ----------------- ##
## -------------------------------------------------- ##
const O_CRAWLER = 'crawler';
const O_CRAWLER_SCHEDULE_TIME = 'crawler-schedule_time';
const O_CRAWLER_USLEEP = 'crawler-usleep';
const O_CRAWLER_RUN_DURATION = 'crawler-run_duration';
const O_CRAWLER_RUN_INTERVAL = 'crawler-run_interval';
Expand Down Expand Up @@ -514,6 +515,7 @@ class Base extends Root

// Crawler
self::O_CRAWLER => false,
self::O_CRAWLER_SCHEDULE_TIME => '',
self::O_CRAWLER_USLEEP => 0,
self::O_CRAWLER_RUN_DURATION => 0,
self::O_CRAWLER_RUN_INTERVAL => 0,
Expand Down
37 changes: 37 additions & 0 deletions src/crawler.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,37 @@ public static function async_handler($manually_run = false)
self::start($manually_run);
}

/**
* Check if crawler can run in the choosen time period
*
* @since 6.1
*/
public static function _crawler_in_schedule_time()
{
$now = new DateTime();
$class_settings = self::cls();
$schedule_times = $class_settings->conf(Base::O_CRAWLER_SCHEDULE_TIME, '');
if($schedule_times!== ''){
$schedule_times = explode(',', $schedule_times);

foreach ($schedule_times as $time) {
if ($time !== '') {
$hours = explode('-', $time);
$start = new DateTime($hours[0] . ":00");
$end = new DateTime($hours[1] . ":00");
if ($now < $end && $now > $start) {
return true;
}
}
}
Tymotey marked this conversation as resolved.
Show resolved Hide resolved

return false;
}
else{
return true;
}
}

/**
* Proceed crawling
*
Expand All @@ -259,6 +290,12 @@ public static function start($manually_run = false)
self::debug('......crawler is NOT allowed by the server admin......');
return false;
}

$crawler_is_in_time = self::cls()->_crawler_in_schedule_time();
if (!$manually_run && !$crawler_is_in_time) {
self::debug('......crawler is NOT allowed in this time slot......');
return false;
}

if ($manually_run) {
self::debug('......crawler manually ran......');
Expand Down
1 change: 1 addition & 0 deletions src/lang.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public static function title($id)
self::O_CDN_CLOUDFLARE => __('Cloudflare API', 'litespeed-cache'),

self::O_CRAWLER => __('Crawler', 'litespeed-cache'),
self::O_CRAWLER_SCHEDULE_TIME => __('Running time', 'litespeed-cache'),
self::O_CRAWLER_USLEEP => __('Delay', 'litespeed-cache'),
self::O_CRAWLER_RUN_DURATION => __('Run Duration', 'litespeed-cache'),
self::O_CRAWLER_RUN_INTERVAL => __('Interval Between Runs', 'litespeed-cache'),
Expand Down
19 changes: 19 additions & 0 deletions tpl/crawler/settings-general.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@
</td>
</tr>

<tr>
<th>
<?php $id = Base::O_CRAWLER_SCHEDULE_TIME; ?>
<?php $this->title($id); ?>
</th>
<td>
<?php $this->build_input($id); ?>
<div class="litespeed-desc">
<?php echo __('Change the crawler running time.', 'litespeed-cache'); ?>
Tymotey marked this conversation as resolved.
Show resolved Hide resolved
<br />
<?php echo __('You can add multiple times delimited by', 'litespeed-cache'); ?> <code>,</code>
<br />
<?php echo __('Server time:', 'litespeed-cache'); ?> <code><?php echo date('H:m'); ?></code>
<br />
<code>00:00-06:00,20:00-23:59</code>
</div>
</td>
</tr>

<tr>
<th>
<?php $id = Base::O_CRAWLER_USLEEP; ?>
Expand Down