你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

如何为最佳辅助角色分配模式自定义辅助角色的排名方式

分发 best-worker 模式选择能够首先处理作业的辅助角色。 可以使用表达式或 Azure 函数来比较两个辅助角色,来自定义对辅助角色进行排名的逻辑。 以下示例演示如何使用自己的 Azure 函数自定义此逻辑。

场景:最佳辅助角色分发模式下的自定义评分规则

我们希望在其与队列关联的辅助角色之间分配产品/服务。 工作人员将根据其标签和技能集获得分数。 得分最高的辅助角色应获得第一个产品/服务(BestWorker 分发模式)。

显示最佳辅助角色分配模式问题语句的关系图

情况

  • 已创建并分类作业。
    • 作业具有以下与之关联的标签
      • [“CommunicationType”] = “Chat”
      • [“IssueType”] = “XboxSupport”
      • [“Language”] = “en”
      • [“HighPriority”] = true
      • [“SubIssueType”] = “ConsoleMalfunction”
      • [“ConsoleType”] = “XBOX_SERIES_X”
      • [“Model”] = “XBOX_SERIES_X_1TB”
    • 作业具有以下与它关联的 WorkerSelector
      • [“English”] >= 7
      • [“ChatSupport”] = true
      • [“XboxSupport”] = true
  • 作业当前处于“已排队”状态;在等待与辅助角色匹配的 Xbox 硬件支持队列 中排队。
  • 多个辅助角色同时可用。
    • 辅助角色 1 已使用以下标签创建
      • [“HighPrioritySupport”] = true
      • [“HardwareSupport”] = true
      • [“Support_XBOX_SERIES_X”] = true
      • [“English”] = 10
      • [“ChatSupport”] = true
      • [“XboxSupport”] = true
    • 辅助角色 2 已使用以下标签创建
      • [“HighPrioritySupport”] = true
      • [“HardwareSupport”] = true
      • [“Support_XBOX_SERIES_X”] = true
      • [“Support_XBOX_SERIES_S”] = true
      • [“English”] = 8
      • [“ChatSupport”] = true
      • [“XboxSupport”] = true
    • 辅助角色 3 已使用以下标签创建
      • [“HighPrioritySupport”] = false
      • [“HardwareSupport”] = true
      • [“Support_XBOX”] = true
      • [“English”] = 7
      • [“ChatSupport”] = true
      • [“XboxSupport”] = true

预期

在为辅助角色评分以选择哪个辅助角色获取第一个产品/服务时,我们希望以下行为。

评分辅助角色的决策流图

决策流(如上所示)如下所示:

  • 如果作业 不是 HighPriority

    • 带标签的辅助角色: [“Support_XBOX”] = true;获取 分数 100
    • 否则,获取 1
  • 如果作业为 HighPriority

    • 标签为 [“HighPrioritySupport”] = false;获取 分数 1
    • 否则,如果 [“HighPrioritySupport”] = true
      • 辅助角色是否专用于控制台类型 -> 辅助角色是否具有标签: [“Support_<jobLabels.ConsoleType>”] = true? 如果为 true,则辅助角色的分数为 200
      • 否则,获取 100

创建 Azure 函数

在进一步执行该过程之前,让我们首先定义一个对辅助角色进行评分的 Azure 函数。

注释

以下 Azure 函数使用 JavaScript。 有关详细信息,请参阅 快速入门:使用 Visual Studio Code 在 Azure 中创建 JavaScript 函数

辅助角色 1 的示例输入

{
  "job": {
    "CommunicationType": "Chat",
    "IssueType": "XboxSupport",
    "Language": "en",
    "HighPriority": true,
    "SubIssueType": "ConsoleMalfunction",
    "ConsoleType": "XBOX_SERIES_X",
    "Model": "XBOX_SERIES_X_1TB"
  },
  "selectors": [
    {
      "key": "English",
      "operator": "GreaterThanEqual",
      "value": 7,
      "expiresAfterSeconds": null
    },
    {
      "key": "ChatSupport",
      "operator": "Equal",
      "value": true,
      "expiresAfterSeconds": null
    },
    {
      "key": "XboxSupport",
      "operator": "Equal",
      "value": true,
      "expiresAfterSeconds": null
    }
  ],
  "worker": {
    "Id": "e3a3f2f9-3582-4bfe-9c5a-aa57831a0f88",
    "HighPrioritySupport": true,
    "HardwareSupport": true,
    "Support_XBOX_SERIES_X": true,
    "English": 10,
    "ChatSupport": true,
    "XboxSupport": true
  }
}

示例实现:

module.exports = async function (context, req) {
    context.log('Best Worker Distribution Mode using Azure Function');

    let score = 0;
    const jobLabels = req.body.job;
    const workerLabels = req.body.worker;

    const isHighPriority = !!jobLabels["HighPriority"];
    context.log('Job is high priority? Status: ' + isHighPriority);

    if(!isHighPriority) {
        const isGenericXboxSupportWorker = !!workerLabels["Support_XBOX"];
        context.log('Worker provides general xbox support? Status: ' + isGenericXboxSupportWorker);

        score = isGenericXboxSupportWorker ? 100 : 1;

    } else {
        const workerSupportsHighPriorityJob = !!workerLabels["HighPrioritySupport"];
        context.log('Worker provides high priority support? Status: ' + workerSupportsHighPriorityJob);

        if(!workerSupportsHighPriorityJob) {
            score = 1;
        } else {
            const key = `Support_${jobLabels["ConsoleType"]}`;
            
            const workerSpecializeInConsoleType = !!workerLabels[key];
            context.log(`Worker specializes in consoleType: ${jobLabels["ConsoleType"]} ? Status: ${workerSpecializeInConsoleType}`);

            score = workerSpecializeInConsoleType ? 200 : 100;
        }
    }
    context.log('Final score of worker: ' + score);

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: score
    };
}

