Python django.db.models.functions.Now() Examples

The following are 7 code examples of django.db.models.functions.Now(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.models.functions , or try the search function .
Example #1
Source File: models.py    From yawn with MIT License 6 votes vote down vote up
def submit_run(self, parameters=None, scheduled_time=None):
        """Create a run of this template"""
        from yawn.task.models import Task

        run_parameters = self.parameters.copy()
        run_parameters.update(parameters or {})

        run = Run.objects.create(
            workflow=self,
            submitted_time=functions.Now(),
            scheduled_time=scheduled_time,
            parameters=run_parameters,
        )
        for template in self.template_set.all():
            task = Task.objects.create(
                run=run,
                template=template,
            )
            if not template.upstream.exists():
                task.enqueue()

        # refresh to get the actual DB submitted time
        run.refresh_from_db()
        return run 
Example #2
Source File: models.py    From yawn with MIT License 6 votes vote down vote up
def find_lost(timeout):
        from yawn.task.models import Execution

        # Make a sparse index so looking up active workers is fast:
        # CREATE INDEX yawn_worker_active ON yawn_worker (status) WHERE status = 'active'
        lost = Worker.objects.filter(
            status=Worker.ACTIVE, last_heartbeat__lt=functions.Now() - timedelta(seconds=timeout)
        )
        for worker in lost:
            logger.warning('Marking %r as lost', worker)
            worker.status = Worker.LOST
            worker.save()

            executions = worker.execution_set.filter(status=Execution.RUNNING)

            for execution in executions:
                logger.warning('Marking %r as lost', execution)
                execution.mark_finished(lost=True) 
Example #3
Source File: read.py    From django-cachalot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_now(self):
        """
        Checks that queries with a Now() parameter are not cached.
        """
        obj = Test.objects.create(datetime='1992-07-02T12:00:00')
        qs = Test.objects.filter(
            datetime__lte=Now())
        with self.assertNumQueries(1):
            obj1 = qs.get()
        with self.assertNumQueries(1):
            obj2 = qs.get()
        self.assertEqual(obj1, obj2)
        self.assertEqual(obj1, obj) 
Example #4
Source File: models.py    From yawn with MIT License 5 votes vote down vote up
def mark_finished(self, exit_code=None, lost=False):
        """
        Update the execution status after it has finished:
         successfully, in error, or because it was lost.

        Also update the task and workflow; re-queue the task if
         it should be retried.
        """
        if lost:
            self.status = Execution.LOST
            self.task.enqueue()

        elif exit_code == 0:
            self.task.status = Task.SUCCEEDED
            self.status = Execution.SUCCEEDED

        else:
            # queue another run if there are remaining retries
            # (current execution is not in count b/c it hasn't been saved yet)
            failed_count = self.task.execution_set.filter(status=Task.FAILED).count()
            if failed_count < self.task.template.max_retries:
                self.task.enqueue()
            else:
                self.task.status = Task.FAILED

            self.status = Execution.FAILED

        if self.task.status != Task.RUNNING:
            self.task.save()
            with transaction.atomic():
                self.task.update_downstream()
            if self.task.run:
                self.task.run.update_status()

        self.stop_timestamp = functions.Now()
        self.exit_code = exit_code
        # need to be careful not to overwrite stdout/stderr
        self.save(update_fields=['status', 'stop_timestamp', 'exit_code']) 
Example #5
Source File: main.py    From yawn with MIT License 5 votes vote down vote up
def update_worker(self):
        """
        Look for executors where the connection has broken and tasks need to be re-submitted.
        """
        if not self.worker:
            self.worker = Worker.objects.create(
                name=self.name,
                start_timestamp=functions.Now(),
                last_heartbeat=functions.Now()
            )
        else:
            self.worker.refresh_from_db()

        if self.worker.status == Worker.LOST:
            # someone else marked us as lost, terminate all tasks and exit
            logger.warning('Marked as lost, committing harakiri')
            self.state = State.terminate
            self.executor.mark_terminated(self.executor.get_running_ids())
            return

        # update our timestamp so no one marks us as lost
        self.worker.last_heartbeat = functions.Now()
        self.worker.save()

        # look for lost workers and re-queue their tasks
        Worker.find_lost(self.timeout) 
Example #6
Source File: test_now.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_basic(self):
        a1 = Article.objects.create(
            title='How to Django',
            text=lorem_ipsum,
            written=timezone.now(),
        )
        a2 = Article.objects.create(
            title='How to Time Travel',
            text=lorem_ipsum,
            written=timezone.now(),
        )
        num_updated = Article.objects.filter(id=a1.id, published=None).update(published=Now())
        self.assertEqual(num_updated, 1)
        num_updated = Article.objects.filter(id=a1.id, published=None).update(published=Now())
        self.assertEqual(num_updated, 0)
        a1.refresh_from_db()
        self.assertIsInstance(a1.published, datetime)
        a2.published = Now() + timedelta(days=2)
        a2.save()
        a2.refresh_from_db()
        self.assertIsInstance(a2.published, datetime)
        self.assertQuerysetEqual(
            Article.objects.filter(published__lte=Now()),
            ['How to Django'],
            lambda a: a.title
        )
        self.assertQuerysetEqual(
            Article.objects.filter(published__gt=Now()),
            ['How to Time Travel'],
            lambda a: a.title
        ) 
Example #7
Source File: apps.py    From django-rq-scheduler with MIT License 5 votes vote down vote up
def reschedule_scheduled_jobs(self):
        ScheduledJob = self.get_model('ScheduledJob')
        jobs = ScheduledJob.objects.filter(
            enabled=True, scheduled_time__lte=Now())
        self.reschedule_jobs(jobs)