Exemplo n.º 1
0
    def do_render(self, doc):
        # Skip very recently rendered documents. This should help make it
        # easier to start and stop an --all command without needing to start
        # from the top of the list every time.
        if doc.last_rendered_at:
            now = datetime.datetime.now()
            render_age = now - doc.last_rendered_at
            min_age = datetime.timedelta(seconds=self.options['min_age'])
            if (render_age < min_age):
                logging.debug(u"Skipping %s (%s) - rendered %s sec ago" %
                              (doc, doc.get_absolute_url(), render_age))
                return

        if self.options['force']:
            doc.render_started_at = None

        if self.options['nocache']:
            cc = 'no-cache'
        else:
            cc = 'max-age=0'

        if self.options['defer']:
            logging.info(u"Queuing deferred render for %s (%s)" %
                          (doc, doc.get_absolute_url()))
            render_document.delay(doc.pk, cc, self.base_url)
            logging.debug(u"Queued.")

        else:
            logging.info(u"Rendering %s (%s)" %
                         (doc, doc.get_absolute_url()))
            try:
                render_document(doc, cc, self.base_url)
                logging.debug(u"DONE.")
            except DocumentRenderingInProgress:
                logging.error(u"Rendering is already in progress for this document")
Exemplo n.º 2
0
    def do_render(self, doc):
        # Skip very recently rendered documents. This should help make it
        # easier to start and stop an --all command without needing to start
        # from the top of the list every time.
        if doc.last_rendered_at:
            now = datetime.datetime.now()
            render_age = now - doc.last_rendered_at
            min_age = datetime.timedelta(seconds=self.options['min_age'])
            if (render_age < min_age):
                logging.debug(u"Skipping %s (%s) - rendered %s sec ago" %
                              (doc, doc.get_absolute_url(), render_age))
                return

        if self.options['force']:
            doc.render_started_at = None

        if self.options['nocache']:
            cc = 'no-cache'
        else:
            cc = 'max-age=0'

        if self.options['defer']:
            logging.info(u"Queuing deferred render for %s (%s)" %
                         (doc, doc.get_absolute_url()))
            render_document.delay(doc.pk, cc, self.base_url)
            logging.debug(u"Queued.")

        else:
            logging.info(u"Rendering %s (%s)" % (doc, doc.get_absolute_url()))
            try:
                render_document(doc, cc, self.base_url)
                logging.debug(u"DONE.")
            except DocumentRenderingInProgress:
                logging.error(
                    u"Rendering is already in progress for this document")
Exemplo n.º 3
0
    def handle(self, *args, **options):
        base_url = options['baseurl'] or absolutify('')
        if options['nocache']:
            cache_control = 'no-cache'
        else:
            cache_control = 'max-age=0'
        force = options['force']
        invalidate_cdn_cache = not options['skip_cdn_invalidation']

        if options['all']:
            # Query all documents, excluding those whose `last_rendered_at` is
            # within `min_render_age` or NULL.
            min_render_age = (
                datetime.datetime.now() -
                datetime.timedelta(seconds=options['min_age']))
            docs = Document.objects.filter(
                Q(last_rendered_at__isnull=True) |
                Q(last_rendered_at__lt=min_render_age))
            if options['locale']:
                docs = docs.filter(locale=options['locale'])
            if options['not_locale']:
                docs = docs.exclude(locale=options['not_locale'])
            docs = docs.order_by('-modified')
            docs = docs.values_list('id', flat=True)

            self.chain_render_docs(
                docs,
                cache_control,
                base_url,
                force,
                invalidate_cdn_cache=invalidate_cdn_cache)

        else:
            # Accept page paths from command line, but be liberal
            # in what we accept, eg: /en-US/docs/CSS (full path);
            # /en-US/CSS (no /docs); or even en-US/CSS (no leading slash)
            paths = options['paths']
            if not paths:
                raise CommandError('Need at least one document path to render')

            for path in paths:
                if path.startswith('/'):
                    path = path[1:]
                locale, sep, slug = path.partition('/')
                head, sep, tail = slug.partition('/')
                if head == 'docs':
                    slug = tail
                doc = Document.objects.get(locale=locale, slug=slug)
                log.info('Rendering %s (%s)' % (doc, doc.get_absolute_url()))
                try:
                    render_document(
                        doc.pk, cache_control, base_url, force,
                        invalidate_cdn_cache=invalidate_cdn_cache
                    )
                    log.debug('DONE.')
                except DocumentRenderingInProgress:
                    log.error(
                        'Rendering is already in progress for this document.')
