Ejemplo n.º 1
0
    def handle(self, *args, start_block_number, **options):
        PeriodicTask.objects.filter(
            task='django_eth_events.tasks.event_listener').delete()
        time.sleep(5)
        call_command('cleandatabase')
        call_command('resync_daemon')
        self.stdout.write(
            self.style.SUCCESS('Making sure no process was running'))
        time.sleep(5)
        call_command('cleandatabase')
        call_command('resync_daemon')

        if start_block_number is not None:
            Block.objects.all().delete()

            daemon = Daemon.get_solo()
            daemon.block_number = start_block_number - 1
            daemon.save()

            self.stdout.write(
                self.style.SUCCESS('Restart processing at block {}'.format(
                    start_block_number)))

        # auto-create celery task
        interval = IntervalSchedule(every=5, period='seconds')
        interval.save()
        if not PeriodicTask.objects.filter(
                task='django_eth_events.tasks.event_listener').count():
            PeriodicTask.objects.create(
                name='Event Listener',
                task='django_eth_events.tasks.event_listener',
                interval=interval)
            self.stdout.write(
                self.style.SUCCESS(
                    'Created Periodic Task for Event Listener every 5s'))
Ejemplo n.º 2
0
    def save_model(self, request, obj: Site, form, change):
        obj.save()
        if not change:
            interval = IntervalSchedule()
            interval.every = obj.crawl_interval
            interval.period = IntervalSchedule.MINUTES
            interval.save()

            periodictask = PeriodicTask()
            periodictask.interval = interval
            periodictask.task = "newzz.crawler.tasks.create_new_news_item"
            periodictask.kwargs = json.dumps(
                json.loads(serialize('json', [
                    obj,
                ]))[0])
            periodictask.name = obj.id

            periodictask.enabled = True
            periodictask.save()

        else:
            periodictask = PeriodicTask.objects.get(name=obj.pk)
            periodictask.interval.every = obj.crawl_interval
            periodictask.enabled = True
            periodictask.task = "newzz.crawler.tasks.create_new_news_item"
            periodictask.kwargs = json.dumps(
                json.loads(serialize('json', [
                    obj,
                ]))[0])
            periodictask.interval.save()
            periodictask.save()

        print(change)
        print(obj)
Ejemplo n.º 3
0
 def schedule_every(task_name, period, every, args=[], kwargs={}):
     """ schedules a task by name every "every" "period". So an example call would be:
         TaskScheduler('seconds', 'mycustomtask', 30, [1,2,3]) 
         that would schedule your custom task to run every 30 seconds with the arguments 1,2 and 3 passed to the actual task. 
     """
     permissible_periods = ['hours', 'days', 'seconds', 'minutes']
     if period not in permissible_periods:
         raise Exception('Invalid period specified')
     # create the periodic task and the interval
     ptask_name = "%s_%s" % (task_name, datetime.now()
                             )  # create some name for the period task
     interval_schedules = IntervalSchedule.objects.filter(period=period,
                                                          every=every)
     if interval_schedules:  # just check if interval schedules exist like that already and reuse em
         interval_schedule = interval_schedules[0]
     else:  # create a brand new interval schedule
         interval_schedule = IntervalSchedule()
         interval_schedule.every = every  # should check to make sure this is a positive int
         interval_schedule.period = period
         interval_schedule.save()
     ptask = PeriodicTask(name=ptask_name,
                          task=task_name,
                          interval=interval_schedule)
     if args:
         ptask.args = args
     if kwargs:
         ptask.kwargs = kwargs
     ptask.save()
     return TaskScheduler.objects.create(periodic_task=ptask)
