Skip to content

Commit a621e27

Browse files
author
Manikanta Nallagatla
committed
Add assign2
1 parent d3360a6 commit a621e27

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/WebJobs.Script.WebHost/Controllers/InstanceController.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
@@ -41,27 +41,23 @@ public async Task<IActionResult> Assign([FromBody] EncryptedHostAssignmentContex
4141

4242
var assignmentContext = _startupContextProvider.SetContext(encryptedAssignmentContext);
4343

44-
// before starting the assignment we want to perform as much
45-
// up front validation on the context as possible
46-
string error = await _instanceManager.ValidateContext(assignmentContext);
47-
if (error != null)
48-
{
49-
return StatusCode(StatusCodes.Status400BadRequest, error);
50-
}
44+
return await AssignInternal(assignmentContext);
45+
}
5146

52-
// Wait for Sidecar specialization to complete before returning ok.
53-
// This shouldn't take too long so ok to do this sequentially.
54-
error = await _instanceManager.SpecializeMSISidecar(assignmentContext);
55-
if (error != null)
47+
[HttpPost]
48+
[Route("admin/instance/assign2")]
49+
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
50+
public async Task<IActionResult> Assign2([FromBody] FunctionsWorkerContainerAssignmentContext workerAssignmentContext)
51+
{
52+
if (workerAssignmentContext?.AssignmentContext == null || workerAssignmentContext.AssignmentContext == null)
5653
{
57-
return StatusCode(StatusCodes.Status500InternalServerError, error);
54+
return BadRequest("Assignment context is missing.");
5855
}
5956

60-
var succeeded = _instanceManager.StartAssignment(assignmentContext);
57+
_logger.LogDebug($"Starting container assignment for host : {Request?.Host}");
58+
var assignmentContext = _startupContextProvider.SetContext(workerAssignmentContext.AssignmentContext);
6159

62-
return succeeded
63-
? Accepted()
64-
: StatusCode(StatusCodes.Status409Conflict, "Instance already assigned");
60+
return await AssignInternal(assignmentContext);
6561
}
6662

6763
[HttpGet]
@@ -79,5 +75,30 @@ public IActionResult GetHttpHealthStatus()
7975
// Reaching here implies that http health of the container is ok.
8076
return Ok();
8177
}
78+
79+
private async Task<IActionResult> AssignInternal(HostAssignmentContext assignmentContext)
80+
{
81+
// before starting the assignment we want to perform as much
82+
// up front validation on the context as possible
83+
string error = await _instanceManager.ValidateContext(assignmentContext);
84+
if (error != null)
85+
{
86+
return StatusCode(StatusCodes.Status400BadRequest, error);
87+
}
88+
89+
// Wait for Sidecar specialization to complete before returning ok.
90+
// This shouldn't take too long so ok to do this sequentially.
91+
error = await _instanceManager.SpecializeMSISidecar(assignmentContext);
92+
if (error != null)
93+
{
94+
return StatusCode(StatusCodes.Status500InternalServerError, error);
95+
}
96+
97+
var succeeded = _instanceManager.StartAssignment(assignmentContext);
98+
99+
return succeeded
100+
? Accepted()
101+
: StatusCode(StatusCodes.Status409Conflict, "Instance already assigned");
102+
}
82103
}
83104
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// -----------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// -----------------------------------------------------------
4+
5+
using Newtonsoft.Json;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.WebHost.Models
8+
{
9+
public class FunctionsWorkerContainerAssignmentContext
10+
{
11+
[JsonProperty("assignmentContext")]
12+
public HostAssignmentContext AssignmentContext { get; private set; }
13+
}
14+
}

src/WebJobs.Script.WebHost/StartupContextProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public virtual HostAssignmentContext SetContext(EncryptedHostAssignmentContext e
142142
string decryptedContext = EncryptionHelper.Decrypt(encryptedContext.EncryptedContext, environment: _environment);
143143
var hostAssignmentContext = JsonConvert.DeserializeObject<HostAssignmentContext>(decryptedContext);
144144

145+
return SetContext(hostAssignmentContext);
146+
}
147+
148+
public virtual HostAssignmentContext SetContext(HostAssignmentContext hostAssignmentContext)
149+
{
145150
// Don't update StartupContext for warmup requests
146151
if (!hostAssignmentContext.IsWarmupRequest)
147152
{

0 commit comments

Comments
 (0)