forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SyncTaskScheduler as a separete class.
- Loading branch information
1 parent
9176172
commit 526b4b7
Showing
9 changed files
with
96 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/ResourceManager/Compute/Commands.Compute/Strategies/AsyncCmdletExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.Azure.Commands.Common.Strategies; | ||
using System; | ||
using System.Management.Automation; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Commands.Compute.Strategies | ||
{ | ||
static class AsyncCmdletExtensions | ||
{ | ||
/// <summary> | ||
/// Note: the function must be called in the main PowerShell thread. | ||
/// </summary> | ||
/// <param name="cmdlet"></param> | ||
/// <param name="createAndStartTask"></param> | ||
public static void StartAndWait(this Cmdlet cmdlet, Func<IAsyncCmdlet, Task> createAndStartTask) | ||
{ | ||
var asyncCmdlet = new AsyncCmdlet(cmdlet); | ||
asyncCmdlet.Scheduler.Wait(createAndStartTask(asyncCmdlet)); | ||
} | ||
|
||
sealed class AsyncCmdlet : IAsyncCmdlet | ||
{ | ||
public SyncTaskScheduler Scheduler { get; } = new SyncTaskScheduler(); | ||
|
||
readonly Cmdlet _Cmdlet; | ||
|
||
public AsyncCmdlet(Cmdlet cmdlet) | ||
{ | ||
_Cmdlet = cmdlet; | ||
} | ||
|
||
public void WriteVerbose(string message) | ||
=> Scheduler.BeginInvoke(() => _Cmdlet.WriteVerbose(message)); | ||
|
||
public Task<bool> ShouldProcessAsync(string target, string action) | ||
=> Scheduler.Invoke(() => _Cmdlet.ShouldProcess(target, action)); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/ResourceManager/Compute/Commands.Compute/Strategies/Client.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.Azure.Commands.Common.Authentication; | ||
using Microsoft.Azure.Commands.Common.Authentication.Abstractions; | ||
using Microsoft.Azure.Commands.Common.Strategies; | ||
using Microsoft.Rest; | ||
|
||
namespace Microsoft.Azure.Commands.Compute.Strategies | ||
{ | ||
sealed class Client : IClient | ||
{ | ||
public string SubscriptionId { get; } | ||
|
||
IAzureContext Context { get; } | ||
|
||
public Client(IAzureContext context) | ||
{ | ||
Context = context; | ||
SubscriptionId = Context.Subscription.Id; | ||
} | ||
|
||
public T GetClient<T>() | ||
where T : ServiceClient<T> | ||
=> AzureSession.Instance.ClientFactory.CreateArmClient<T>( | ||
Context, AzureEnvironment.Endpoint.ResourceManager); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/ResourceManager/Compute/Commands.Compute/Strategies/IAsyncCmdlet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Commands.Compute.Strategies | ||
{ | ||
interface IAsyncCmdlet | ||
{ | ||
void WriteVerbose(string message); | ||
|
||
Task<bool> ShouldProcessAsync(string target, string action); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters