使用适用于python的azure函数的Timer触发器来触发HttpTrigger函数

时间:2019-12-05 12:12:49

标签: python python-3.x azure-functions azure-functions-runtime azure-function-app

我想每天使用Timer触发器在9.00 PM触发HttpTrigger函数。可以在Azure函数中使用python吗?

我无法理解该如何实施?有什么办法可以实现上述情况?

请检查以下代码。

HttpTrigger功能代码

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')
            logging.info(name)

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

TimerTrigger功能代码:-

import datetime
import logging
import requests

import azure.functions as func


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)

    url = '<My URL>'
    PARAMS = {"name": "Akshay"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    print(response_decoded_json.text)
    logging.info("Result is :- ")
    logging.info(response_decoded_json.text)

请检查以下错误日志图像:-

enter image description here

1 个答案:

答案 0 :(得分:0)

这当然是可用的,只是在您的计时器触发功能中执行获取/发布请求。下面是我的测试代码。

import datetime
import logging
import requests

import azure.functions as func

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)
    url = 'your function url'
    PARAMS = {"name": "test"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    print(response_decoded_json.text)

enter image description here

我从http触发函数得到了响应,根据您的要求,将cron表达式更改为0 0 21 * * *

更新:我测试是否可用。

enter image description here

计时器功能代码:

import datetime
import logging
import requests

import azure.functions as func


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)
    url = 'https://functionname.azurewebsites.net/api/httptest?code=master key'
    PARAMS = {"name": "test"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    logging.info(response_decoded_json.text)

HTTP功能代码:

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

上传函数时,请记住在requests==2.22.0中添加requirements.txt

假设主要问题与如何获取httptrigger函数url有关。所需的主要参数url是功能APP名称,功能名称和主键(或默认键)。实际上您已经知道功能APP名称和功能名称。

关于代码(密钥),您可以先部署任何功能,然后转到管理页面,您将能够获取代码并粘贴到url之后,您将获得完整的http触发器url。然后上传两个功能,就可以了。

enter image description here