简体   繁体   English

自定义容器中的 Azure Function 和 TimerTrigger:在 function.D6D466DEEC76ECDF25 文件中还需要什么?

[英]Azure Function & TimerTrigger in a Custom Container: what else do I need in the function.json file?

I have a custom container with a time-triggered Azure Function.我有一个带有时间触发的 Azure Function 的自定义容器。 When I build the Docker image, and run it locally, I don't get any action, ie the trigger doesn't fire.当我构建 Docker 映像并在本地运行它时,我没有得到任何操作,即触发器没有触发。

I'm wondering - is my function.json missing something?我想知道 - 我的function.json缺少什么吗? Do I need an output within that, and/or in __init__.py ?我是否需要在其中和/或在__init__.py中使用 output ? I'm starting to think that the timer trigger alone isn't enough to elicit any kind of response.我开始认为仅计时器触发器不足以引起任何类型的响应。

The function.json file: function.json文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 * * * * *",
      "authLevel": "anonymous"
    }
  ]
}

The __init__.py imports some custom functions, which work, but need the custom container for selenium concerns. __init__.py导入了一些自定义函数,它们可以工作,但需要自定义容器来解决 selenium 问题。 The function scrapes and then outputs to Twitter. function 抓取然后输出到 Twitter。 But is there a need for an output, like the answer in this question ?但是是否需要 output,就像这个问题的答案一样? If this Function needs to output to Twitter (the internet), is the timer trigger enough?如果这个 Function 需要 output 到 Twitter (互联网),定时器触发是否足够?

import logging
import azure.functions as func
#importing custom modules, here, they work

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    
    # Class instantiation for the scraping, etc. ----
    #calling other functions

Some of the logs from the running container, though I don't think it's a string connection issue, as I have that defined in the local.settings.json file, or I use the storage emulator in VS Code, and that works too.来自正在运行的容器的一些日志,虽然我不认为这是一个字符串连接问题,因为我在local.settings.json文件中定义了它,或者我在 VS Code 中使用了存储模拟器,这也可以。

      The listener for function 'Functions.BadFunc' was unable to start.
Microsoft.Azure.WebJobs.Host.Listeners.FunctionListenerException: The listener for function 'Functions.BadFunc' was unable to start.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
   at Microsoft.Azure.Storage.CloudStorageAccount.Parse(String connectionString)
   at Microsoft.Azure.WebJobs.Extensions.Timers.StorageScheduleMonitor.get_TimerStatusDirectory() in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions\Extensions\Timers\Scheduling\StorageScheduleMonitor.cs:line 77
   at Microsoft.Azure.WebJobs.Extensions.Timers.StorageScheduleMonitor.GetStatusBlobReference(String timerName) in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions\Extensions\Timers\Scheduling\StorageScheduleMonitor.cs:line 144
   at Microsoft.Azure.WebJobs.Extensions.Timers.StorageScheduleMonitor.GetStatusAsync(String timerName)
   at Microsoft.Azure.WebJobs.Extensions.Timers.Listeners.TimerListener.StartAsync(CancellationToken cancellationToken) in C:\azure-webjobs-sdk-extensions\src\WebJobs.Extensions\Extensions\Timers\Listener\TimerListener.cs:line 99
   at Microsoft.Azure.WebJobs.Host.Listeners.SingletonListener.StartAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Singleton\SingletonListener.cs:line 72
   at Microsoft.Azure.WebJobs.Host.Listeners.FunctionListener.StartAsync(CancellationToken cancellationToken, Boolean allowRetry) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Listeners\FunctionListener.cs:line 69
   --- End of inner exception stack trace ---
info: Host.Startup[413]
      Host started (154ms)
info: Host.Startup[0]
      Job host started
Hosting environment: Production
Content root path: /
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
info: Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcFunctionInvocationDispatcher[0]
      Worker process started and initialized.
info: Host.Startup[0]
      Retrying to start listener for function 'Functions.BadFunc' (Attempt 1)
info: Host.Startup[0]
      Listener successfully started for function 'Functions.BadFunc' after 1 retries.
info: Host.General[316]
      Host lock lease acquired by instance ID '000000000000000000000000F28FAECC'.

It looks like your function is no problem.看起来你的 function 没问题。 So maybe problems come from the azure storage emulator?那么问题可能来自 azure 存储仿真器?

Try to change the 'local.settings.json' file just like this:尝试像这样更改“local.settings.json”文件:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  }
}

The value of AzureWebJobsStorage needs you to create a storage on azure and then get the connection string of it: AzureWebJobsStorage 的值需要你在 azure 上创建一个存储,然后获取它的连接字符串:

在此处输入图像描述

You can do a try.你可以试一试。

Have you tried this ?你试过这个吗?

Add an ENV statement (where you define your connection string) to your Dockerfile:ENV语句(在其中定义连接字符串)添加到 Dockerfile:

ENV AzureWebJobsStorage=DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx==;EndpointSuffix=core.windows.net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM