Wordpress cron job running more than once

I developed a plugin to read some XML files and publish their itens as post but for some reason, sometimes, it is duplicating posts. Basically that is what I have:

// function one is scheduled (hourly) and unscheduled on plugin activation and deactivation hooks
register_activation_hook(__FILE__, 'function_one_activation');
register_deactivation_hook(__FILE__, 'function_one_deactivate');

function function_one_activation() {
    if (!wp_next_scheduled('function_one_cron')) {
        wp_schedule_event(time(), 'hourly', 'function_one_cron');
    }
}

function function_one_deactivate() {
    $timestamp = wp_next_scheduled('function_one_cron');
    wp_unschedule_event($timestamp, 'function_one_cron');
}

add_action('function_one_cron', 'one');
add_action('function_two_cron', 'two');
add_action('function_three_cron', 'three');

function one() {
    // download XML files using CURL and save on server
    // schedule cron to run function two after 60 seconds
    if(!wp_next_scheduled('function_two_cron')) {
        wp_schedule_single_event(time() + 60, 'function_two_cron');
    }
}

function two() {
    // get an array from DB of posts saved from XML files
    // get and run each file and check if item is new (is not on array above) and save it as post, if already posted remove it ID from array
    // save the new array
    // schedule cron to run function three after 60 seconds
    if(!wp_next_scheduled('function_three_cron')) {
        wp_schedule_single_event(time() + 60, 'function_three_cron');
    }
}

function three() {
    // get the remain ID from array above and delete posts
}

Does anyone know what I am missing that makes it runs twice sometimes or how can I prevent it to happens?

Topic wp-cron cron plugin-development plugins Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.