Ejemplo n.º 4
0
def create_sched_obj(**kwargs):
    """

    :param kwargs:
    :return:
    """
    name = kwargs.get('name', None)
    sched_type = kwargs.get('type', None)
    value = kwargs.get('value', None)

    if not all((
            name,
            value,
            sched_type,
    )):
        raise ParameterIsEmptyException(
            u'"name, value, sched_type" parameters cannot be empty !')

    if sched_type:
        sched_type = int(sched_type)

    close_old_connections()

    if sched_type == 1:
        tri = _get_interval(seconds=value)
        if not tri:
            value = int(value)
            tri = IntervalSchedule(every=value * 60, period='seconds')
            tri.save()

    else:
        tri = _get_crontab(cron_expression=value)
        if not tri:
            minute, hour, day_of_week, day_of_month, month_of_year = value.split(
                ' ')
            tri = CrontabSchedule(
                minute=minute,
                hour=hour,
                day_of_week=day_of_week,
                day_of_month=day_of_month,
                month_of_year=month_of_year,
                timezone='Asia/Shanghai',
            )
            tri.save()

    sched = SchedInfo(
        name=name,
        type=int(sched_type),
    )
    if sched_type == 1:
        sched.interval = tri
    else:
        sched.crontab = tri
    sched.save()
    return sched
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        from datetime import datetime
        from django.utils import timezone
        print('Importing settings...')
        AppSettings.objects.all().delete()
        

        print('Load data in DB')
        AppSettings.objects.create(name='Support Email', value='*****@*****.**', alias='support')
        AppSettings.objects.create(name='Noreplay Email', value='*****@*****.**', alias='noreply')
        print('Data loaded!')

        print('Import Intervals')
        IntervalSchedule.objects.all().delete()
        PeriodicTask.objects.all().delete()

        i1 = IntervalSchedule()
        i1.period = 'seconds'
        i1.every = 10
        i1.save()

        i2 = IntervalSchedule()
        i2.period = 'seconds'
        i2.every = 20
        i2.save()

        i3 = IntervalSchedule()
        i3.period = 'minutes'
        i3.every = 1
        i3.save()

        i5 = IntervalSchedule()
        i5.period = 'minutes'
        i5.every = 5
        i5.save()

        p = PeriodicTask()
        p.name = 'clearing offline users'
        p.task = 'online.tasks.remove_offline'
        p.interval = i5
        p.last_update = datetime.now(tz=timezone.utc)
        p.save()

        p = PeriodicTask()
        p.name = 'charging for chat messages'
        p.task = 'payment.tasks.charge_for_chat'
        p.interval = i1
        p.last_update = datetime.now(tz=timezone.utc)
        p.save()

        p = PeriodicTask()
        p.name = 'update active sockets'
        p.task = 'online.tasks.update_online'
        p.interval = i2
        p.last_update = datetime.now(tz=timezone.utc)
        p.save()

        load_smiles()
        load_replanisments()
        load_mail_tpls()
Ejemplo n.º 6
0
 def interval_schedule(self):
     print('interval_schedule')
     if len(IntervalSchedule.objects.filter(
         every=self.scrape_frecuency,
         period='minutes',
     )) == 0:
         interval_schedule = IntervalSchedule(
             every=self.scrape_frecuency,
             period='minutes',
         )
         interval_schedule.save()
     else:
         interval_schedule = IntervalSchedule.objects.get(
             every=self.scrape_frecuency,
             period='minutes',
         )
     return interval_schedule
Ejemplo n.º 7
0
 def post(self, request):
     posts = ActualNews.objects.all()
     form = SheduleForm(request.POST)
     if form.is_valid():
         timeout = form.cleaned_data['timeout']
         try:
             schedule = IntervalSchedule.objects.all()[0]
         except Exception:
             schedule = IntervalSchedule()
         schedule.every = timeout
         schedule.save()
         messages.add_message(request, messages.INFO,
                              'Updating every {} seconds'.format(timeout))
     return render(request,
                   'index.html',
                   context={
                       'posts': posts,
                       'form': form
                   })
Ejemplo n.º 8
0
def createTaskSnmpGet(host_nomeTabela_snmpGet, host_ip, host_porta,
                      host_community, templates, item_id, item_nome, item_oid,
                      item_intervaloAtualizacao, item_intervaloAtualizacaoUn):
    template_ids = ''
    for template in templates:
        template_ids = template_ids + ('_template_id:' + str(template.id))

    intervalo_id = None

    novoIntervalo = IntervalSchedule(every=item_intervaloAtualizacao,
                                     period=item_intervaloAtualizacaoUn)

    intervalosCadastrados = IntervalSchedule.objects.all()

    for intervalo in intervalosCadastrados:
        if (intervalo.every
                == novoIntervalo.every) and (intervalo.period
                                             == novoIntervalo.period):
            intervalo_id = intervalo.id
            print('ACHEI UM INTERVALO NO BD')

    if intervalo_id is None:
        novoIntervalo.save()
        intervalo_id = novoIntervalo.id
        print('NÃO ACHEI UM INTERVALO E SALVEI NO BD')

    novaTask = PeriodicTask(name='SNMPGETTASK=' + host_nomeTabela_snmpGet +
                            str(template_ids) + '_item_id:' + str(item_id),
                            task='hosts.tasks.task_snmp_get',
                            args='[' + '"' + host_nomeTabela_snmpGet + '", "' +
                            host_community + '", "' + str(item_id) + '", "' +
                            item_nome + '", "' + host_ip + '", "' + item_oid +
                            '", "' + str(host_porta) + '", "' +
                            str(template_ids) + '"]',
                            kwargs='{}',
                            enabled=1,
                            interval_id=intervalo_id,
                            one_off=0,
                            headers='{}')
    novaTask.save()
    print('CRIEI UMA NOVA TAREFA')
