Skip to content

Commit

Permalink
Add —throttle option to cli syncing #112
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Dec 18, 2024
1 parent fcdd6e6 commit 1a82193
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Shopify

## Unreleased

- `shopify/sync` commands now support a `--throttle` option.

## 4.1.2 - 2024-04-15

- Fixed a PHP error that could occur when syncing products with emojis. ([#107](https://github.com/craftcms/shopify/issues/107))
Expand Down
23 changes: 23 additions & 0 deletions src/console/controllers/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ class SyncController extends Controller
/** @var string $defaultAction */
public $defaultAction = 'products';

/**
* @var bool Whether to slow down API requests to avoid rate limiting.
* @since 4.2.0
*/
public bool $throttle = false;

/**
* @inheritdoc
*/
public function options($actionID): array
{
$options = parent::options($actionID);
$options[] = 'throttle';
return $options;
}

/**
* Sync all Shopify data.
*/
Expand All @@ -47,7 +63,14 @@ private function _syncProducts(): void
$this->stdout('Syncing Shopify products…' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
// start timer
$start = microtime(true);

$originalThrottle = Plugin::getInstance()->getProducts()->throttle;
Plugin::getInstance()->getProducts()->throttle = $this->throttle;

Plugin::getInstance()->getProducts()->syncAllProducts();

Plugin::getInstance()->getProducts()->throttle = $originalThrottle;

// end timer
$time = microtime(true) - $start;
$this->stdout('Finished syncing ' . Product::find()->count() . ' product(s) in ' . round($time, 2) . 's' . PHP_EOL . PHP_EOL, Console::FG_GREEN);
Expand Down
16 changes: 16 additions & 0 deletions src/services/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ class Products extends Component
*/
public const EVENT_BEFORE_SYNCHRONIZE_PRODUCT = 'beforeSynchronizeProduct';

/**
* @var bool Whether to slow down API requests to avoid rate limiting.
* @since 4.2.0
*/
public bool $throttle = false;

/**
* @var int The number of seconds to sleep between requests when `$throttle` is enabled.
* @since 4.2.0
*/
public int $sleepSeconds = 1;

/**
* @param ShopifyProduct $product
* @return void
Expand All @@ -66,6 +78,10 @@ private function _updateProduct(ShopifyProduct $product): void
$api = Plugin::getInstance()->getApi();

$variants = $api->getVariantsByProductId($product->id);

if ($this->throttle) {
usleep((int) (1E6 * $this->sleepSeconds));
}
$productMetafields = $api->getMetafieldsByProductId($product->id);

foreach ($variants as &$variant) {
Expand Down

0 comments on commit 1a82193

Please sign in to comment.