question

JeanPaulTecson-9198 avatar image
0 Votes"
JeanPaulTecson-9198 asked MughundhanRaveendran-MSFT commented

Function app running properly as http trigger but doesn't work as time trigger

I was previously working on an HTTP trigger to pull 1 hour worth of logs from cloudflare but was asked that it had to pull logs every hour and upload it to azure storage.
When I fire the HTTP trigger it uploads the logs properly into the azure storage account but when I use time trigger it doesn't upload anything.

this is what if looks like as a time trigger

import datetime
import logging
import requests
import pytz

import azure.functions as func
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

#update conn_str when storage account connection string changes
conn_str = "My_connection_string"

#establish connection for blob management
Blob_Service_Client = BlobServiceClient.from_connection_string(conn_str)

#declarevariables
date_time = datetime.datetime.now(pytz.timezone('Asia/Manila'))
urltime = datetime.datetime.now()
txt_name = date_time.strftime("%m-%d-%Y")

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    
    urldate = urltime.strftime("%Y-%m-%d")
    en_date = urltime.strftime("%Y-%m")
    en_day = int(urltime.strftime("%d"))
    ehour = int(urltime.strftime("%H"))
    shour = ehour
    en_min = int(urltime.strftime("%M")) - 1
    
    #min var check
    if en_min==-1:
        en_min = "59"
        shour = int(shour)- 1
    elif en_min==1 or en_min==2 or en_min==3 or en_min==4 or en_min==5 or en_min==6 or en_min==7 or en_min==8 or en_min==9:
        en_min = "0" + str(en_min)
        shour = str(int(shour)- 1)

    #hour var check
    if shour == -1:
        ehour = "23"
        shour = ehour
        en_day = str(en_day - 1)

    elif shour==0 or shour==1 or shour==2 or shour==3 or shour==4 or shour==5 or shour==6 or shour==7 or shour==8 or shour==9:
        shour = "0" + str(shour)
        ehour = shour
    else :
        shour = str(shour)
        ehour = shour
    

    #api request
    url = "cloudflareurl"
    fields =  f"start={urldate}T{shour}:00:00Z&end={en_date}-{en_day}T{ehour}:{en_min}:00Z&fields=ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeResponseStatus,RayID,ClientCountry,ClientSSLProtocol,Action,RuleID"
    urlwhole = url + fields
    headersapi = {
                'X-Auth-Email': 'Auth_email',
                'Content-Type': 'application/json',
                'X-Auth-Key':'Authkey',
                'Accept-Encoding': 'gzip'
                 }
    response = requests.get(urlwhole, headers=headersapi)

    #upload blob
    blob_client = Blob_Service_Client.get_blob_client(container="my_container", blob=f"{txt_name}CFlog.txt")
    blob_client.upload_blob(response.text)
    


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

    logging.info('Python timer trigger function ran at %s', utc_timestamp)


This as HTTP trigger

import datetime
import logging
import requests
import pytz

import azure.functions as func
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

#update conn_str when storage account connection string changes
conn_str = "My_connection_string"

#establish connection for blob management
Blob_Service_Client = BlobServiceClient.from_connection_string(conn_str)

