Search code examples
azuretriggerstimerazure-functions

Azure Function not called by timer


I have published an Azure Function. It's trigger is an timer.Timer is correct.Function is recorded. But it does not get invoked. What can be the problem?

What is checked:

  • Timezone is set.
  • Timer value is correct.
  • Restarted after publishing.
  • Triggers are sync.

Integration: enter image description here

My function code :

    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 15 21 * * *")]TimerInfo myTimer, ILogger log)
    {
        //log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        UpdateBuyData();
        UpdateSellData();

    }
    public static float GetDolarData(DateTime date, bool sell)
    {

        string url = "https://evds2.tcmb.gov.tr/service/evds/series=TP.DK.USD." + (sell ? "S" : "A") + "&startDate=" + date.ToString("dd-MM-yyyy") + "&endDate=" + date.ToString("dd-MM-yyyy") + "&type=xml&key=" + ApiKey;
        WebRequest webRequest = WebRequest.Create(url);
        webRequest.Method = "GET";
        webRequest.ContentType = "application/xml;charset=UTF-8";


        WebResponse response = webRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
        XmlReader reader = XmlReader.Create(sr);
        reader.ReadToFollowing("document");
        reader.ReadToFollowing("items");
        reader.ReadToFollowing("TP_DK_USD_" + (sell ? "S" : "A"));

        string dolarstr = reader.ReadElementContentAsString();
        dolarstr = dolarstr.Replace(",", ".");
        float dolar = float.Parse(dolarstr);
        return dolar;

    }

    public static void UpdateSellData()
    {
        float dolar_try = GetDolarData(DateTime.Today.AddDays(-1), true);

        using (var conn = SqlClientFactory.Instance.CreateConnection())
        {
            conn.ConnectionString = "Server=tcp:dolardatabaseserver.database.windows.net,1433;Initial Catalog=DolarDatabase;Persist Security Info=False;User ID=calculatoradmin;Password=Onur15975312*-;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
            conn.Open();

            using (var command = conn.CreateCommand())
            {
                command.CommandText = "INSERT INTO DolarTRYS (Tarih, TP_DK_USD_S, UNIXTIME)VALUES (@Date, @DolarTRY, null)";
                // Use parameters to prevent SQL injection
                command.Parameters.Add(new SqlParameter("@date", DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy")));
                command.Parameters.Add(new SqlParameter("@DolarTRY", dolar_try));
                command.ExecuteNonQuery();
            }
        }

    }
    public static void UpdateBuyData()
    {
        float dolar_try = GetDolarData(DateTime.Today.AddDays(-1), false);

        using (var conn = SqlClientFactory.Instance.CreateConnection())
        {
            conn.ConnectionString = "Server=tcp:dolardatabaseserver.database.windows.net,1433;Initial Catalog=DolarDatabase;Persist Security Info=False;User ID=calculatoradmin;Password=Onur15975312*-;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
            conn.Open();

            using (var command = conn.CreateCommand())
            {
                command.CommandText = "INSERT INTO DolarTRYA (Tarih, TP_DK_USD_A, UNIXTIME)VALUES (@Date, @DolarTRY, null)";
                // Use parameters to prevent SQL injection
                command.Parameters.Add(new SqlParameter("@date", DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy")));
                command.Parameters.Add(new SqlParameter("@DolarTRY", dolar_try));
                command.ExecuteNonQuery();
            }
        }

    }

Solution

  • Might be because you're writing the CRON expression in your local time zone but the function in Azure is executing at the UTC equivalent.