<s> """<STR_LIT>""" <EOL> from celery import Task <EOL> from pyramid import scripting <EOL> from pyramid . interfaces import IRequest <EOL> from pyramid . request import Request <EOL> from pyramid_tm import tm_tween_factory <EOL> def _pop_request_argument ( args , kwargs ) : <EOL> request = None <EOL> if args : <EOL> args = list ( args ) <EOL> request = args [ <NUM_LIT:0> ] <EOL> args . pop ( <NUM_LIT:0> ) <EOL> if kwargs : <EOL> request = kwargs . pop ( "<STR_LIT>" , None ) <EOL> return request , args , kwargs <EOL> class BadAsyncLifeCycleException ( Exception ) : <EOL> pass <EOL> class RequestAwareTask ( Task ) : <EOL> """<STR_LIT>""" <EOL> abstract = True <EOL> def get_env ( self ) : <EOL> registry = self . app . conf . PYRAMID_REGISTRY <EOL> base_url = registry . settings [ "<STR_LIT>" ] <EOL> request = Request . blank ( "<STR_LIT:/>" , base_url = base_url ) <EOL> env = scripting . prepare ( request = request , registry = registry ) <EOL> return env <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> pyramid_env = self . get_env ( ) <EOL> try : <EOL> underlying = super ( ) . __call__ <EOL> return underlying ( pyramid_env [ "<STR_LIT>" ] , * args , ** kwargs ) <EOL> finally : <EOL> pyramid_env [ "<STR_LIT>" ] ( ) <EOL> def apply_async_web_process ( self , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> args_ = kwargs . get ( "<STR_LIT:args>" , [ ] ) <EOL> kwargs_ = kwargs . get ( "<STR_LIT>" , { } ) <EOL> request , args_ , kwargs_ = _pop_request_argument ( args_ , kwargs_ ) <EOL> kwargs [ "<STR_LIT:args>" ] = args_ <EOL> kwargs [ "<STR_LIT>" ] = kwargs_ <EOL> if not IRequest . providedBy ( request ) : <EOL> raise BadAsyncLifeCycleException ( "<STR_LIT>" ) <EOL> if not hasattr ( request , "<STR_LIT>" ) : <EOL> return super ( ) . apply_async ( * args , ** kwargs ) <EOL> request . tm . get ( ) . addAfterCommitHook ( <EOL> self . _after_commit_hook , <EOL> args = args , <EOL> kws = kwargs , <EOL> ) <EOL> def apply_async_beat ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> return super ( ) . apply_async ( * args , ** options ) <EOL> def apply_async ( self , * args , ** options ) : <EOL> if "<STR_LIT>" in options : <EOL> return self . apply_async_beat ( * args , ** options ) <EOL> else : <EOL> return self . apply_async_web_process ( args , options ) <EOL> def _after_commit_hook ( self , success , args , kwargs , ** options ) : <EOL> """<STR_LIT>""" <EOL> if success : <EOL> super ( ) . apply_async ( args , kwargs , ** options ) <EOL> class TransactionalTask ( RequestAwareTask ) : <EOL> """<STR_LIT>""" <EOL> abstract = True <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> pyramid_env = self . get_env ( ) <EOL> try : <EOL> underlying = Task . __call__ . __get__ ( self , Task ) <EOL> def handler ( request ) : <EOL> underlying ( request , * args , ** kwargs ) <EOL> handler = tm_tween_factory ( handler , pyramid_env [ "<STR_LIT>" ] ) <EOL> result = handler ( pyramid_env [ "<STR_LIT>" ] ) <EOL> finally : <EOL> pyramid_env [ "<STR_LIT>" ] ( ) <EOL> return result </s>