Exemplo n.º 4
0
    def handle(self, *args, **options):
        self.options = options
        self.base_url = options['baseurl'] or absolutify('')
        if self.options['nocache']:
            self.cache_control = 'no-cache'
        else:
            self.cache_control = 'max-age=0'

        if options['all']:
            # Query all documents, excluding those whose `last_rendered_at` is
            # within `min_render_age` or NULL.
            min_render_age = (
                datetime.datetime.now() -
                datetime.timedelta(seconds=self.options['min_age']))
            docs = Document.objects.filter(
                Q(last_rendered_at__isnull=True) |
                Q(last_rendered_at__lt=min_render_age))
            docs = docs.order_by('-modified')
            docs = docs.values_list('id', flat=True)

            self.chain_render_docs(docs)

        else:
            if not len(args) == 1:
                raise CommandError('Need at least one document path to render')
            for path in args:
                # Accept a single page path from command line, but be liberal
                # in what we accept, eg: /en-US/docs/CSS (full path);
                # /en-US/CSS (no /docs); or even en-US/CSS (no leading slash)
                if path.startswith('/'):
                    path = path[1:]
                locale, sep, slug = path.partition('/')
                head, sep, tail = slug.partition('/')
                if head == 'docs':
                    slug = tail
                doc = Document.objects.get(locale=locale, slug=slug)
                log.info(u'Rendering %s (%s)' % (doc, doc.get_absolute_url()))
                try:
                    render_document(doc.pk, self.cache_control, self.base_url,
                                    self.options['force'])
                    log.debug(u'DONE.')
                except DocumentRenderingInProgress:
                    log.error(
                        u'Rendering is already in progress for this document.')
Exemplo n.º 5
0
    def handle(self, *args, **options):
        self.options = options
        self.base_url = options['baseurl'] or absolutify('')
        if self.options['nocache']:
            self.cache_control = 'no-cache'
        else:
            self.cache_control = 'max-age=0'

        if options['all']:
            # Query all documents, excluding those whose `last_rendered_at` is
            # within `min_render_age` or NULL.
            min_render_age = (
                datetime.datetime.now() -
                datetime.timedelta(seconds=self.options['min_age']))
            docs = Document.objects.filter(
                Q(last_rendered_at__isnull=True) |
                Q(last_rendered_at__lt=min_render_age))
            docs = docs.order_by('-modified')
            docs = docs.values_list('id', flat=True)

            self.chain_render_docs(docs)

        else:
            if not len(args) == 1:
                raise CommandError('Need at least one document path to render')
            for path in args:
                # Accept a single page path from command line, but be liberal
                # in what we accept, eg: /en-US/docs/CSS (full path);
                # /en-US/CSS (no /docs); or even en-US/CSS (no leading slash)
                if path.startswith('/'):
                    path = path[1:]
                locale, sep, slug = path.partition('/')
                head, sep, tail = slug.partition('/')
                if head == 'docs':
                    slug = tail
                doc = Document.objects.get(locale=locale, slug=slug)
                log.info(u'Rendering {0!s} ({1!s})'.format(doc, doc.get_absolute_url()))
                try:
                    render_document(doc.pk, self.cache_control, self.base_url,
                                    self.options['force'])
                    log.debug(u'DONE.')
                except DocumentRenderingInProgress:
                    log.error(
                        u'Rendering is already in progress for this document.')
Exemplo n.º 6
0
    def handle(self, *args, **options):
        base_url = options["baseurl"] or absolutify("")
        if options["nocache"]:
            cache_control = "no-cache"
        else:
            cache_control = "max-age=0"
        force = options["force"]
        invalidate_cdn_cache = not options["skip_cdn_invalidation"]

        if options["all"]:
            # Query all documents, excluding those whose `last_rendered_at` is
            # within `min_render_age` or NULL.
            min_render_age = datetime.datetime.now() - datetime.timedelta(
                seconds=options["min_age"])
            docs = Document.objects.filter(
                Q(last_rendered_at__isnull=True)
                | Q(last_rendered_at__lt=min_render_age))
            if options["locale"]:
                docs = docs.filter(locale=options["locale"])
            if options["not_locale"]:
                docs = docs.exclude(locale=options["not_locale"])
            if options["slugsearch"]:
                if options["slugsearch"].endswith("*"):
                    docs = docs.exclude(
                        slug__startswith=options["slugsearch"].rstrip("*"))
                elif "*" in options["slugsearch"]:
                    raise NotImplementedError("* can only be on the end")
                else:
                    docs = docs.exclude(slug__contains=options["slugsearch"])
            docs = docs.order_by("-modified")
            docs = docs.values_list("id", flat=True)

            self.chain_render_docs(
                docs,
                cache_control,
                base_url,
                force,
                invalidate_cdn_cache=invalidate_cdn_cache,
            )

        else:
            # Accept page paths from command line, but be liberal
            # in what we accept, eg: /en-US/docs/CSS (full path);
            # /en-US/CSS (no /docs); or even en-US/CSS (no leading slash)
            paths = options["paths"]
            if not paths:
                raise CommandError("Need at least one document path to render")

            for path in paths:
                if path.startswith("/"):
                    path = path[1:]
                locale, sep, slug = path.partition("/")
                head, sep, tail = slug.partition("/")
                if head == "docs":
                    slug = tail
                doc = Document.objects.get(locale=locale, slug=slug)
                log.info(f"Rendering {doc} ({doc.get_absolute_url()})")
                try:
                    render_document(
                        doc.pk,
                        cache_control,
                        base_url,
                        force,
                        invalidate_cdn_cache=invalidate_cdn_cache,
                    )
                    log.debug("DONE.")
                except DocumentRenderingInProgress:
                    log.error(
                        "Rendering is already in progress for this document.")