<s> from django . db import models <EOL> class QueueManager ( models . Manager ) : <EOL> def publish ( self , queue_name , payload ) : <EOL> queue , created = self . get_or_create ( name = queue_name ) <EOL> queue . messages . create ( payload = payload ) <EOL> def fetch ( self , queue_name ) : <EOL> try : <EOL> queue = self . get ( name = queue_name ) <EOL> except self . model . DoesNotExist : <EOL> return <EOL> return queue . messages . pop ( ) <EOL> def purge ( self , queue_name ) : <EOL> try : <EOL> queue = self . get ( name = queue_name ) <EOL> except self . model . DoesNotExist : <EOL> return <EOL> messages = queue . messages . all ( ) <EOL> count = messages . count ( ) <EOL> messages . delete ( ) <EOL> return count <EOL> class MessageManager ( models . Manager ) : <EOL> def pop ( self ) : <EOL> try : <EOL> resultset = self . filter ( visible = True ) . order_by ( '<STR_LIT>' , '<STR_LIT:id>' ) <EOL> result = resultset [ <NUM_LIT:0> : <NUM_LIT:1> ] . get ( ) <EOL> result . visible = False <EOL> result . save ( ) <EOL> return result . payload <EOL> except self . model . DoesNotExist : <EOL> pass <EOL> def cleanup ( self ) : <EOL> self . filter ( visible = False ) . delete ( ) </s>
