beatonma / django-wm

Automatic Webmention functionality for Django models
https://beatonma.org/webmentions_tester/
GNU General Public License v3.0
13 stars 2 forks source link

How to debug the Outgoing Mentions functionality #9

Open rasulkireev opened 4 years ago

rasulkireev commented 4 years ago

I have fully implemented the django-wm library. I tested for incoming webmentions via https://django-wm.dev/. Everything works fine, I can receive the Hcard information and the mention itself.

When it comes to outgoing mentions, here I am running into a couple of issues. I have tested the outgoing mention using https://django-wm.dev/ and https://webmention.rocks/. Neither worked. The link to a "mentionable" post is https://rasulkireev.com/writings/wm-test-1.

The problem is that I am not receiving any errors, so I am not entirely sure what to do. @beatonma Do you have any ideas or suggestions? Thanks a ton in advance.

beatonma commented 4 years ago

Hey,

I think I know what's causing this - MentionableMixin has the fields:

allow_incoming_webmentions = models.BooleanField(default=True)
allow_outgoing_webmentions = models.BooleanField(default=False)

As you can see, allow_outgoing_webmentions defaults to False. Thank you for reminding me about this default behaviour! I definitely need to add this to the documentation because it's not at all obvious.

So for outgoing webmentions to be processed you need to set allow_outgoing_webmentions to True. How you do that depends on your preference and/or how you create your posts.

Change default behaviour

If you always want your model to process outgoing mentions you can override the field on your model:

class MyPostModel(MentionableMixin, models.Model):
    allow_outgoing_webmentions = models.BooleanField(default=True)
    ...

    def all_text(self):
        """This also needs to be implemented if you haven't already!
        It should return whatever text you want to process for
        outgoing mentions."""
        return f'{self.title} {self.content}'

(You will need to run makemigrations and migrate after doing this.)

Outgoing mentions should then be processed every time you save your post.

Keep default behaviour

Creating/editing via Django Admin pages

If you are editing your posts via the Django Admin and you are using fieldsets then add allow_outgoing_webmentions:

@admin.register(MyPostModel)
class MyPostModelAdmin(admin.ModelAdmin):
    fieldsets = (
        ...
        ('Mentions', {
            'fields': (
                'allow_incoming_webmentions',
                'allow_outgoing_webmentions',
            ),
        }),
        ...
    )

This will provide a checkbox that you can tick when you want to process outgoing mentions.

(If you are not using fieldsets then those fields should already appear on the admin page.)

Creating/editing manually

If you are creating the posts via MyPostModel.objects.create() then you will need to set the field before saving:

post = MyPostModel.objects.create(title='whatever')
post.allow_outgoing_webmentions = True
post.save()

Let me know if that fixes the issue for you. I will add this to the documentation asap.

rasulkireev commented 4 years ago

I will certainly adjust my model to have allow_outgoing_webmentions = models.BooleanField(default=True). However I think this is not the issue.

This is how my django-admin "Add a Post" looks like: image

I made sure to allow outgoing webmentions before saving the post.

I will make some adjustment to my code base as you suggested. Meanwhile, do you have a suggestion as to how to debug sending and receiving webmentions? For example, do you think it would be useful to check celery logs on the server?

Thanks a ton in advance.

beatonma commented 4 years ago

Hey,

Yes, your celery logs should show several messages while the task runs. Something like:

outgoing_webmentions Checking for outgoing webmention links...
outgoing_webmentions Checking url=https://django-wm.dev
outgoing_webmentions Found webmention endpoint: https://django-wm.dev:443/webmention/
outgoing_webmentions https://django-wm.dev:443/webmention/: {'target': 'https://django-wm.dev', 'source': 'https://beatonma.org/a/190406-webmention-test'}
outgoing_webmentions Sending webmention to "https://django-wm.dev:443/webmention/" successful with status_code=202

Also, I have just published an update which adds a new model to help track the status of outgoing mentions. I've also added a log message so you can see when the process_outgoing_webmentions task is passed to Celery after saving your post.

Please update to this version, run makemigrations and migrate, reload your server and save a post with a mentionable link again. You should then see Outgoing webmention processing task added to queue... in your server log, and you should see an entry at your-server-admin-url/mentions/outgoingwebmentionstatus/ like this:

outgoing-wm-admin

Sorry for the late response - I'm off until Tuesday now so I'll be more available until then!

Michael

rasulkireev commented 4 years ago

What command do you use to check Celery logs on your server (or what file you look at)? I've googled this and read the documentation, but couldn't get it quite right. Thanks in advance.

beatonma commented 4 years ago

I'm not sure what the default behaviour is - you could try tail -f /var/log/syslog. If that doesn't show anything useful I'd look in /var/log/ for something like celery.log.

Otherwise, you can configure the log to go anywhere you like via the LOGGING entry in your project settings file. If you already have a LOGGING entry you can just add the following to LOGGING.loggers:

        'celery.task': {
            'handlers': ['console', 'file',],  # These are defined in LOGGING.handlers - see below if you need to add those entries
            'propagate': True,
            'level': 'DEBUG',
        },

Here's an example:

# settings.py
CELERY_LOG_PATH = '/path/to/mysite-celery.log'  # Make sure your `celery` user has write access to this file, wherever you put it!
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %($
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'verbose',
            'filename': CELERY_LOG_PATH,
            'maxBytes': 1 * 1024 * 1024,
            'backupCount': 2
        },
    },

    'loggers': {
        'celery.task': {
            'handlers': ['console', 'file',],
            'propagate': True,
            'level': 'DEBUG',
        },
        # add any other app loggers you want here
    },
}