没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing Parameters for Console Commands in Yii 2.0

Now, based on the value of the parameter $groupId, we need to synchronize the users under this tenant, which means this tenant needs to be placed first in order to meet the requirement. First, remove $groupId from $sortCmcApiGroupIds, then insert it at the beginning. Print the arrays before and after sorting. It works as expected

1. CmcConsoleUser/actionSync, existing implementation, reference: https://www.shuijingwanwq.com/2020/03/02/3964/ . Now we need to add a parameter: tenant ID

2. The parameter will be passed to the action method corresponding to the requested subcommand. Set the default value of the parameter $groupId to null

PHP

public function actionSync($groupId = null)

3. Check if $groupId is empty. If it is not empty and does not exist in $httpCmcApiGroupIds, exit successfully

PHP

        /* 判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出 */
        if (!empty($groupId) && !in_array($groupId, $httpCmcApiGroupIds)) {
            return ExitCode::OK;
        }

4. Sort based on the last synchronization time order, assign to: $sortCmcApiGroupIds, and print the arrays before and after sorting

PHP

        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        print_r($sortCmcApiGroupIds);
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);
        print_r($sortCmcApiGroupIds);
        exit;

Plaintext

PS E:\wwwroot\pcs-api> ./yii cmc-console-user/sync
Array
(
    [0] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

    [1] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [4] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => b28312e46044da2682e540d1fb838c67
            [cmc_console_user_last_synced_at] => 0
        )

)
Array
(
    [0] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [1] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [4] => Array
        (
            [group_id] => b28312e46044da2682e540d1fb838c67
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

)

5. Now, based on the value of the parameter $groupId, we need to synchronize the users under this tenant, which means this tenant needs to be placed first in order to meet the requirement. First, remove $groupId from $sortCmcApiGroupIds, then insert it at the beginning. Print the arrays before and after sorting. It works as expected. See Figure 1

Now, based on the value of the parameter $groupId, we need to synchronize the users under this tenant, which means this tenant needs to be placed first in order to meet the requirement. First, remove $groupId from $sortCmcApiGroupIds, then insert it at the beginning. Print the arrays before and after sorting. It works as expected
Figure 1
PHP

        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        print_r($sortCmcApiGroupIds);
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);
        print_r($sortCmcApiGroupIds);

        /* 判断 $groupId 是否为空,如果不为空 */
        if (!empty($groupId)) {
            // 销毁 $sortCmcApiGroupIds 中的 $groupId
            foreach ($sortCmcApiGroupIds as $sortCmcApiGroupIdKey => $sortCmcApiGroupId) {
                if ($sortCmcApiGroupId['group_id'] == $groupId) {
                    unset($sortCmcApiGroupIds[$sortCmcApiGroupIdKey]);
                    break;
                }
            }
            print_r($sortCmcApiGroupIds);
            // 将 $groupId 插入至 $sortCmcApiGroupIds 的开头
            array_unshift($sortCmcApiGroupIds, [
                'group_id' => $groupId,
                'cmc_console_user_last_synced_at' => 0, //上次同步时间
            ]);
        }

        print_r($sortCmcApiGroupIds);
        exit;

Plaintext

