I am using Google PubSub Publisher to trigger a notification and send a message to Subscriber when a document got uploaded in Google Cloud Storage Bucket.
I created a notification topic by -> gsutil notification create -t [TOPIC_NAME] -f json -e OBJECT_FINALIZE gs://[BUCKET_NAME]
My Subscriber Function is : **
def callback(message):
try:
#storage_client = storage.Client.from_service_account_json('storage_service_key.json')
print('Received message: {}'.format(message.data.decode("utf-8")))
data = json.loads(message.data.decode("utf-8"))
filename = data['name']
file_name = re.search(r'/(.*)', filename).group(1)
#filelink = data['selfLink']
print("Processing the file : {}".format(file_name))
path = "sample/"+file_name
download_files(path, file_name)
rming.image_remover(file_name) ## my custom function
message.ack()
os.remove(file_name)
except Exception as error_message:
print("Error in callback method: {}".format(error_message))
pass
flow_control = pubsub_v1.types.FlowControl(max_messages=1)
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(proj_name, sub_name)
streaming_pull_future = subscriber.subscribe(
subscription_path, callback=callback,flow_control=flow_control
)
print('Listening for messages on: {}'.format(subscription_path))
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
#flag=0
streaming_pull_future.result()
except Exception as error_message: # noqa
print("exception occured while handling subscription: {}").format(error_message)
pass
** I want to deploy this Subscriber Function in a docker container. Then it will make the Subscriber run 24/7.
Can I put the subscriber to sleep/(not make a pull request) to Publisher when there are no messages available OR My point in other term is can I make Subscriber sleep at idle times when there are no incoming messages.
Any help is appreciated!!!!