Skip to content

Commit

Permalink
Add partial implementation of cron-based external-groups sync
Browse files Browse the repository at this point in the history
  • Loading branch information
forevermatt committed Sep 4, 2024
1 parent 763a853 commit 930e932
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
66 changes: 66 additions & 0 deletions application/common/components/ExternalGroupsSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace common\components;

use common\models\User;
use Yii;
use yii\base\Component;

class ExternalGroupsSync extends Component
{
public const MAX_SYNC_SETS = 20;

public static function syncAllSets(array $syncSetsParams)
{
for ($i = 1; $i <= self::MAX_SYNC_SETS; $i++) {
$appPrefixKey = sprintf('set%uAppPrefix', $i);
$googleSheetIdKey = sprintf('set%uGoogleSheetId', $i);

if (! array_key_exists($appPrefixKey, $syncSetsParams)) {
Yii::warning(sprintf(
'Finished syncing external groups after %s sync set(s).',
($i - 1)
));
break;
}

$appPrefix = $syncSetsParams[$appPrefixKey] ?? null;
$googleSheetId = $syncSetsParams[$googleSheetIdKey] ?? null;

if (empty($appPrefix) || empty($googleSheetId)) {
Yii::error(sprintf(
'Unable to do external-groups sync set %s: '
. 'app-prefix (%s) or Google Sheet ID (%s) was empty.',
$i,
json_encode($appPrefix),
json_encode($googleSheetId),
));
} else {
Yii::warning(sprintf(
'Syncing %s external groups from Google Sheet (%s)...',
json_encode($appPrefix),
$googleSheetId
));
self::syncSet($appPrefix, $googleSheetId);
}
}
}

private static function syncSet(string $appPrefix, string $googleSheetId)
{
$desiredExternalGroups = self::getExternalGroupsFromGoogleSheet($googleSheetId);
$errors = User::syncExternalGroups($appPrefix, $desiredExternalGroups);
Yii::warning(sprintf(
'Ran sync for %s external groups.',
$appPrefix
));

if (! empty($errors)) {
Yii::error(sprintf(
"Errors that occurred while syncing %s external groups: \n%s",
$appPrefix,
join("\n", $errors)
));
}
}
}
1 change: 1 addition & 0 deletions application/common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
],
Env::getArrayFromPrefix('ABANDONED_USER_')
),
'externalGroupsSyncSets' => Env::getArrayFromPrefix('EXTERNAL_GROUPS_SYNC_'),
'googleAnalytics' => [
'apiSecret' => Env::get('GA_API_SECRET'),
'measurementId' => Env::get('GA_MEASUREMENT_ID'),
Expand Down
11 changes: 11 additions & 0 deletions application/console/controllers/CronController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace console\controllers;

use common\components\ExternalGroupsSync;
use common\helpers\Utils;
use common\models\Invite;
use common\models\Method;
Expand Down Expand Up @@ -137,6 +138,16 @@ public function actionExportToSheets()
User::exportToSheets();
}

/**
* Sync external groups from Google Sheets
*/
public function actionSyncExternalGroups()
{
ExternalGroupsSync::syncAllSets(
\Yii::$app->params['externalGroupsSyncSets'] ?? []
);
}

/**
* Run all cron tasks, catching and logging errors to allow remaining tasks to run after an exception
*/
Expand Down
8 changes: 8 additions & 0 deletions local.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,11 @@ [email protected]

# spreadsheet ID
GOOGLE_spreadsheetId=putAnActualSheetIDHerejD70xAjqPnOCHlDK3YomH

# === external groups sync sets === #

# EXTERNAL_GROUPS_SYNC_set1AppPrefix=
# EXTERNAL_GROUPS_SYNC_set1GoogleSheetId=
# EXTERNAL_GROUPS_SYNC_set2AppPrefix=
# EXTERNAL_GROUPS_SYNC_set2GoogleSheetId=
# ... with as many sets as you want.

0 comments on commit 930e932

Please sign in to comment.