<s> from __future__ import absolute_import <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . importlib import import_module <EOL> from django . utils . timezone import now <EOL> from greenqueue . utils import Singleton <EOL> from . base import BaseService <EOL> from . . import settings , shortcuts <EOL> import logging , os , datetime <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class ZMQService ( BaseService ) : <EOL> socket = None <EOL> def start ( self ) : <EOL> self . manager = shortcuts . load_worker_class ( ) . instance ( ) <EOL> if self . manager . greenlet : <EOL> zmq = import_module ( "<STR_LIT>" ) . zmq <EOL> else : <EOL> zmq = import_module ( '<STR_LIT>' ) <EOL> self . manager . start ( ) <EOL> ctx = zmq . Context . instance ( ) <EOL> socket = ctx . socket ( zmq . PULL ) <EOL> socket . bind ( settings . GREENQUEUE_BIND_ADDRESS ) <EOL> log . info ( u"<STR_LIT>" . format ( <EOL> address = settings . GREENQUEUE_BIND_ADDRESS , <EOL> pid = os . getpid ( ) <EOL> ) ) <EOL> while True : <EOL> message = socket . recv_pyobj ( ) <EOL> ok , name = self . validate_message ( message ) <EOL> if not ok : <EOL> log . error ( "<STR_LIT>" ) <EOL> continue <EOL> self . manager . handle_message ( name , message ) <EOL> def create_socket ( self ) : <EOL> zmq = import_module ( '<STR_LIT>' ) <EOL> ctx = zmq . Context . instance ( ) <EOL> socket = ctx . socket ( zmq . PUSH ) <EOL> socket . connect ( settings . GREENQUEUE_BIND_ADDRESS ) <EOL> return socket <EOL> def send ( self , name , args = [ ] , kwargs = { } , eta = None , countdown = None ) : <EOL> new_uuid = self . create_new_uuid ( ) <EOL> message_object = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT:args>' : args , <EOL> '<STR_LIT>' : kwargs , <EOL> '<STR_LIT>' : new_uuid , <EOL> } <EOL> if eta is not None : <EOL> message_object [ '<STR_LIT>' ] = eta . isoformat ( ) <EOL> elif countdown is not None : <EOL> eta = now ( ) + datetime . timedelta ( seconds = countdown ) <EOL> message_object [ '<STR_LIT>' ] = eta . isoformat ( ) <EOL> socket = self . create_socket ( ) <EOL> socket . send_pyobj ( message_object ) <EOL> return new_uuid </s>
