c# - Azure Function App: TimerTrigger not Firing when Deployed
问题描述
I have an Azure Function App (v2) that is run through a TimerTrigger (every day at midnight - EST). It works fine when I run it locally in Visual Studio. However, when I use the built in Visual Studio Publishing Tool to publish it to my Function App directly (Zip Deploy), it doesn't trigger at midnight nor any time for that matter.
I have also tried to run the function directly through the Azure portal and that doesn't work either. Here is my code. All it is doing is pulling a file from Azure Storage, updating the ranked game JSON, and then replacing the file back in Azure Storage. I have a mobile app that pulls the ranked_settings.json
and generates the ranked game.
[FunctionName("RankedSettingsIntervalFunction")]
public static void Run([TimerTrigger("0 0 0 */1 * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
Console.WriteLine("App started running at " + DateTimeOffset.UtcNow.ToString());
const string fileName = "ranked_settings.json";
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var conn1 = config["FileStorage:ConnectionStringOne"];
var conn2 = config["FileStorage:ConnectionStringTwo"];
StorageService service = new StorageService(conn1, conn2);
Settings settings = RankedSettingsGenerator.Generate();
var json = JsonConvert.SerializeObject(settings);
Console.WriteLine("App original JSON created at " + DateTimeOffset.UtcNow.ToString());
Console.WriteLine(json);
byte[] byteArray = Encoding.ASCII.GetBytes(json);
MemoryStream stream = new MemoryStream(byteArray);
service.AddFile(fileName, stream);
Console.WriteLine("App finished running at " + DateTimeOffset.UtcNow.ToString());
}
My function.json file has the following bindings:
"bindings": [
{
"type": "timerTrigger",
"schedule": "0 0 0 */1 * *",
"useMonitor": true,
"runOnStartup": false,
"name": "myTimer"
}
],
I was thinking the issue was that the runOnStartup
property is false, but that doesn't explain why it doesn't work when I press the Run
button in the portal.
解决方案
Update:
For your situation, please note that there is not a local.settings.json
file on Azure. The local.settings.json
will be publish to Azure Application Settings as usual.
The Application Settings is here:
And the code that you use to get settings should be like this:
var config = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
string conn1 = config["xxxxxxxxxxxxx"];
string conn2 = config["xxxxxxxxxxxxx"];
Or simply
Environment.GetEnvironmentVariable("xxxxxxxxxxxxx");
In addition,
I notice you are using Console.WriteLine()
in your code. Please note that the index of Console.WriteLine()
will not show on the portal. You need to use log.LogInformation()
to show the information.
Original Answer:
For Azure Function, many things are possible to run locally. But there are a lot of things to watch out for when deploying to Azure.
Do you use the Consumption plan? Or you don't set the time zone? These two reason will both cause this problem.
Have a look of this Offcial doc.
推荐阅读
- java - 锁定地图活动
- android - 使用 ListView 在下一个活动中显示数据库详细信息
- python - 将 numpy 函数应用于对整个值进行操作的 pandas.Series 对象
- mysql - 将整数从 Arduino 发送到 PHP 服务器和 JSON 的最佳方式
- unity3d - Unity Editor GUI,更改EditorGUILayout.Toggle的标签宽度
- mysql - Rails5:为什么迁移会破坏我的生产环境,但不会破坏我的测试环境?
- macos - NSOpenGLView 在 macOS 10.9 上放弃了它的 NSOpenGLContext
- python - ValueError:无法将输入数组从形状 (3) 广播到形状 (2)
- c# - 多次创建同一个表单
- ftp - 使用 sftp 删除大目录