问题
I am new to Google Compute/Google App Engine platform. I am currently migrating a python flask application using celery for async tasks to Google Compute/Google App Engine platform. However in the docs it's written I should use Google Pub/Sub instead of celery. In my application whenever I run an async task I have a page to monitor the status of the job using the same principle as http://blog.miguelgrinberg.com/post/using-celery-with-flask. I have checked the documents for google pub/sub, but I am at loss how to implement the same using google pub/sub. Can anybody help or point me to the right direction to implement the same in google pub/sub.
回答1:
You might be able to use psq for this, which is designed to look like celery. From a general Cloud Pub/Sub perspective, you would follow these steps:
- Create a topic for your status update messages.
- In the async task whose status you want to monitor, periodically publish a message with the status. This message will be of some format of your choosing that would indicate percentage completion or specific message to display.
- Create a subscription for your monitoring page that will receive messages on the topic.
- In your monitoring page (or a background process that will supply the data to your monitoring page), pull messages for the subscription.
- Process the messages and update the state of your jobs for your monitoring page.
- Ack the messages you pulled and processed.
A couple of things to keep in mind in this workflow:
- Cloud Pub/Sub guarantees at-least-once delivery. That means you could potentially receive the same message more than once.
- Cloud Pub/Sub does not provide any guarantees on ordering. Therefore, if you are periodically publishing status updates, your subscriber could potentially receive these out of order. For your case, you'll probably want your message to include some sort of timestamp or strictly-increasing identifier in your message to sequence your status updates per task. If you keep track of the most recent status update received, then you can disregard older messages and ack them immediately.
来源:https://stackoverflow.com/questions/37070442/monitoring-the-status-of-a-google-pub-sub-submitted-job