如何在Google应用引擎中自动收到有关错误的电子邮件提醒 [英] How to automatically get email alerts on errors in Google app engine

查看:122
本文介绍了如何在Google应用引擎中自动收到有关错误的电子邮件提醒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保留了很多Google App Engine应用,如果运行顺利,那么很难持续跟踪所有的服务器。



尴尬只有在用户抱怨系统崩溃后才醒来。



有可能不断更新有关我的服务器运行状况吗?



如果我们的Google应用引擎应用程序出现错误,我可以收到邮件吗?



不能等待Google接受这个要求

解决方案

您可以安排一个可以为您执行此操作的cron工作

首先在cron.yaml中创建一个条目

   - 描述:休斯顿我们有一个问题Finder 
url:/ errorfinder /
时间表:每3个小时
时区:亚洲/加尔各答
/ pre>

然后在 app.yaml

中创建一个条目

 处理程序:
- url:/ errorfinder /
脚本:errorfinder.app
安全:总是

现在保持 errorfinder.py 与以下内容

  import base64 
import datetime
import logging
import time
import urllib
import webapp2
from google.appengine 。 api.logservice import logservice
from google.appengine.api import mail

class MainHandler(webapp2.RequestHandler):
def get(self):
#设置结束时间为我们的查询。
end_time = time.time()
start_time = end_time - 10800#3小时前。与cronjob间隔相同
html =''
report_needed = False
#迭代所有的RequestLog对象,显示一些字段和
#遍历所有AppLogs beloging到每个RequestLog计数次数
在logservice.fetch中的req_log(start_time = start_time,end_time = end_time,minimum_log_level = logservice.LOG_LEVEL_WARNING,include_app_logs = True):
report_needed = True
html = html +'& > REQUEST LOG< br />'
html = html +'IP:%s< br />方法:%s< br />资源:%s< br />'%(req_log.ip,req_log.method,req_log.resource)
html = html +'日期:%s< br />'%datetime.datetime.fromtimestamp (req_log.end_time).strftime('%D%T UTC')

在req_log.app_logs中的app_log:
html = html +'< br />& emsp; APP LOG< br />'
html = html +'& emsp;日期:%s< br />'%datetime.datetime.fromtimestamp(app_log.time).strftime('%D%T UTC')
html = html +'& emsp;消息:< b>%s< / b>< br />'%app_log.message
html = html +'< br />< br />< br /> < br />'
if(report_needed):
mail.send_mail(sender =Beagle Bot< bot@urdomain.com>,
to ='lazyadmins @ urdomain。 com',
subject ='休斯顿我们有一个问题..',
body = html,
html = html,
reply_to ='support@urdomain.com')
self.response.out.write(html)

app = webapp2.WSGIApplication([('/ errorfinder /',MainHandler)],debug = True)

您可以通过减少cron作业间隔来实时接近错误搜索


I maintain a lot of Google App Engine apps and it is very difficult to constantly keep a track of all my servers if they are running smoothly.

It is very embarrassing to wake up only after user complains about a system crash.

Is it possible to constantly get updated about my server health?

Can i get a mail or something in case their is a error on my google app engine application?

Can't wait for Google to take up this requirement

解决方案

You can schedule a cron job which can do this for you

First create a entry in cron.yaml

- description: Houston we have a problem Finder
  url: /errorfinder/
  schedule: every 3 hours
  timezone: Asia/Kolkata

Then create an entry in app.yaml

handlers:
- url: /errorfinder/
  script: errorfinder.app
  secure: always 

Now keep errorfinder.py with the following content

import base64
import datetime
import logging
import time
import urllib
import webapp2
from google.appengine.api.logservice import logservice
from google.appengine.api import mail

class MainHandler(webapp2.RequestHandler):
    def get(self):    
      # Set up end time for our query.
      end_time = time.time()
      start_time = end_time  - 10800 # 3 hours before now . same as cronjob interval
      html = ''
      report_needed = False
      # Iterate through all the RequestLog objects, displaying some fields and
      # iterate through all AppLogs beloging to each RequestLog count times
      for req_log in logservice.fetch(start_time=start_time, end_time=end_time, minimum_log_level=logservice.LOG_LEVEL_WARNING, include_app_logs=True):
            report_needed = True
            html = html + '<br /> REQUEST LOG <br />'
            html = html + 'IP: %s <br /> Method: %s <br /> Resource: %s <br />' % (req_log.ip, req_log.method, req_log.resource)
            html = html + 'Date: %s<br />' % datetime.datetime.fromtimestamp(req_log.end_time).strftime('%D %T UTC')

            for app_log in req_log.app_logs:
                html = html + '<br />&emsp; APP LOG<br />'
                html = html + '&emsp; Date: %s<br />' % datetime.datetime.fromtimestamp(app_log.time).strftime('%D %T UTC')
                html = html + '&emsp; Message: <b>%s</b><br />' % app_log.message
                html = html + '<br /><br /><br /><br />'
      if(report_needed):
           mail.send_mail(sender="Beagle Bot <bot@urdomain.com>",
                to='lazyadmins@urdomain.com',
                subject='Houston we have a problem ..',
                body=html,
                html=html,
                reply_to='support@urdomain.com')
      self.response.out.write(html)

app = webapp2.WSGIApplication([('/errorfinder/', MainHandler)], debug=True)

You can make the error search close to real time by reducing the cron job interval

这篇关于如何在Google应用引擎中自动收到有关错误的电子邮件提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