WordPress: Cron locking and Queue

Simple Cronjob Walker

  1. Create a CPT cronjob for your cronjobs.
  2. Add the relevant post meta data (action, last_executed,… and whatever you need)
  3. Create a walker function

    function my_cronjob_walker() {
        $args= array(
            'post_type' => 'cronjob',
            'posts_per_page' => 1,
            'meta_key' => 'last_executed'
            'orderby' => 'meta_value',
        );
        $cronjob = new WP_Query($args);
        if ($cronjob->have_posts()) while ($cronjob->have_posts()) {
            $cronjob->the_post();
            // do your cronjob stuff here
            do_action(get_post_meta(get_the_ID(), 'action', true);
            update_post_meta(get_the_ID(), 'last_executed', date('c'));
        }
    }
    if ( ! wp_next_scheduled( 'do_cronjob_walker' ) ) {
        wp_schedule_event( time(), 'hourly', 'do_cronjob_walker' );
    }
    
    add_action( 'do_cronjob_walker', 'my_cronjob_walker' );