Giter Site home page Giter Site logo

Comments (5)

barryhughes avatar barryhughes commented on June 6, 2024 2

It would be nice if this library introduced it's own DOING_ACTION_SCHEDULER or something similar.

Re-opening so we don't lose sight of this idea (a little unsure if defining a constant is the best approach for us, but the general idea makes sense).

from action-scheduler.

barryhughes avatar barryhughes commented on June 6, 2024

Hi!

However, it seems that wp_doing_cron() does not always return true when running "Action Scheduler" tasks.

That's right. By default, there are up to three different contexts through which Action Scheduler may be triggered:

  • WP Cron
  • Async HTTP requests
  • WP CLI

In most cases, it's a mix of the first two: it doesn't always run via WP Cron (and so DOING_CRON will not always be set).

However, we wonder whether this is wise and/or correct, so we are curious about the opinion of the developers of this library.

If you are finding you need something like this, then possibly? Can I just highlight though that we already have some related functionality within the library:

https://github.com/woocommerce/action-scheduler/blob/3.7.1/classes/ActionScheduler_Compatibility.php#L87-L104

This is generally called immediately prior to processing waiting actions. However, there isn't really any way to ensure this takes effect: restrictions in a given hosting environment take precedence, in many cases.

from action-scheduler.

remcotolsma avatar remcotolsma commented on June 6, 2024

Thanks @barryhughes, good to know about these 3 contexts. I think the PHP timeouts you were referring to are different from the WordPress HTTP API timeouts. WordPress uses a standard timeout of 5 seconds for HTTP requests: https://developer.wordpress.org/reference/classes/wp_http/request/#parameters. For HTTP requests that are executed via background tasks, this may be slightly higher. However, there is currently no easy way to check whether a script is running in one of the 3 contexts mentioned? For the WordPress cron wp_doing_cron() for WP-CLI defined( 'WP_CLI' ) && WP_CLI and for the Action Scheduler "Async HTTP requests"?

from action-scheduler.

barryhughes avatar barryhughes commented on June 6, 2024

I'm sorry, I misunderstood.

However, there is currently no easy way to check whether a script is running in one of the 3 contexts mentioned?

I'm not sure how useful this would be in your case, but the action_scheduler_begin_execute hook is triggered immediately before each scheduled action is fetched and executed, and supplies two parameters to its callbacks:

  • $action_id
  • $context

The second of those provides the information you are looking for (and even without it, you could still use the action itself as a signal that you are 'inside' the scheduled action queue).

Might that work? If not, we could explore alternatives. For instance, it would seem reasonable to also share the context via the action_scheduler_before_process_queue action hook, and there are probably various other possibilities.

from action-scheduler.

remcotolsma avatar remcotolsma commented on June 6, 2024

Thanks, for now we handled it this way:

It would be nice if this library introduced it's own DOING_ACTION_SCHEDULER or something similar.

from action-scheduler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.