Skip to content

Commit

Permalink
Merge pull request #11 from 26B/issue/cooldown-period
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes authored Jun 28, 2022
2 parents b60bdb0 + 025d2f8 commit 6095507
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Config/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,26 @@ public function get_pot_path() : string {
return "{$path}.pot";
}

/**
* Get cooldown parameter.
*
* @return int|bool Positive int or boolean.
*/
public function get_cooldown() {
if (
! isset( $this->config['cooldown'] )
|| ( is_int( $this->config['cooldown'] ) && $this->config['cooldown'] <= 0 )
) {
return false;
}

if ( is_bool( $this->config['cooldown'] ) || is_int( $this->config['cooldown'] ) ) {
return $this->config['cooldown'];
}

return false;
}

/**
* Parse filename in config and replace requested arguments with their values.
*
Expand Down
15 changes: 15 additions & 0 deletions lib/Translations/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
class Download extends ServiceBase {

/**
* Default cooldown time in microseconds.
*
* @var int
*/
const DEFAULT_COOLDOWN = 100000;

/**
* Keys that are accepted for exporting/downloading a locale.
*
Expand Down Expand Up @@ -45,8 +52,16 @@ public function download() : array {

$lock = LockHandler::get_instance();
$last_modified = $lock->get( $this->config->get_name(), 'Last-Modified', '' );
$cooldown = $this->config->get_cooldown();
$cooldown_time = is_bool( $cooldown ) ? self::DEFAULT_COOLDOWN : $cooldown;
$first = true;

foreach ( $this->config->get_locales() as $locale ) {
if ( $cooldown && ! $first ) {
usleep( $cooldown_time );
}
$first = false;

$export = $this->config->get_client()->export( $this->make_export_config( $locale, $last_modified ) );

if ( $export === '' ) {
Expand Down

0 comments on commit 6095507

Please sign in to comment.