processone / ejabberd-contrib

Growing and curated ejabberd contributions repository - PR or ask to join !
http://ejabberd.im
248 stars 137 forks source link

Question: mod_cron - schedule mysql mam purge #307

Closed sando38 closed 2 years ago

sando38 commented 2 years ago

Hello ejabberd frieds,

just a quick question: I am running ejabberd with a mariaDB instance and within kubernetes. Currently, I have a cron-job scheduled on the host-machine deleting mam archive for messages older than 30 days with

bin/ejabberdctl delete-old-mam-messages all 30

I understand that mod_cron calls the different modules of ejabberd, can the module also call the API like this:

modules:
  mod_cron:
    tasks:
      - time: 90
        units: minutes
        module: api
        function: ejabberdctl
        arguments: 
          - "delete-old-mam-messages"
          - "all"
          - "30"
        timer_type: fixed

Thanks in advance and wish you a great weekend!

badlop commented 2 years ago

Now that you mention, it is not possible to use API commands, and it is not possible to call functions when have mixed argument types. So, right now I see no way to use mod_cron for that.

I'll take a look the next days and ping you with a solution.

By the way, a new set of commands have been added right now: delete_old_mam_messages_batch, ... See https://github.com/processone/ejabberd/commit/b86fe14ef098212a1ed33f333f739888a7b9c038

sando38 commented 2 years ago

Thanks for the quick feedback and for having a look into it! Looking forward to it.

badlop commented 2 years ago

I've improved mod_cron, now there are three ways to purge mam messages!

I have also improved the mod_cron documentation: https://github.com/processone/ejabberd-contrib/tree/master/mod_cron

Those two methods should work correctly with ejabberd 21.12 and some older versions:

modules:
  mod_cron:
    tasks:
      - time: 15
        units: seconds
        ctl: delete_old_mam_messages
        arguments:
          - "all"
          - "0"

      - time: 20
        units: seconds
        module: mod_mam
        function: delete_old_messages
        arguments:
          - "all"
          - 0

This requires using ejabberd from git:

modules:
  mod_cron:
    tasks:
      - time: 10
        units: seconds
        command: delete_old_mam_messages # requires a recent ejabberd git
        arguments:
          - "all"
          - 0
sando38 commented 2 years ago

Nice, it works as expected. I tested the following config with ejabberd 21.12:

modules:
  mod_cron:
    tasks:
      - time: 60
        units: minutes
        timer_type: interval
        ctl: delete_old_mam_messages
        arguments:
          - "all"
          - "30"

I did not test the current ejabberd git version, no time to rebuild the docker image in the moment.

Since this is a very smooth solution, easy to use for people and with expanded administrative operations with the "API" extension, you may consider to include mod_cron into the core modules shipped directly with ejabberd builts like with your new 22.xx ejabberd version?

Thanks again for your work and this quick and easy update!