#declarevariables
date_time = datetime.datetime.now(pytz.timezone('Asia/Manila'))
urltime = datetime.datetime.now()
txt_name = date_time.strftime("%m-%d-%Y")

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')    
    
    urldate = urltime.strftime("%Y-%m-%d")
    en_date = urltime.strftime("%Y-%m")
    en_day = int(urltime.strftime("%d"))
    ehour = int(urltime.strftime("%H"))
    shour = ehour
    en_min = int(urltime.strftime("%M")) - 1
    
    #min var check
    if en_min==-1:
        en_min = "59"
        shour = int(shour)- 1
    elif en_min==1 or en_min==2 or en_min==3 or en_min==4 or en_min==5 or en_min==6 or en_min==7 or en_min==8 or en_min==9:
        en_min = "0" + str(en_min)
        shour = str(int(shour)- 1)

    #hour var check
    if shour == -1:
        ehour = "23"
        shour = ehour
        en_day = str(en_day - 1)

    elif shour==0 or shour==1 or shour==2 or shour==3 or shour==4 or shour==5 or shour==6 or shour==7 or shour==8 or shour==9:
        shour = "0" + str(shour)
        ehour = shour
    else :
        shour = str(shour)
        ehour = shour
    

    #api request
    url = "cloudflareurl"
    fields =  f"start={urldate}T{shour}:00:00Z&end={en_date}-{en_day}T{ehour}:{en_min}:00Z&fields=ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeResponseStatus,RayID,ClientCountry,ClientSSLProtocol,Action,RuleID"
    urlwhole = url + fields
    headersapi = {
                'X-Auth-Email': 'Auth_email',
                'Content-Type': 'application/json',
                'X-Auth-Key':'Authkey',
                'Accept-Encoding': 'gzip'
                 }
    response = requests.get(urlwhole, headers=headersapi)

    #upload blob
    blob_client = Blob_Service_Client.get_blob_client(container="my_container", blob=f"{txt_name}CFlog.txt")
    blob_client.upload_blob(response.text)
    


    #successful trigger msg
    return func.HttpResponse(
        "This HTTP triggered function executed successfully.",
        status_code=200
        )


Both of them has this host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}


The time trigger also gives me an error on log stream

2022-04-18T07:10:00Z   [Information]   Executing 'Functions.TimerTrigger1' (Reason='Timer fired at 2022-04-18T07:09:59.9998887+00:00', Id=2c2150ae-b9b8-4b62-902e-b1b35afef496)
2022-04-18T07:10:00Z   [Verbose]   Sending invocation id:2c2150ae-b9b8-4b62-902e-b1b35afef496
2022-04-18T07:10:00Z   [Verbose]   Posting invocation id:2c2150ae-b9b8-4b62-902e-b1b35afef496 on workerId:d2cd72d4-9fdd-4ed5-915c-6db8fd38d886
2022-04-18T07:10:00Z   [Error]   Executed 'Functions.TimerTrigger1' (Failed, Id=2c2150ae-b9b8-4b62-902e-b1b35afef496, Duration=2ms)
2022-04-18T07:10:00Z   [Verbose]   Function 'TimerTrigger1' updated status: Last='2022-04-18T07:10:00.0000000+00:00', Next='2022-04-18T07:15:00.0000000+00:00', LastUpdated='2022-04-18T07:10:00.0000000+00:00'
2022-04-18T07:10:00Z   [Verbose]   Timer for 'TimerTrigger1' started with interval '00:04:59.8997499'.
2022-04-18T07:10:07Z   [Verbose]   [HostMonitor] Checking worker statuses (Count=1)
2022-04-18T07:10:07Z   [Verbose]   [HostMonitor] Worker status: ID=d2cd72d4-9fdd-4ed5-915c-6db8fd38d886, Latency=3ms
2022-04-18T07:10:07Z   [Verbose]   [HostMonitor] Host process CPU stats (PID 59): History=(0,1,1,1,0), AvgCpuLoad=1, MaxCpuLoad=1
2022-04-18T07:10:07Z   [Verbose]   [HostMonitor] Host aggregate CPU load 1
2022-04-18T07:10:07Z   [Information]   Executing StatusCodeResult, setting HTTP status code 200
azure-functions
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MughundhanRaveendran-MSFT avatar image
0 Votes"
MughundhanRaveendran-MSFT answered MughundhanRaveendran-MSFT commented

@JeanPaulTecson-9198 ,

Thanks for reaching out Q&A.

From the logs, I can see that the timer trigger function was invoked as per the cron schedule. However, it completed its execution with a failure. I would suggest you to add try catch block in your code so that the exception/error is handled and we can get to know what is causing the execution to fail.

Additionally, try to debug the code in your local environment to check if it is working without any issues. Feel free to reach out to me if you have any questions or concerns.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JeanPaulTecson-9198 ,

Following up to see if the above answer helps. Do let me know if you have any queries.

Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·