PS E:\wwwroot\pcs-api> ./yii cmc-console-user/sync b28312e46044da2682e540d1fb838c67
Array
(
    [0] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

    [1] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [4] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => b28312e46044da2682e540d1fb838c67
            [cmc_console_user_last_synced_at] => 0
        )

)
Array
(
    [0] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [1] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [4] => Array
        (
            [group_id] => b28312e46044da2682e540d1fb838c67
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

)
Array
(
    [0] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [1] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

)
Array
(
    [0] => Array
        (
            [group_id] => b28312e46044da2682e540d1fb838c67
            [cmc_console_user_last_synced_at] => 0
        )

    [1] => Array
        (
            [group_id] => 015ce30b116ce86058fa6ab4fea4ac63
            [cmc_console_user_last_synced_at] => 0
        )

    [2] => Array
        (
            [group_id] => 4fd58ceba1fbc537b5402302702131eb
            [cmc_console_user_last_synced_at] => 0
        )

    [3] => Array
        (
            [group_id] => f53df1a8d46108afc8ae9eeb3f0e1f0e
            [cmc_console_user_last_synced_at] => 0
        )

    [4] => Array
        (
            [group_id] => e774bfcf8fc4cfe2ce57ac875a266e94
            [cmc_console_user_last_synced_at] => 0
        )

    [5] => Array
        (
            [group_id] => c10e87f39873512a16727e17f57456a5
            [cmc_console_user_last_synced_at] => 1590649259
        )

)

6. Final code implementation

PHP

        // 设置同步标识的缓存键
        $redisCache = Yii::$app->redisCache;

        // HTTP 请求,获取开通有效服务的租户ID列表
        $httpCmcApiGroupIds = CmcApiGroupService::httpGetGroupIds();

        /* 判断 $httpCmcApiGroupIds 是否为空,如果为空,则成功退出 */
        if (empty($httpCmcApiGroupIds)) {
            // 延缓执行 60 * 60 秒
            // sleep(Yii::$app->params['cmcConsoleUser']['isEmptyYesSleepTime']);

            return ExitCode::OK;
        }

        /* 判断 $groupId 是否为空,如果不为空,且在 $httpCmcApiGroupIds 中不存在,则成功退出 */
        if (!empty($groupId) && !in_array($groupId, $httpCmcApiGroupIds)) {
            return ExitCode::OK;
        }

        // 设置租户ID列表的缓存键
        $redisCacheGroupIdsKey = 'cmc_api_group_ids';

        // 从缓存中取回租户ID列表
        $redisCacheGroupIdsData = $redisCache[$redisCacheGroupIdsKey];

        // 是否设置租户ID列表的缓存,默认:否
        $isSetRedisCacheGroupIds = false;

        if ($redisCacheGroupIdsData === false) {
            $cmcApiGroupIds = [];
            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                $cmcApiGroupIds[] = [
                    'group_id' => $httpCmcApiGroupId,
                    'cmc_console_user_last_synced_at' => 0, //上次同步时间
                ];
            }
            // 是否设置租户ID列表的缓存:是
            $isSetRedisCacheGroupIds = true;
        } else {
            // 获取 group_id 值列表
            $redisCacheGroupIds = ArrayHelper::getColumn($redisCacheGroupIdsData, 'group_id');
            $cmcApiGroupIds = $redisCacheGroupIdsData;

            /* 删除出现在 $cmcApiGroupIds 中但是未出现在 $httpCmcApiGroupIds 中的租户 */
            foreach ($cmcApiGroupIds as $cmcApiGroupIdKey => $cmcApiGroupId) {
                if (!in_array($cmcApiGroupId['group_id'], $httpCmcApiGroupIds)) {
                    unset($cmcApiGroupIds[$cmcApiGroupIdKey]);
                    // 是否设置租户ID列表的缓存:是
                    $isSetRedisCacheGroupIds = true;
                }
            }

            foreach ($httpCmcApiGroupIds as $httpCmcApiGroupId) {
                if (!in_array($httpCmcApiGroupId, $redisCacheGroupIds)) {
                    $cmcApiGroupIds[] = [
                        'group_id' => $httpCmcApiGroupId,
                        'cmc_console_user_last_synced_at' => 0, //上次同步时间
                    ];
                    // 是否设置租户ID列表的缓存:是
                    $isSetRedisCacheGroupIds = true;
                }
            }
        }

        // 判断是否设置租户ID列表的缓存
        if ($isSetRedisCacheGroupIds) {
            // 将 $cmcApiGroupIds 存放到缓存供下次使用,将数据在缓存中永久保留
            $redisCache->set($redisCacheGroupIdsKey, $cmcApiGroupIds);
        }


        // 基于上次同步时间顺序排列,赋值给:$sortCmcApiGroupIds
        $sortCmcApiGroupIds = $cmcApiGroupIds;
        ArrayHelper::multisort($sortCmcApiGroupIds, 'cmc_console_user_last_synced_at', SORT_ASC);

        /* 判断 $groupId 是否为空,如果不为空 */
        if (!empty($groupId)) {
            // 销毁 $sortCmcApiGroupIds 中的 $groupId
            foreach ($sortCmcApiGroupIds as $sortCmcApiGroupIdKey => $sortCmcApiGroupId) {
                if ($sortCmcApiGroupId['group_id'] == $groupId) {
                    unset($sortCmcApiGroupIds[$sortCmcApiGroupIdKey]);
                    break;
                }
            }
            // 将 $groupId 插入至 $sortCmcApiGroupIds 的开头
            array_unshift($sortCmcApiGroupIds, [
                'group_id' => $groupId,
                'cmc_console_user_last_synced_at' => 0, //上次同步时间
            ]);
        }


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系