Skip to content

Commit b2c8c7b

Browse files
authored
Merge pull request #68 from mystster/fixNotResponse
fix not response azure function
2 parents 2a3b511 + d515f26 commit b2c8c7b

File tree

1 file changed

+70
-35
lines changed

1 file changed

+70
-35
lines changed

Azure/Functions/WeatherDisp/Function1.cs

+70-35
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace WeatherDisp
1717
{
1818
public class WeatherInfo : HttpFunctionBase
1919
{
20-
static int execCounter = 0;
20+
static int execCounter;
2121
public WeatherInfo(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
2222
{
2323
}
@@ -35,10 +35,10 @@ public async Task<IActionResult> Run(
3535
return BadRequest();
3636
}
3737

38-
int maxCount = 0;
39-
if(int.TryParse(Environment.GetEnvironmentVariable("MAX_EXEC", EnvironmentVariableTarget.Process), out maxCount)
40-
&& maxCount > 0
41-
&& execCounter <= maxCount){
38+
if (int.TryParse(Environment.GetEnvironmentVariable("MAX_EXEC", EnvironmentVariableTarget.Process), out int maxCount)
39+
&& maxCount > 0
40+
&& execCounter <= maxCount)
41+
{
4242
model.DarkskyKey = Environment.GetEnvironmentVariable("DARKSKY_API", EnvironmentVariableTarget.Process) ?? model.DarkskyKey;
4343
model.Lag = Environment.GetEnvironmentVariable("LAG", EnvironmentVariableTarget.Process) ?? model.Lag;
4444
model.Lat = Environment.GetEnvironmentVariable("LAT", EnvironmentVariableTarget.Process) ?? model.Lat;
@@ -50,51 +50,86 @@ public async Task<IActionResult> Run(
5050
}
5151

5252
log.LogInformation($"Lag:{model.Lag}, Lat:{model.Lat}");
53-
54-
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
53+
54+
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
5555
{
5656
Headless = true,
5757
LogProcess = true,
5858
Args = new[]
5959
{
6060
"--no-sandbox",
6161
"--disable-web-security"
62+
},
63+
DumpIO = true
64+
});
65+
using var page = await browser.NewPageAsync();
66+
page.Console += (target, e) => {
67+
switch (e.Message.Type)
68+
{
69+
case ConsoleType.Error:
70+
log.LogError($"ConsoleError:{e.Message.Text}");
71+
break;
72+
case ConsoleType.Warning:
73+
log.LogWarning($"ConsoleWarning:{e.Message.Text}");
74+
break;
75+
default:
76+
log.LogInformation($"Console{e.Message.Type}:{e.Message.Text}");
77+
break;
6278
}
63-
}))
79+
};
80+
page.Error += (target, e) =>
81+
{
82+
log.LogError($"Error:{e.Error}");
83+
};
84+
page.PageError += (target, e) =>
85+
{
86+
log.LogError($"PageError:{e.Message}");
87+
};
88+
page.Response += (target, e) =>
89+
{
90+
log.LogInformation($"Response:{e.Response.Status}:{e.Response.StatusText}");
91+
};
92+
page.RequestFailed += (target, e) =>
93+
{
94+
log.LogError($"RequestFailed:{e.Request.Failure}");
95+
};
96+
await page.SetViewportAsync(new ViewPortOptions()
97+
{
98+
Width = 298,
99+
Height = 128
100+
});
101+
102+
using (var fs = new StreamReader(Path.Combine(context.FunctionAppDirectory, Path.Combine("dist", "index.html"))))
64103
{
65-
using var page = await browser.NewPageAsync();
66-
await page.SetViewportAsync(new ViewPortOptions()
104+
var html = (await fs.ReadToEndAsync())
105+
.Replace("DARKSKY_KEY_PLACEHOLDER", model.DarkskyKey, StringComparison.OrdinalIgnoreCase)
106+
.Replace("LAT_PLACEHOLDER", model.Lat, StringComparison.OrdinalIgnoreCase)
107+
.Replace("LAG_PLACEHOLDER", model.Lag, StringComparison.OrdinalIgnoreCase);
108+
await page.SetContentAsync(html, new NavigationOptions()
67109
{
68-
Width = 298,
69-
Height = 128
110+
WaitUntil = new[] { WaitUntilNavigation.Networkidle0 }
70111
});
112+
}
71113

72-
using (var fs = new StreamReader(Path.Combine(context.FunctionAppDirectory, Path.Combine("dist", "index.html"))))
73-
{
74-
var html = (await fs.ReadToEndAsync())
75-
.Replace("DARKSKY_KEY_PLACEHOLDER", model.DarkskyKey, StringComparison.OrdinalIgnoreCase)
76-
.Replace("LAT_PLACEHOLDER", model.Lat, StringComparison.OrdinalIgnoreCase)
77-
.Replace("LAG_PLACEHOLDER", model.Lag, StringComparison.OrdinalIgnoreCase);
78-
await page.SetContentAsync(html, new NavigationOptions()
114+
using var im = new MagickImage(await page.ScreenshotDataAsync(
115+
new ScreenshotOptions()
79116
{
80-
WaitUntil = new[] { WaitUntilNavigation.Networkidle0 }
81-
});
82-
}
83-
84-
using var im = new MagickImage(await page.ScreenshotDataAsync(
85-
new ScreenshotOptions()
86-
{
87-
Type = ScreenshotType.Png
88-
}));
89-
im.Map(new[] {
90-
new MagickColor(0, 0, 0),
91-
new MagickColor(255, 255, 255)
92-
}, new QuantizeSettings() {
117+
Type = ScreenshotType.Png
118+
}));
119+
im.Map(new[] {
120+
new MagickColor(0, 0, 0),
121+
new MagickColor(255, 255, 255)
122+
}, new QuantizeSettings()
123+
{
93124
DitherMethod = DitherMethod.No
94125
});
95-
im.Quality = 100;
96-
return File(im.ToByteArray(MagickFormat.Jpg), "image/jpeg");
97-
}
126+
im.Quality = 100;
127+
return File(im.ToByteArray(MagickFormat.Jpg), "image/jpeg");
128+
}
129+
130+
private void Page_Console(object sender, ConsoleEventArgs e)
131+
{
132+
throw new NotImplementedException();
98133
}
99134
}
100135

0 commit comments

Comments
 (0)