当collection.find在mongodb数据库中发现任务失败时,我需要发送一条松弛消息



当查询collection.find()在数据库中发现task_status失败时,我需要能够在MongoDB中向slack发送带有集合名称的消息。到目前为止,这就是我所拥有的,它是有效的,但我需要消息来说明集合的名称,因为这段代码应该在有很多集合的数据库中使用,有人能帮我吗?

import pymongo
import requests
import json
from pymongo import MongoClient
def slackmessage ():
wekbook_url = 'https://hooks.slack.com/*****'
slack_data = {
'text': "There is problem in your mongodb collection, task is in FAILURE.",
'username': 'MongodbAlert',
'icon_emoji': ':fire:'
}
response = requests.post(wekbook_url, data = json.dumps(slack_data), headers={'Content-Type': 'application/json'})
def mongodbfind ():
cluster = MongoClient("mongodb+srv://*****")
db = cluster["nameofthemongodb"]
collection = db["nameofthecollection"]
mysearch = collection.find({ "task_status":"FAILURE"})
for x in mysearch:
print (m + str(x))
return True

if mongocheck() == True:
slackmessage()

在slackmessage((函数中传递集合名称,并将其与slack_data:中的文本连接

def slackmessage (collectionName):
wekbook_url = 'https://hooks.slack.com/*****'
slack_data = {
'text': "There is problem in your mongodb collection - "+collectionName+", task is in FAILURE.",
'username': 'MongodbAlert',
'icon_emoji': ':fire:'
}
response = requests.post(wekbook_url, data = json.dumps(slack_data), headers={'Content-Type': 'application/json'})
# call the function with connectionname as argument
slackmessage(collectionName)

相关内容

最新更新