class Component(object): """Base class of business object inheritance classes can make use of self.log, self.db and self.util directly without import or instantiating, """ log = RequiredFeature("log") db = RequiredFeature("db") util = RequiredFeature("util") scheduler = RequiredFeature("scheduler") cache = RequiredFeature("cache")
def send_emails(self, sender, receivers, subject, content, cc=[], bcc=[], attachments=[]): email_service = RequiredFeature("email") return email_service.send_emails(sender, receivers, subject, content, cc, bcc, attachments)
def scheduler_executor(feature, method, context): """task for all apscheduler jobs While the context of apscheduler job will be serialized and saved into MySQL, it's hard that add an instance method as an apscheduler job because the context is usually very complicate and not easy to be serialized. For example, see we want to add an new job to execute 'user_mgr.get_user_info' in 5 minutes, then the whole 'user_mgr' which involves many other classes will be serialized and saved which probably fail for many of them including 'user_mgr' itself are not serializable. However functions are much easier, that's why we define function 'scheduler_executor' out of any class. It acts as a redirect engine. We find the method that the job really want to execute and then call it. :type feature: str|unicode :param feature: the instance key for hackathon_factory. :type method: str|unicode :param method: the name of method related to instance :type context: Context, see definition in hackathon/__init__.py :param context: the expected execution context of target method """ log.debug("prepare to execute '%s.%s' with context: %s" % (feature, method, context)) inst = RequiredFeature(feature) mtd = getattr(inst, method) args_len = len(inspect.getargspec(mtd).args) if args_len < 2: # if target method doesn't expect any parameter except 'self', the args_len is 1 mtd() else: # call with execution context mtd(context)
def init_app(): """Initialize the application. Works including : - setting up hackathon factory, - register restful API routes - initialize scheduled jobs """ init_components() from .views import init_routes init_routes() init_schedule_jobs() health_check_guacamole = RequiredFeature("health_check_guacamole") u = RequiredFeature("util") if u.is_local(): log.debug("guacamole status: %s" % health_check_guacamole.report_health())
def __init_schedule_jobs(): """Init scheduled jobs in fact""" log.debug("init scheduled jobs......") util = RequiredFeature("util") sche = RequiredFeature("scheduler") if not util.is_local(): hackathon_manager = RequiredFeature("hackathon_manager") # schedule job to check recycle operation next_run_time = util.get_now() + timedelta(seconds=10) sche.add_interval(feature="expr_manager", method="scheduler_recycle_expr", id="scheduler_recycle_expr", next_run_time=next_run_time, minutes=10) # schedule job to pre-allocate environment hackathon_manager.schedule_pre_allocate_expr_job() # schedule job to pre-create a docker host server VM # host_server_manager.schedule_pre_allocate_host_server_job() # init the overtime-sessions detection to update users' online status sche.add_interval(feature="user_manager", method="check_user_online_status", id="check_user_online_status", minutes=10)
def send_sms(self, receiver, template_id, content): sms_service = RequiredFeature("sms") return sms_service.send_sms(receiver, template_id, content)
def send_voice_verify(self, receiver, content): voice_verify_service = RequiredFeature("voice_verify") return voice_verify_service.send_voice_verify(receiver, content)
def before_request(): user_manager = RequiredFeature("user_manager") user_manager.update_user_operation_time()