def task(self, *args, **kwargs): from celery.decorators import task if len(args) == 1 and callable(args[0]): return task(base=self.create_task_cls())(*args) if "base" not in kwargs: kwargs["base"] = self.create_task_cls() return task(*args, **kwargs)
def statistic_misc(): ts = TerminalStatistic() ts.statistic_misc() if __name__ == '__main__': ConfHelper.load(options.conf) parse_command_line() #NOTE: here, you can name the date to be statisticed. for item in range(17,18): year = '2013' month = '08' day = item timestamp = int(time.mktime(time.strptime("%s-%s-%s-23-59-59"%(year,month,day),"%Y-%m-%d-%H-%M-%S"))) logging.info('[CHECKER] year: %s, month: %s, day: %s, timestamp: %s. ' , year, month, day,timestamp) ts = TerminalStatistic() #ts.statistic_online_terminal(timestamp) #ts.statistic_user(timestamp) #ts.statistic_offline_terminal(timestamp) #ts.statistic_misc() else: try: from celery.decorators import task statistic_offline_terminal = task(ignore_result=True)(statistic_offline_terminal) statistic_online_terminal = task(ignore_result=True)(statistic_online_terminal) statistic_user = task(ignore_result=True)(statistic_user) except Exception as e: logging.exception("[CELERY] admintask statistic failed. Exception: %s", e.args)
except requests.exceptions.RequestException: # for an unknown reason, curl may work here, and requests fail # so let's try it out skip_ssl_flag = '-k ' if not verify_ssl else '' p = subprocess.Popen( ('curl %s %s-m 3 -I' % (service.connection_string, skip_ssl_flag)).split(), stdout=subprocess.PIPE) res = p.communicate()[0] if any([status in res for status in ('500', '501', '502', '503', '504')]): service.update_status('Down', res) else: service.update_status('Up', res) check_https = task(http_checker) check_http = task(http_checker) @task def check_ping(service): """Should ping service.host""" res = subprocess.call(('/bin/ping -c 5 -i 0.5 -w 5 %s' % service.connection.hostname).split()) if res == 0: service.update_status('Up', "ping okay") else: service.update_status('Down', "ping failed")
def send_admin_mail(subject, message=None, *args, **kwargs): from django.conf import settings from .utils.mail import SendmailQueue SendmailQueue()._add( subject=subject, body=message, html_body=kwargs.get('html_message', None), to=[v[1] for v in settings.ADMINS] )._flush( request=kwargs.get('request', None) ) try: from celery.decorators import task send_admin_mail_task = task(send_admin_mail, name='send_admin_mail') except ImportError: send_admin_mail_task = send_admin_mail send_admin_mail_task.delay = send_admin_mail def send_admin_mail_delay(subject, message=None, *args, **kwargs): try: # Try to send mail in background (if Celery is available). send_admin_mail_task.delay(subject, message, *args, **kwargs) except Exception: # If Celery is not running or is improperly configured, try to send mail immediately. send_admin_mail(subject, message, *args, **kwargs) class DjkEmailHandler(AdminEmailHandler):
from celery import Task from celery.decorators import task from celery.utils.log import get_task_logger logger = get_task_logger(__name__) class BaseTask(Task): def on_failure(self, exc, task_id, args, kwargs, einfo): # TODO: Emails in production # TODO: Validation logger.error('BaseTask ERROR', exc, task_id, args, kwargs, einfo) task = task(base=BaseTask) # noqa: F821
from oddasat.data.rss_parse import fetchURL # from oddasat.feeds.models import FeedItem ; a = FeedItem.objects.all() ; a[0].save() from celery.decorators import task import time from django.conf import settings if settings.CELERY: fetchURL = task(fetchURL).delay else: pass def fetch_feeditem(item): """ Fetches data for feed item. """ # do stuff # print "Signal called." data = fetchURL(item.url) try: while not data.ready(): # print "waiting: %s" % str(data.ready()) continue else: if data.ready(): data = data.result except: pass
# can instanciate and run the appropriate operation here. def start_process(process_as_json_string): ret = None proc = json.loads(process_as_json_string) logger.info("Starting process %s %s" % (proc['id'], current_task.backend)) def progress_callback(value=0, label=None): current_task.update_state(state=PROGRESS, meta={ 'process': proc.get('id'), 'value': value, 'label': label }) opclass = operations.REGISTERED_OPERATIONS[proc.get('operation')] op = opclass(source=os.path.join(getattr(settings, 'MEDIA_ROOT', ''), proc.get('source')), destination=proc.get('destination'), parameters=proc.get('parameters') or {}, progress_callback=progress_callback) ret = op.start() logger.info("Finished process %s" % proc.get('id')) # Augment task output with process id ret['process'] = proc.get('id') return ret # Define operations as tasks: For every opclass, define a dynamic # op.method that will instanciate the opclass with the deserialized # json parameters for opname in operations.REGISTERED_OPERATIONS: REGISTERED_TASKS[opname] = task(name="tmg." + opname, track_started=True)(start_process)
except requests.exceptions.RequestException: # for an unknown reason, curl may work here, and requests fail # so let's try it out skip_ssl_flag = '-k ' if not verify_ssl else '' p = subprocess.Popen( ('curl %s %s-m 3 -I' % (service.connection_string, skip_ssl_flag)).split(), stdout=subprocess.PIPE) res = p.communicate()[0] if any( [status in res for status in ('500', '501', '502', '503', '504')]): service.update_status('Down', res) else: service.update_status('Up', res) check_https = task(http_checker) check_http = task(http_checker) @task def check_ping(service): """Should ping service.host""" res = subprocess.call(('/bin/ping -c 5 -i 0.5 -w 5 %s' % service.connection.hostname).split()) if res == 0: service.update_status('Up', "ping okay") else: service.update_status('Down', "ping failed")
ct = CheckTask() ct.send_offline_remind_sms() def mileage_notify(): ct = CheckTask() ct.mileage_notify() def day_notify(): ct = CheckTask() ct.day_notify() if __name__ == '__main__': ConfHelper.load(options.conf) parse_command_line() #check_charge() #offline_remind() #mileage_notify() #day_notify() else: try: from celery.decorators import task check_poweroff = task(ignore_result=True)(check_poweroff) #check_charge= task(ignore_result=True)(check_charge) check_track = task(ignore_result=True)(check_track) offline_remind = task(ignore_result=True)(offline_remind) mileage_notify = task(ignore_result=True)(mileage_notify) day_notify = task(ignore_result=True)(day_notify) except Exception as e: logging.exception("[CELERY] admintask statistic failed. Exception: %s", e.args)