WP Pipes: Cronjob

I use the awesome WP Pipes plugin for importing RSS feed items to posts (including content extraction, duplicate detection):

For running the pipe periodically, I have to set up a cronjob.
Is there a best practice approach for setting up a cronjob in Bedrock/Trellis site?
Should the cronjob be added to site config or to Trellis ansible config?

If you’re using Trellis then I’d suggest just adding a new simple role to define a cronjob like this: https://github.com/roots/trellis/blob/a36eb264024b6eb2e34b863509020dee9de64631/roles/wordpress-setup/tasks/main.yml#L45-L53

You can copy that and just change the command to whatever pipes needs: wp cron event run --due-now > /dev/null 2>&1

disable_wp_cron is set to true by default in trellis.
So the consensus is that WordPress cron should be disabled and real cron be used?
WordPress cron seems to emulate real cron by checking date+time for each request by user, so a visitor has to actually “pump” the cronjobs by requesting from the WordPress site.
In task Setup WP system cron trellis sets up a real cronjob that let the WordPress site check for due cronjobs? Do I have to align these real cronjobs to all the internal WordPress site jobs so they are run in a timely manner?

Yes although we haven’t thought too much about it lately. WP Cron can cause performance issues and if you have real cron, you might as well use it? (which all Trellis sites do)

Do I have to align these real cronjobs to all the internal WordPress site jobs so they are run in a timely manner?

I’m not really sure what you mean by this. Are there more cronjobs to run? The frequency is up to you.

With aligning I mean that there could be cronjobs that should run each hour and some that should run each day. But the system cronjob invokes one WordPress cron script, so it makes sense to create one system cron with the smallest interval (here each hour)?

Would it be a good idea to add a dictionary for cronjobs for the Trellis wordpress_sites?

So I add a role for custom stuff with sties, like a particular cron job.

Now the system cron job, trellis sets up, uses the wordpress_sites dictionary, which is great:

- name: Setup WP system cron
  cron:
    name: "{{ item.key }} WordPress cron"
    minute: "*/15"
    user: "{{ web_user }}"
    job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }} && wp cron event run --due-now > /dev/null 2>&1"
    cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
    state: "{{ (cron_enabled and not item.value.multisite.enabled) | ternary('present', 'absent') }}"
  with_dict: "{{ wordpress_sites }}"

How can I access one particular site in wordpress_sites, the one for which I want to set up the cron job, so I can use the proper paths to the site PHP script and toggle state (absent when the wordpress site is absent, present when the wordpress site is present)?

Thanks!

You just need to add a when condition for sites that have the custom property defined:

- name: Setup WP system cron
  cron:
    name: "{{ item.key }} WordPress cron"
    minute: "*/15"
    user: "{{ web_user }}"
    job: "cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default('current') }} && {{ item.value.cron.job }}"
    cron_file: "wordpress-{{ item.key | replace('.', '_') }}"
    state: "{{ (cron_enabled and not item.value.multisite.enabled) | ternary('present', 'absent') }}"
  with_dict: "{{ wordpress_sites }}"
  when: item.value.cron is defined

Assuming you’ve defined a site like:

wordpress_sites:
  example.com:
    site_hosts:
      - canonical: k-thx.com
        redirects:
          - www.k-thx.com
    local_path: ../site # path targeting local Bedrock site directory (relative to Ansible root)
    admin_email: admin@example.test
    multisite:
      enabled: false
    ssl:
      enabled: true
      provider: letsencrypt
    cache:
      enabled: false
    cron:
      job: "some command to run in the cron"
2 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.