Created
August 28, 2012 11:42
-
-
Save siroken3/3497447 to your computer and use it in GitHub Desktop.
指定時間内に関数が終了しなかったらAWS-SNSで通知するデコレータ ref: http://qiita.com/items/9d6730acbfa4c8e0b5a4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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で通知するデコレータです。 | |
@timeout(limit=3600, topic='arn:aws:sns:xxxxxxxx:yyyyyyy') | |
def long_time_function(): | |
very_very_long_calc() | |
''' | |
def notify_aws_sns(signum, frame): | |
conn = boto.sns.connect_to_region(region) | |
conn.publish(topic, body, subject) | |
def __decorator(function): | |
def __wrapper(*args, **kwargs): | |
signal.signal(signal.SIGALRM, notify_aws_sns) | |
signal.alarm(limit) | |
result = function(*args, **kwargs) | |
signal.alarm(0) | |
return result | |
return __wrapper | |
return __decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment