Skip to content

Instantly share code, notes, and snippets.

@siroken3
Created August 28, 2012 10:54
Show Gist options
  • Save siroken3/3497184 to your computer and use it in GitHub Desktop.
Save siroken3/3497184 to your computer and use it in GitHub Desktop.
所定時間内に終了しなかったらaws-snsで通知する関数デコレータ ref: http://qiita.com/items/c4e77bc8a3be53aacb08
# -*- coding: utf-8 -*-
import signal
import boto.sns
def timeout(limit, topic, subject='Execution Timeout.', body='Please check program.', region='us-east-1'):
'''
指定した実行時間(秒)に終了しなかった場合、awsのsnsで通知するデコレータです。
この例では long_time_func()を呼び出し後10秒経過していたら aws snsに通知が飛びます。
@timeout(limit=10,topic='arn:aws:sns:xxxxxxxx:yyyyyyy')
def long_time_func():
import time
time.sleep(30)
'''
_region = region
_topic = topic
_subject = subject
_body = body
def notify_aws_sns(signum, frame):
conn = boto.sns.connect_to_region(_region)
conn.publish(_topic, _body, _subject)
def __decorator(function):
signal.signal(signal.SIGALRM, notify_aws_sns)
signal.alarm(limit)
return function
return __decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment