Пример #1
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()
Пример #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)
Пример #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)
Пример #4
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
                   })