辅助角色 1 的输出

200

对于上述实现,对于给定的工作,我们将获得以下辅助角色分数:

工人 得分
辅助角色 1 200
工作线程 2 200
辅助角色 3 1

基于最佳辅助角色模式分发产品/服务

现在,Azure 函数应用已准备就绪,让我们使用路由器 SDK 创建 BestWorkerDistribution 模式的实例。

var administrationClient = new JobRouterAdministrationClient("<YOUR_ACS_CONNECTION_STRING>");

// Setup Distribution Policy
var distributionPolicy = await administrationClient.CreateDistributionPolicyAsync(
    new CreateDistributionPolicyOptions(
        distributionPolicyId: "BestWorkerDistributionMode",
        offerExpiresAfter: TimeSpan.FromMinutes(5),
        mode: new BestWorkerMode(scoringRule: new FunctionRouterRule(new Uri("<insert function url>")))
    ) { Name = "XBox hardware support distribution" });

// Setup Queue
var queue = await administrationClient.CreateQueueAsync(
    new CreateQueueOptions(
        queueId: "XBox_Hardware_Support_Q",
        distributionPolicyId: distributionPolicy.Value.Id
    ) { Name = "XBox Hardware Support Queue" });

// Create workers
var worker1 = await client.CreateWorkerAsync(new CreateWorkerOptions(workerId: "Worker_1", capacity: 100)
    {
        Queues = { queue.Value.Id },
        Channels = { new RouterChannel(channelId: "Xbox_Chat_Channel", capacityCostPerJob: 10) },
        Labels =
        {
            ["English"] = new RouterValue(10),
            ["HighPrioritySupport"] = new RouterValue(true),
            ["HardwareSupport"] = new RouterValue(true),
            ["Support_XBOX_SERIES_X"] = new RouterValue(true),
            ["ChatSupport"] = new RouterValue(true),
            ["XboxSupport"] = new RouterValue(true)
        }
    });

var worker2 = await client.CreateWorkerAsync(new CreateWorkerOptions(workerId: "Worker_2", capacity: 100)
    {
        Queues = { queue.Value.Id },
        Channels = { new RouterChannel(channelId: "Xbox_Chat_Channel", capacityCostPerJob: 10) },
        Labels =
        {
            ["English"] = new RouterValue(8),
            ["HighPrioritySupport"] = new RouterValue(true),
            ["HardwareSupport"] = new RouterValue(true),
            ["Support_XBOX_SERIES_X"] = new RouterValue(true),
            ["ChatSupport"] = new RouterValue(true),
            ["XboxSupport"] = new RouterValue(true)
        }
    });

var worker3 = await client.CreateWorkerAsync(new CreateWorkerOptions(workerId: "Worker_3", capacity: 100)
    {
        Queues = { queue.Value.Id },
        Channels = { new RouterChannel(channelId: "Xbox_Chat_Channel", capacityCostPerJob: 10) },
        Labels =
        {
            ["English"] = new RouterValue(7),
            ["HighPrioritySupport"] = new RouterValue(true),
            ["HardwareSupport"] = new RouterValue(true),
            ["Support_XBOX_SERIES_X"] = new RouterValue(true),
            ["ChatSupport"] = new RouterValue(true),
            ["XboxSupport"] = new RouterValue(true)
        }
    });

// Create Job
var job = await client.CreateJobAsync(
    new CreateJobOptions(jobId: "job1", channelId: "Xbox_Chat_Channel", queueId: queue.Value.Id)
    {
        Priority = 100,
        ChannelReference = "ChatChannel",
        RequestedWorkerSelectors =
        {
            new RouterWorkerSelector(key: "English", labelOperator: LabelOperator.GreaterThanEqual, value: new RouterValue(7)),
            new RouterWorkerSelector(key: "ChatSupport", labelOperator: LabelOperator.Equal, value: new RouterValue(true)),
            new RouterWorkerSelector(key: "XboxSupport", labelOperator: LabelOperator.Equal, value: new RouterValue(true))
        },
        Labels =
        {
            ["CommunicationType"] = new RouterValue("Chat"),
            ["IssueType"] = new RouterValue("XboxSupport"),
            ["Language"] = new RouterValue("en"),
            ["HighPriority"] = new RouterValue(true),
            ["SubIssueType"] = new RouterValue("ConsoleMalfunction"),
            ["ConsoleType"] = new RouterValue("XBOX_SERIES_X"),
            ["Model"] = new RouterValue("XBOX_SERIES_X_1TB")
        }
    });

// Wait a few seconds and see which worker was matched
await Task.Delay(TimeSpan.FromSeconds(5));
var getJob = await client.GetJobAsync(job.Value.Id);
Console.WriteLine(getJob.Value.Assignments.Select(assignment => assignment.Value.WorkerId).First());

输出

Worker_1 // or Worker_2

Since both workers, Worker_1 and Worker_2, get the same score of 200,
the worker who has been idle the longest will get the first offer.