Ejemplo n.º 9
0
    def handle(self, *args, **options):
        EventDescription.objects.all().delete()
        Block.objects.all().delete()
        daemon = Daemon.get_solo()
        daemon.block_number = 0
        daemon.last_error_block_number = 0
        daemon.save()
        self.stdout.write(self.style.SUCCESS('DB Successfully cleaned.'))

        # auto-create celery task
        interval = IntervalSchedule(every=5, period='seconds')
        interval.save()
        if not PeriodicTask.objects.filter(
                task='django_eth_events.tasks.event_listener').count():
            PeriodicTask.objects.create(
                name='Event Listener',
                task='django_eth_events.tasks.event_listener',
                interval=interval)
            self.stdout.write(
                self.style.SUCCESS(
                    'Created Periodic Task for Event Listener every 5s.'))
Ejemplo n.º 10
0
def createTaskCleanData(host_nomeTabela_snmpGet, templates, item_id,
                        item_tempoArmazenamentoDados,
                        item_tempoArmazenamentoDadosUn):

    template_ids = ''
    for template in templates:
        template_ids = template_ids + ('_template_id:' + str(template.id))

    intervalo_id = None

    novoIntervalo = IntervalSchedule(every=1, period='minutes')

    intervalosCadastrados = IntervalSchedule.objects.all()

    for intervalo in intervalosCadastrados:
        if (intervalo.every
                == novoIntervalo.every) and (intervalo.period
                                             == novoIntervalo.period):
            intervalo_id = intervalo.id
            print('ACHEI UM INTERVALO NO BD')

    if intervalo_id is None:
        novoIntervalo.save()
        intervalo_id = novoIntervalo.id
        print('NÃO ACHEI UM INTERVALO E SALVEI NO BD')

    novaTask = PeriodicTask(name='CLEANDATATASK=' + host_nomeTabela_snmpGet +
                            str(template_ids) + '_item_id:' + str(item_id),
                            task='hosts.tasks.task_clean_data',
                            args='[' + '"' + str(item_id) + '", "' +
                            str(item_tempoArmazenamentoDados) + '", "' +
                            item_tempoArmazenamentoDadosUn + '", "' +
                            host_nomeTabela_snmpGet + '"]',
                            kwargs='{}',
                            enabled=1,
                            interval_id=intervalo_id,
                            one_off=0,
                            headers='{}')
    novaTask.save()
    print('CRIEI UMA NOVA TAREFA')
Ejemplo n.º 11
0
Archivo: tasks.py Proyecto: tccjrl/tcc
def create_task_snmpGet_item_updated(item_id, item_nome_old, item_oid_old,
                                     item_intervaloAtualizacao_old,
                                     item_intervaloAtualizacaoUn_old):
    lista_periodicTasks = PeriodicTask.objects.filter(
        name__contains=('_item_id:' +
                        str(item_id))).filter(name__contains=('SNMPGETTASK'))
    item = Item.objects.get(id=item_id)

    for periodicTask in lista_periodicTasks:
        args = periodicTask.args
        print(item_nome_old, item_oid_old)
        args = args.replace('"' + item_nome_old + '"',
                            '"' + item.item_nome + '"')
        args = args.replace('"' + item_oid_old + '"',
                            '"' + item.item_oid + '"')
        periodicTask.args = args

        intervalo_id = None

        novoIntervalo = IntervalSchedule(
            every=item_intervaloAtualizacao_old,
            period=item_intervaloAtualizacaoUn_old)

        intervalosCadastrados = IntervalSchedule.objects.all()

        for intervalo in intervalosCadastrados:
            if (intervalo.every
                    == novoIntervalo.every) and (intervalo.period
                                                 == novoIntervalo.period):
                intervalo_id = intervalo.id
                print('ACHEI UM INTERVALO NO BD')

        if intervalo_id is None:
            novoIntervalo.save()
            intervalo_id = novoIntervalo.id
            print('NÃO ACHEI UM INTERVALO E SALVEI NO BD')

        periodicTask.interval_id = intervalo_id

        periodicTask.save()
