Browse Source

Support watch for azure functions and fix a bug with active replicas (#595)

jkotalik/fixAzureFunctions
Justin Kotalik 6 years ago
committed by GitHub
parent
commit
dbcc90dc5a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/Microsoft.Tye.Core/ApplicationFactory.cs
  2. 1
      src/Microsoft.Tye.Core/AzureFunctionServiceBuilder.cs
  3. 2
      src/Microsoft.Tye.Hosting/Model/AzureFunctionRunInfo.cs
  4. 29
      src/Microsoft.Tye.Hosting/ProcessRunner.cs
  5. 5
      src/Microsoft.Tye.Hosting/Watch/DotNetWatcher.cs

16
src/Microsoft.Tye.Core/ApplicationFactory.cs

@ -225,15 +225,27 @@ namespace Microsoft.Tye
} }
else if (!string.IsNullOrEmpty(configService.AzureFunction)) else if (!string.IsNullOrEmpty(configService.AzureFunction))
{ {
var azureFunctionDirectory = Path.Combine(config.Source.DirectoryName!, configService.AzureFunction);
var functionBuilder = new AzureFunctionServiceBuilder( var functionBuilder = new AzureFunctionServiceBuilder(
configService.Name, configService.Name,
Path.Combine(config.Source.DirectoryName!, configService.AzureFunction)) azureFunctionDirectory)
{ {
Args = configService.Args, Args = configService.Args,
Replicas = configService.Replicas ?? 1, Replicas = configService.Replicas ?? 1,
FuncExecutablePath = configService.FuncExecutable FuncExecutablePath = configService.FuncExecutable,
}; };
foreach (var proj in Directory.EnumerateFiles(azureFunctionDirectory))
{
var fileInfo = new FileInfo(proj);
if (fileInfo.Extension == ".csproj" || fileInfo.Extension == ".fsproj")
{
functionBuilder.ProjectFile = fileInfo.FullName;
break;
}
}
// TODO liveness? // TODO liveness?
service = functionBuilder; service = functionBuilder;
} }

1
src/Microsoft.Tye.Core/AzureFunctionServiceBuilder.cs

@ -16,5 +16,6 @@ namespace Microsoft.Tye
public string? Args { get; set; } public string? Args { get; set; }
public string FunctionPath { get; } public string FunctionPath { get; }
public string? FuncExecutablePath { get; set; } public string? FuncExecutablePath { get; set; }
public string? ProjectFile { get; set; }
} }
} }

2
src/Microsoft.Tye.Hosting/Model/AzureFunctionRunInfo.cs

@ -13,10 +13,12 @@ namespace Microsoft.Tye.Hosting.Model
Args = function.Args; Args = function.Args;
FunctionPath = function.FunctionPath; FunctionPath = function.FunctionPath;
FuncExecutablePath = function.FuncExecutablePath; FuncExecutablePath = function.FuncExecutablePath;
ProjectFile = function.ProjectFile;
} }
public string? Args { get; } public string? Args { get; }
public string FunctionPath { get; } public string FunctionPath { get; }
public string? FuncExecutablePath { get; set; } public string? FuncExecutablePath { get; set; }
public string? ProjectFile { get; set; }
} }
} }

29
src/Microsoft.Tye.Hosting/ProcessRunner.cs

@ -322,7 +322,7 @@ namespace Microsoft.Tye.Hosting
WriteReplicaToStore(pid.ToString()); WriteReplicaToStore(pid.ToString());
if (_options.Watch && service.Description.RunInfo is ProjectRunInfo runInfo) if (_options.Watch)
{ {
// OnStart/OnStop will be called multiple times for watch. // OnStart/OnStop will be called multiple times for watch.
// Watch will constantly be adding and removing from the list, so only add here for watch. // Watch will constantly be adding and removing from the list, so only add here for watch.
@ -359,16 +359,21 @@ namespace Microsoft.Tye.Hosting
}, },
Build = async () => Build = async () =>
{ {
var buildResult = await ProcessUtil.RunAsync("dotnet", $"build \"{service.Status.ProjectFilePath}\" /nologo", throwOnError: false, workingDirectory: application.ContextDirectory); if (service.Description.RunInfo is ProjectRunInfo)
if (buildResult.ExitCode != 0)
{ {
_logger.LogInformation("Building projects failed with exit code {ExitCode}: \r\n" + buildResult.StandardOutput, buildResult.ExitCode); var buildResult = await ProcessUtil.RunAsync("dotnet", $"build \"{service.Status.ProjectFilePath}\" /nologo", throwOnError: false, workingDirectory: application.ContextDirectory);
if (buildResult.ExitCode != 0)
{
_logger.LogInformation("Building projects failed with exit code {ExitCode}: \r\n" + buildResult.StandardOutput, buildResult.ExitCode);
}
return buildResult.ExitCode;
} }
return buildResult.ExitCode;
return 0;
} }
}; };
if (_options.Watch && service.Description.RunInfo is ProjectRunInfo runInfo) if (_options.Watch && (service.Description.RunInfo is ProjectRunInfo runInfo))
{ {
var projectFile = runInfo.ProjectFile.FullName; var projectFile = runInfo.ProjectFile.FullName;
var fileSetFactory = new MsBuildFileSetFactory(_logger, var fileSetFactory = new MsBuildFileSetFactory(_logger,
@ -380,6 +385,18 @@ namespace Microsoft.Tye.Hosting
await new DotNetWatcher(_logger) await new DotNetWatcher(_logger)
.WatchAsync(processInfo, fileSetFactory, replica, status.StoppingTokenSource.Token); .WatchAsync(processInfo, fileSetFactory, replica, status.StoppingTokenSource.Token);
} }
else if (_options.Watch && (service.Description.RunInfo is AzureFunctionRunInfo azureFunctionRunInfo) && !string.IsNullOrEmpty(azureFunctionRunInfo.ProjectFile))
{
var projectFile = azureFunctionRunInfo.ProjectFile;
var fileSetFactory = new MsBuildFileSetFactory(_logger,
projectFile,
waitOnError: true,
trace: false);
environment["DOTNET_WATCH"] = "1";
await new DotNetWatcher(_logger)
.WatchAsync(processInfo, fileSetFactory, replica, status.StoppingTokenSource.Token);
}
else else
{ {
await ProcessUtil.RunAsync(processInfo, status.StoppingTokenSource.Token, throwOnError: false); await ProcessUtil.RunAsync(processInfo, status.StoppingTokenSource.Token, throwOnError: false);

5
src/Microsoft.Tye.Hosting/Watch/DotNetWatcher.cs

@ -100,6 +100,11 @@ namespace Microsoft.DotNet.Watcher
{ {
while (true) while (true)
{ {
if (cancellationToken.IsCancellationRequested)
{
break;
}
var exitCode = await processSpec.Build(); var exitCode = await processSpec.Build();
if (exitCode == 0) if (exitCode == 0)
{ {

Loading…
Cancel
Save