Browse Source

优化若干内容

Pchen. 4 days ago
parent
commit
17f2b609a1

+ 2 - 2
apis/Lepao/SingleRun.js

@@ -104,9 +104,9 @@ class SingleRun extends API {
                     retry: 0
                 }
 
-                publishRunforgeTask(channel, payload)
+                publishRunforgeTask(channel, payload, this.logger)
             } catch (err) {
-                this.logger.error(`后台乐跑任务异常:${err.stack}`)
+                this.logger.error(`手动乐跑 MQ 投递失败(前端已返回成功):${err.stack}`)
             }
 
         } catch (err) {

+ 0 - 2
apis/User/uniLogin/BindSocial.js

@@ -8,7 +8,6 @@ const {
     getUserSocialBindings,
     insertSocialBinding,
     updateSocialBindingProfile,
-    syncLegacySocialMirror,
     toSocialBindingSummary
 } = require('../../../lib/UserSocialBinding')
 
@@ -69,7 +68,6 @@ class BindSocial extends API {
                 'UPDATE users SET nickname = COALESCE(NULLIF(nickname, ""), ?), avatar = COALESCE(NULLIF(avatar, ""), ?) WHERE uuid = ?',
                 [nickname, faceimg, uuid]
             )
-            await syncLegacySocialMirror(uuid, type)
 
             const newBindings = await getUserSocialBindings(uuid)
             return res.json({

+ 3 - 5
apis/User/uniLogin/Login.js

@@ -8,8 +8,7 @@ const {
     getBindingByIdentity,
     getLegacyUserByIdentity,
     insertSocialBinding,
-    updateSocialBindingProfile,
-    syncLegacySocialMirror
+    updateSocialBindingProfile
 } = require('../../../lib/UserSocialBinding')
 
 class Login extends API {
@@ -59,8 +58,8 @@ class Login extends API {
                 uuid = uuidv4()
                 const username = `用户${uuid.slice(0, 8)}`
 
-                let regSql = 'INSERT INTO users (uuid, username, registTime, social_uid, social_type, nickname, avatar, email) VALUES (?,?,?,?,?,?,?,?) '
-                let regRows = await db.query(regSql, [uuid, username, time, social_uid, type, nickname, faceimg, '未设置'])
+                let regSql = 'INSERT INTO users (uuid, username, registTime, nickname, avatar, email) VALUES (?,?,?,?,?,?) '
+                let regRows = await db.query(regSql, [uuid, username, time, nickname, faceimg, '未设置'])
                 if (!regRows || regRows.affectedRows !== 1) {
                     this.logger.error(`聚合登录用户注册失败!数据库错误`)
                     return res.json({
@@ -96,7 +95,6 @@ class Login extends API {
                 [time, nickname, uuid]
             )
             await updateSocialBindingProfile(type, social_uid, nickname, faceimg)
-            await syncLegacySocialMirror(uuid, type)
 
             res.json({
                 ...BaseStdResponse.OK,

+ 0 - 3
apis/User/uniLogin/UnbindSocial.js

@@ -6,7 +6,6 @@ const { normalizeSocialType } = require('../../../lib/UniLoginClient')
 const {
     getUserSocialBindings,
     removeSocialBinding,
-    syncLegacySocialMirror,
     toSocialBindingSummary
 } = require('../../../lib/UserSocialBinding')
 
@@ -66,8 +65,6 @@ class UnbindSocial extends API {
                     msg: '解绑失败,请稍后再试'
                 })
 
-            await syncLegacySocialMirror(uuid)
-
             const newBindings = await getUserSocialBindings(uuid)
             return res.json({
                 ...BaseStdResponse.OK,

+ 3 - 1
lib/Lepao/Worker.js

@@ -1548,9 +1548,11 @@ class Worker {
                 }
             })
 
-            this.logger.info('RunForge Worker 启动成功')
+            this.logger.info(`RunForge Worker 启动成功,消费队列 ${this.taskQueue}`)
         } catch (err) {
             this.logger.error('RunForge Worker 启动失败: ' + err.stack)
+            this.running = false
+            throw err
         }
     }
 

+ 8 - 2
lib/Server.js

@@ -16,6 +16,8 @@ const {
     shouldServeApi,
     shouldRunLepaoWorker
 } = require('./serverRole')
+const { TASK_QUEUE } = require('../plugin/mq/runforgeTaskMq')
+const { PREFIX } = require('../plugin/mq/mqPrefix')
 
 class SERVER {
     constructor() {
@@ -76,7 +78,9 @@ class SERVER {
                     process.exit(1)
                 }
             } else {
-                this.logger.info('serverRole=api,跳过乐跑 Worker 与 Redis→MQ 调度')
+                this.logger.info(
+                    `serverRole=api,跳过乐跑 Worker;乐跑任务将投递到 MQ 队列「${TASK_QUEUE}」(mqPrefix=${JSON.stringify(PREFIX)})`
+                )
             }
         } catch (e) {
             this.logger.error('❌ RabbitMQ 初始化失败')
@@ -156,7 +160,9 @@ class SERVER {
     }
 
     start() {
-        this.logger.info(`============正在启动 (role=${this.serverRole})============`)
+        this.logger.info(
+            `============正在启动 (role=${this.serverRole}, lepaoQueue=${TASK_QUEUE}, mqPrefix=${JSON.stringify(PREFIX)})============`
+        )
 
         this.initDB()
             .then(() => this.initMQ())

+ 11 - 25
lib/UserSocialBinding.js

@@ -19,12 +19,17 @@ async function getLegacyUserByIdentity(socialType, socialUid) {
     if (!type || !socialUid)
         return null
 
-    const rows = await db.query(
-        'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1',
-        [socialUid, type]
-    )
-
-    return rows?.[0] || null
+    try {
+        const rows = await db.query(
+            'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1',
+            [socialUid, type]
+        )
+        return rows?.[0] || null
+    } catch (error) {
+        if (error?.code === 'ER_BAD_FIELD_ERROR')
+            return null
+        throw error
+    }
 }
 
 async function getUserSocialBindings(userUuid) {
@@ -60,24 +65,6 @@ async function updateSocialBindingProfile(socialType, socialUid, socialNickname
     )
 }
 
-async function syncLegacySocialMirror(userUuid, preferredSocialType = null) {
-    const bindings = await getUserSocialBindings(userUuid)
-    const preferredType = normalizeSocialType(preferredSocialType)
-    const binding = bindings.find(item => item.social_type === preferredType) || bindings[0]
-
-    if (!binding) {
-        return await db.query(
-            'UPDATE users SET social_uid = NULL, social_type = NULL WHERE uuid = ?',
-            [userUuid]
-        )
-    }
-
-    return await db.query(
-        'UPDATE users SET social_uid = ?, social_type = ? WHERE uuid = ?',
-        [binding.social_uid, binding.social_type, userUuid]
-    )
-}
-
 async function removeSocialBinding(userUuid, socialType) {
     const type = normalizeSocialType(socialType)
     if (!type)
@@ -108,7 +95,6 @@ module.exports = {
     getUserSocialBindings,
     insertSocialBinding,
     updateSocialBindingProfile,
-    syncLegacySocialMirror,
     removeSocialBinding,
     toSocialBindingSummary
 }

+ 8 - 2
plugin/mq/runforgeTaskMq.js

@@ -12,12 +12,18 @@ async function assertRunforgeTaskIngress(channel, logger) {
 /**
  * 投递乐跑任务 JSON 消息体(与 Worker 消费格式一致)
  */
-function publishRunforgeTask(channel, messageObject) {
+function publishRunforgeTask(channel, messageObject, logger) {
     const body = Buffer.from(JSON.stringify(messageObject))
-    channel.sendToQueue(TASK_QUEUE, body, {
+    const ok = channel.sendToQueue(TASK_QUEUE, body, {
         persistent: true,
         contentType: 'application/json'
     })
+    if (!ok) {
+        throw new Error(`MQ 背压,未能写入队列 ${TASK_QUEUE}`)
+    }
+    logger?.info?.(
+        `乐跑任务已投递 MQ queue=${TASK_QUEUE} id=${messageObject?.id} type=${messageObject?.type}`
+    )
 }
 
 module.exports = {