Ejemplo n.º 12
0
Archivo: tasks.py Proyecto: tccjrl/tcc
def create_task_CleanData_item_updated(item_id,
                                       item_tempoArmazenamentoDados_old,
                                       item_tempoArmazenamentoDadosUn_old):
    lista_periodicTasks = PeriodicTask.objects.filter(
        name__contains=('_item_id:' +
                        str(item_id))).filter(name__contains=('CLEANDATATASK'))
    item = Item.objects.get(id=item_id)

    for periodicTask in lista_periodicTasks:
        args = periodicTask.args
        args = args.replace(
            '"' + str(item_tempoArmazenamentoDados_old) + '"',
            ('"' + str(item.item_tempoArmazenamentoDados) + '"'))
        args = args.replace('"' + item_tempoArmazenamentoDadosUn_old + '"',
                            '"' + item.item_tempoArmazenamentoDadosUn + '"')
        periodicTask.args = args

        intervalo_id = None

        novoIntervalo = IntervalSchedule(every=1, period='minutes')

        intervalosCadastrados = IntervalSchedule.objects.all()

        for intervalo in intervalosCadastrados:
            if (intervalo.every
                    == novoIntervalo.every) and (intervalo.period
                                                 == novoIntervalo.period):
                intervalo_id = intervalo.id
                print('ACHEI UM INTERVALO NO BD')

        if intervalo_id is None:
            novoIntervalo.save()
            intervalo_id = novoIntervalo.id
            print('NÃO ACHEI UM INTERVALO E SALVEI NO BD')

        periodicTask.interval_id = intervalo_id

        periodicTask.save()
Ejemplo n.º 13
0
    def handle(self, *args, start_block_number, **options):
        PeriodicTask.objects.filter(task__in=[
            'django_eth_events.tasks.event_listener',
            'tradingdb.relationaldb.tasks.calculate_scoreboard',
            'tradingdb.relationaldb.tasks.issue_tokens',
            'tradingdb.relationaldb.tasks.clear_issued_tokens_flag',
        ]).delete()
        time.sleep(5)
        call_command('cleandatabase')
        call_command('resync_daemon')
        self.stdout.write(
            self.style.SUCCESS('Making sure no process was running'))

        time.sleep(5)
        call_command('cleandatabase')
        call_command('resync_daemon')

        if start_block_number is not None:
            Block.objects.all().delete()

            daemon = Daemon.get_solo()
            daemon.block_number = start_block_number - 1
            daemon.save()

            self.stdout.write(
                self.style.SUCCESS('Restart processing at block {}'.format(
                    start_block_number)))

        # auto-create celery task
        five_seconds_interval = IntervalSchedule(every=5, period='seconds')
        five_seconds_interval.save()

        ten_minutes_interval = IntervalSchedule(every=10, period='minutes')
        ten_minutes_interval.save()

        one_minute_interval = IntervalSchedule(every=1, period='minutes')
        one_minute_interval.save()

        one_day_interval = IntervalSchedule(every=1, period='days')
        one_day_interval.save()

        PeriodicTask.objects.create(
            name='Event Listener',
            task='django_eth_events.tasks.event_listener',
            interval=five_seconds_interval,
        )
        self.stdout.write(
            self.style.SUCCESS(
                'Created Periodic Task for Event Listener every 5 seconds'))

        PeriodicTask.objects.create(
            name='Scoreboard Calculation',
            task='tradingdb.relationaldb.tasks.calculate_scoreboard',
            interval=ten_minutes_interval,
        )
        self.stdout.write(
            self.style.SUCCESS(
                'Created Periodic Task for Scoreboard every 10 minutes'))

        PeriodicTask.objects.create(
            name='Token issuance',
            task='tradingdb.relationaldb.tasks.issue_tokens',
            interval=one_minute_interval,
        )
        self.stdout.write(
            self.style.SUCCESS(
                'Created Periodic Task for Token Issuance every minute'))

        PeriodicTask.objects.create(
            name='Token issuance flag clear',
            task='tradingdb.relationaldb.tasks.clear_issued_tokens_flag',
            interval=one_day_interval,
        )
        self.stdout.write(
            self.style.SUCCESS(
                'Created Periodic Task for Token Issuance flag clear every day'
            ))

        TournamentWhitelistedCreator.objects.create(
            address=normalize_address_without_0x(
                settings.ETHEREUM_DEFAULT_ACCOUNT),
            enabled=True)
        self.stdout.write(
            self.style.SUCCESS('Added User {} to Tournament Whitelist'.format(
                settings.ETHEREUM_DEFAULT_ACCOUNT)))