Marque si la función es llamada por el trabajo cron

10

Tengo una función que solo quiero ejecutar a través de un trabajo cron. ¿Hay alguna manera de verificar que un evento programado particular esté llamando a esta función y no a otra cosa?

    
pregunta urok93 05.09.2012 - 11:33

3 respuestas

14

WordPress tiene un DOING_CRON constante que nos ayuda a saber si estamos haciendo los trabajos cron o no. Se define en el archivo wp-cron.php .

Por lo tanto, puede verificar esta constante en su código:

if ( defined( 'DOING_CRON' ) )
{
    // Do something
}
    
respondido por el Anh Tran 07.09.2012 - 17:20
2

Solo eche un vistazo al »inspector de API Cron« , que escribí para question # 18017 . El complemento crea una tabla que muestra el shutdown -hook / el final de la página.

Utiliza la función _get_cron_array() para recuperar todo lo registrado & Acciones programadas. Otra función útil es wp_get_schedules() . La tercera forma es llamar directamente a los datos subyacentes para _get_cron_array() , llamando a get_option( 'cron' ); . Aún es mejor usar las funciones API predeterminadas desde el núcleo de WP.

Si desea saber si está actualmente en un trabajo de Cron, puede verificar defined( 'DOING_CRON' ) AND DOING_CRON .

    
respondido por el kaiser 07.09.2012 - 17:01
1

No estoy desarrollando estudios (solo soy un entusiasta) pero creo que puedes agregar un add_action al evento

Es solo un currículum ...

//to the event
if(your_condition)
{
    add_action('original_event_to_hook', 'your_scheduled');
}

function your_scheduled()
{
    //create a param here
    //And shedule your function with arg
    wp_schedule_event(time(), 'hourly', 'your_function', array('param_1' => value));
}

function your_function($args){

   $verification = $args['param_1'];
   if($verification)
   {
      //OK
      //Eventually delete this schedule with this specific arg
   }
}

Para recuperar una lista de tu cron "tu_función" donde tengas este argumento

     //Get a filtered list of cron hooks 
        function kw_filter_crons_hooks($hooks = false, $args = false)
        {
            $crons = get_option('cron');

            if (!$crons)
            {
                $crons[0] = NULL;
            }

            $filter = array();
            $cronlist = array();
            $schedule = array();

            foreach($crons as $timestamp => $cron)
            {
                //@param $hooks = string 'schedule'
                //@param $args = false
                //Return an array of cron task sheduled
                $schedule[] = $cron;

                if(!$schedule && $hooks == 'schedule' && $args == false)
                {
                    $schedule[0] = NULL;
                }

                foreach($hooks as $hook)
                {
                    if(isset($cron[$hook]))
                    {
                        //@param $hooks = array($hook);
                        //@param $args = false
                        //Return an array of cron task sheduled
                        $cronlist[] = $cron;

                        if(!$cronlist && is_array($hooks) && $args == false)
                        {
                            $cronlist[0] = NULL;
                        }

                        if(!empty($args))
                        {
                            foreach($cronlist as $key => $value)
                            {
                                foreach($value as $k => $v)
                                {
                                    foreach($v as $x => $y)
                                    {
                                        foreach($args as $arg => $val)
                                        {
                                            if ($y['args'][$arg] == $val)
                                            {
                                                //@param $hooks = array($hook);
                                                //@param $args = array($arg);
                                                //Return an array of cron task specified filtered by arg
                                                $filter[$x] = $y;
                                                if(!$filter && is_array($hooks) && is_array($args))
                                                {
                                                    $filter[0] = NULL;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }


            if(is_array($hooks) && $args === false && $cronlist)
            {
                return $cronlist;
            }
            else if(is_array($hooks) && is_array($args) && $filter)
            {
                return $filter;
            }
            else if($hooks === 'schedule' && $args === false && $schedule)
            {
                return $schedule;
            }
            else if($hooks === false && $args === false && $crons)
            {
                return $crons;
            }
            else
            {
                return false;
            }
        }

//Usage
    $cron_filtered = kw_filter_crons_hooks(array('your_function'), array('param_1' => value));
    var_dump($cron_filtered);
    
respondido por el MonkeyTime 08.09.2012 - 16:42

Lea otras preguntas en las etiquetas