2

I have a queue trigger which when message is manually added into queue it gets started and runs as expected. However, when message is written into queue by the following timer trigger function it fails to start. I can see the message is successfully written by the trigger.

** init.py**

import datetime
import logging
import os, uuid
from azure.storage.queue import (
        QueueClient,
        BinaryBase64EncodePolicy,
        BinaryBase64DecodePolicy
)

import os, uuid
import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    conn_str = os.environ['AzureWebJobsStorage']

    queue_name="outqueue12"
    
    message = u"Hello234"
    queue_client = QueueClient.from_connection_string(conn_str, queue_name)
    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()
    
    queue_client.send_message(message)
    
           
    logging.info('Python timer trigger function ran at %s', utc_timestamp)

host.json

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

Is there something I am missing out?

wwnde
  • 26,119
  • 6
  • 18
  • 32

2 Answers2

2

According to some test, the problem is related to base64 encode. I added three lines of code:

message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')

Please refer to the whole function code below: enter image description here

host.json

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

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "*/20 * * * * *"
    }
  ]
}
Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • did this pull the queue trigger it on your end when you tested? It indicates it has run successfully.However unlike before there is no message in the queue or posion queue. Queue trigger does not get started – wwnde Jan 08 '21 at 08:54
  • @wwnde My queue trigger was triggered success after the timer trigger function send message to queue. – Hury Shen Jan 08 '21 at 08:55
  • Could you please add your host and function.json in the answer? – wwnde Jan 08 '21 at 08:56
  • @wwnde How did you know the queue trigger does not get started ? If you check the log of queue trigger, you need to know the log of function may delay or missing sometimes. – Hury Shen Jan 08 '21 at 09:00
  • @wwnde And could you please delete the queue `outqueue12-posion` first and then run the timer trigger. If the message be sent to queue success and trigger the queue trigger function success, it will not generate a queue named `outqueue12-posion`. – Hury Shen Jan 08 '21 at 09:01
  • I know the function didnt run because it chained to another which downloads arcgis service feature service and writes back to portal. When I manually write, this happens instantly. – wwnde Jan 08 '21 at 09:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227021/discussion-between-hury-shen-and-wwnde). – Hury Shen Jan 08 '21 at 09:03
  • are you able to help https://stackoverflow.com/questions/65677939/how-can-i-create-a-python-webhook-sender-app – wwnde Jan 12 '21 at 04:13
  • @ Hury Shen I need to modify this code `if (dbItem != null) { if (!string.IsNullOrEmpty(dbItem.BinMeasurements)) to include ccc='56'` in the if statement. Not a `c#` person so in problems. Happy to post question – wwnde Jan 22 '21 at 05:47
  • Hi @wwnde I'm not so clear about your description above, so could you please post a question and provide more details. – Hury Shen Jan 22 '21 at 05:51
  • @ Hury Shen, I have no issues posting questions. I simply want to expand `if (!string.IsNullOrEmpty(dbItem.BinMeasurements))' to make it moreless read `BinMeasurement is not null and ccc='56'`. In python would simply be say `if BinMeasurements.notna() &cc.eq('56')` – wwnde Jan 22 '21 at 05:56
  • @wwnde Is this: `if (!string.IsNullOrEmpty(dbItem.BinMeasurements) && cc.Equals("56"))` what you want ? – Hury Shen Jan 22 '21 at 06:05
  • Tried that but in the format `if (!string.IsNullOrEmpty(dbItem.BinMeasurements) && cc==("56"))` and it failed – wwnde Jan 22 '21 at 06:06
  • Ho do I write NotEqual – wwnde Jan 22 '21 at 06:11
  • @wwnde You can use `!cc.Equals("56")` or `cc != "56"` to implement not equal. – Hury Shen Jan 22 '21 at 06:13
  • Ok, let me try. Under pump. Once finished post question. Let you know before I do – wwnde Jan 22 '21 at 06:17
  • @wwnde Please do not post a new question for it. This problem is too simple that it may be closed(or down vote) by other communities. – Hury Shen Jan 22 '21 at 06:19
  • 1
    Ok, not simple for me but I appreciate your help – wwnde Jan 22 '21 at 06:20
  • I am trying out Azure Purview and for some reasons I cant gain access to on prem sql, I can access sql in ADF though. Able to help? Happy to post question – wwnde Feb 23 '21 at 05:49
  • Hi @wwnde I'm sorry that I don't know much about it. – Hury Shen Feb 23 '21 at 05:53
  • I have a url to a binary gis object. Need to write it to blob. Are you able to help? Happy to post question – wwnde Jun 22 '21 at 01:42
  • 1
    @wwnde I'm not sure, you can post the question first. If I can't help, maybe other communities can help you. – Hury Shen Jun 22 '21 at 01:45
  • https://gis.stackexchange.com/questions/402106/zip-filegeodatabase-online-using-python-and-write-file-to-blobstorage – wwnde Jun 22 '21 at 02:47
  • I get this error, `Exception: TypeError: unable to encode outgoing TypedData: unsupported type "" for Python type "Item" how can I set it as a binary file?` – wwnde Jun 22 '21 at 02:57
0

If you don't want to use base64 encoding (I personally won't recommend that), then there is no point in setting up the policies, just remove following lines and your original code should work:

    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()

See example from docs.

Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • that approach was attempted. The message was being written into the queue. However, the queue trigger was not firing as a result of the queue being populated. – wwnde Jan 09 '21 at 02:57