当 collection.find 在 mongodb 数据库中发现任务失败时,我需要发送一条松弛消息
I need to send a slack message when a collection.find finds a task in failure in a mongodb database
当查询 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)
当查询 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)