File: /var/www/vhosts/uyarreklam.com.tr/httpdocs/ActionScheduler.tar
ActionScheduler.php 0000644 00000014733 15154401720 0010340 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler;
use ActionScheduler as ActionSchedulerCore;
use ActionScheduler_Action;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
defined( 'ABSPATH' ) || exit;
/**
* ActionScheduler service class.
*
* Acts as a wrapper for ActionScheduler's public functions.
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler
*/
class ActionScheduler implements ActionSchedulerInterface, Service {
use PluginHelper;
/**
* @var AsyncActionRunner
*/
protected $async_runner;
/**
* ActionScheduler constructor.
*
* @param AsyncActionRunner $async_runner
*/
public function __construct( AsyncActionRunner $async_runner ) {
$this->async_runner = $async_runner;
}
/**
* Schedule an action to run once at some time in the future
*
* @param int $timestamp When the job will run.
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_single( int $timestamp, string $hook, $args = [] ): int {
return as_schedule_single_action( $timestamp, $hook, $args, $this->get_slug() );
}
/**
* Schedule an action to run now i.e. in the next available batch.
*
* This differs from async actions by having a scheduled time rather than being set for '0000-00-00 00:00:00'.
* We could use an async action instead but they can't be viewed easily in the admin area
* because the table is sorted by schedule date.
*
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_immediate( string $hook, $args = [] ): int {
return as_schedule_single_action( gmdate( 'U' ) - 1, $hook, $args, $this->get_slug() );
}
/**
* Schedule a recurring action to run now (i.e. in the next available batch), and in the given intervals.
*
* @param int $timestamp When the job will run.
* @param int $interval_in_seconds How long to wait between runs.
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_recurring( int $timestamp, int $interval_in_seconds, string $hook, $args = [] ): int {
return as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args, $this->get_slug() );
}
/**
* Schedule an action that recurs on a cron-like schedule.
*
* @param int $timestamp The first instance of the action will be scheduled to run at a time
* calculated after this timestamp matching the cron expression. This
* can be used to delay the first instance of the action.
* @param string $schedule A cron-link schedule string
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*
* @see https://en.wikipedia.org/wiki/Cron
* * * * * * *
* ┬ ┬ ┬ ┬ ┬ ┬
* | | | | | |
* | | | | | + year [optional]
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
* | | | +---------- month (1 - 12)
* | | +--------------- day of month (1 - 31)
* | +-------------------- hour (0 - 23)
* +------------------------- min (0 - 59)
*/
public function schedule_cron( int $timestamp, string $schedule, string $hook, $args = [] ): int {
return as_schedule_cron_action( $timestamp, $schedule, $hook, $args, $this->get_slug() );
}
/**
* Enqueue an action to run one time, as soon as possible
*
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function enqueue_async_action( string $hook, $args = [] ): int {
$this->async_runner->attach_shutdown_hook();
return $this->schedule_immediate( $hook, $args );
}
/**
* Check if there is an existing action in the queue with a given hook and args combination.
*
* An action in the queue could be pending, in-progress or async. If the action is pending for a time in
* future, currently being run, or an async action sitting in the queue waiting to be processed, boolean
* true will be returned. Or there may be no async, in-progress or pending action for this hook, in which
* case, boolean false will be the return value.
*
* @param string $hook
* @param array|null $args
*
* @return bool True if there is a pending scheduled, async or in-progress action in the queue or false if there is no matching action.
*/
public function has_scheduled_action( string $hook, $args = [] ): bool {
return ( false !== as_next_scheduled_action( $hook, $args, $this->get_slug() ) );
}
/**
* Search for scheduled actions.
*
* @param array|null $args See as_get_scheduled_actions() for possible arguments.
* @param string $return_format OBJECT, ARRAY_A, or ids.
*
* @return array
*/
public function search( $args = [], $return_format = OBJECT ): array {
$args['group'] = $this->get_slug();
return as_get_scheduled_actions( $args, $return_format );
}
/**
* Cancel the next scheduled instance of an action with a matching hook (and optionally matching args).
*
* Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action.
*
* @param string $hook The hook that the job will trigger.
* @param array|null $args Args that would have been passed to the job.
*
* @return int The scheduled action ID if a scheduled action was found.
*
* @throws ActionSchedulerException If no matching action found.
*/
public function cancel( string $hook, $args = [] ) {
$action_id = as_unschedule_action( $hook, $args, $this->get_slug() );
if ( null === $action_id ) {
throw ActionSchedulerException::action_not_found( $hook );
}
return $action_id;
}
/**
* Retrieve an action.
*
* @param int $action_id Action ID.
*
* @return ActionScheduler_Action
*
* @since 1.7.0
*/
public function fetch_action( int $action_id ): ActionScheduler_Action {
return ActionSchedulerCore::store()->fetch_action( $action_id );
}
}
ActionSchedulerException.php 0000644 00000001567 15154401720 0012220 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\GoogleListingsAndAdsException;
use LogicException;
defined( 'ABSPATH' ) || exit;
/**
* Class ActionSchedulerException
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler
*/
class ActionSchedulerException extends LogicException implements GoogleListingsAndAdsException {
/**
* Create a new exception instance for when a job item is not found.
*
* @param string $action Action name
*
* @return ActionSchedulerException
*/
public static function action_not_found( string $action ): ActionSchedulerException {
return new static(
sprintf(
/* translators: %s: the action name */
__( 'No action matching %s was found.', 'google-listings-and-ads' ),
$action
)
);
}
}
ActionSchedulerInterface.php 0000644 00000012431 15154401720 0012152 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler;
use ActionScheduler_Action;
defined( 'ABSPATH' ) || exit;
/**
* Interface ActionSchedulerInterface
*
* Acts as a wrapper for ActionScheduler's public functions.
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler
*/
interface ActionSchedulerInterface {
public const STATUS_COMPLETE = 'complete';
public const STATUS_PENDING = 'pending';
public const STATUS_RUNNING = 'in-progress';
public const STATUS_FAILED = 'failed';
public const STATUS_CANCELED = 'canceled';
/**
* Schedule an action to run once at some time in the future
*
* @param int $timestamp When the job will run.
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_single( int $timestamp, string $hook, $args = [] ): int;
/**
* Schedule an action to run now i.e. in the next available batch.
*
* This differs from async actions by having a scheduled time rather than being set for '0000-00-00 00:00:00'.
* We could use an async action instead but they can't be viewed easily in the admin area
* because the table is sorted by schedule date.
*
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_immediate( string $hook, $args = [] ): int;
/**
* Schedule a recurring action to run now (i.e. in the next available batch), and in the given intervals.
*
* @param int $timestamp When the job will run.
* @param int $interval_in_seconds How long to wait between runs.
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function schedule_recurring( int $timestamp, int $interval_in_seconds, string $hook, $args = [] ): int;
/**
* Schedule an action that recurs on a cron-like schedule.
*
* @param int $timestamp The first instance of the action will be scheduled to run at a time
* calculated after this timestamp matching the cron expression. This
* can be used to delay the first instance of the action.
* @param string $schedule A cron-link schedule string
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*
* @see https://en.wikipedia.org/wiki/Cron
* * * * * * *
* ┬ ┬ ┬ ┬ ┬ ┬
* | | | | | |
* | | | | | + year [optional]
* | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
* | | | +---------- month (1 - 12)
* | | +--------------- day of month (1 - 31)
* | +-------------------- hour (0 - 23)
* +------------------------- min (0 - 59)
*/
public function schedule_cron( int $timestamp, string $schedule, string $hook, $args = [] ): int;
/**
* Enqueue an action to run one time, as soon as possible
*
* @param string $hook The hook to trigger.
* @param array|null $args Arguments to pass when the hook triggers.
*
* @return int The action ID.
*/
public function enqueue_async_action( string $hook, $args = [] ): int;
/**
* Check if there is an existing action in the queue with a given hook and args combination.
*
* An action in the queue could be pending, in-progress or async. If the action is pending for a time in
* future, currently being run, or an async action sitting in the queue waiting to be processed, boolean
* true will be returned. Or there may be no async, in-progress or pending action for this hook, in which
* case, boolean false will be the return value.
*
* @param string $hook
* @param array|null $args
*
* @return bool True if there is a pending scheduled, async or in-progress action in the queue or false if there is no matching action.
*/
public function has_scheduled_action( string $hook, $args = [] ): bool;
/**
* Search for scheduled actions.
*
* @param array|null $args See as_get_scheduled_actions() for possible arguments.
* @param string $return_format OBJECT, ARRAY_A, or ids.
*
* @return array
*/
public function search( $args = [], $return_format = OBJECT ): array;
/**
* Cancel the next scheduled instance of an action with a matching hook (and optionally matching args).
*
* Any recurring actions with a matching hook should also be cancelled, not just the next scheduled action.
*
* @param string $hook The hook that the job will trigger.
* @param array|null $args Args that would have been passed to the job.
*
* @return int The scheduled action ID if a scheduled action was found.
*
* @throws ActionSchedulerException If no matching action found.
*/
public function cancel( string $hook, $args = [] );
/**
* Retrieve an action.
*
* @param int $action_id Action ID.
*
* @return ActionScheduler_Action
*
* @since 1.7.0
*/
public function fetch_action( int $action_id ): ActionScheduler_Action;
}
AsyncActionRunner.php 0000644 00000004353 15154401720 0010666 0 ustar 00 <?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler;
use ActionScheduler_AsyncRequest_QueueRunner as QueueRunnerAsyncRequest;
use ActionScheduler_Lock;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
defined( 'ABSPATH' ) || exit;
/**
* Class AsyncActionRunner
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler
*/
class AsyncActionRunner implements Service {
/**
* Whether the shutdown hook has been attached.
*
* @var bool
*/
protected $has_attached_shutdown_hook = false;
/**
* @var QueueRunnerAsyncRequest
*/
protected $async_request;
/**
* @var ActionScheduler_Lock
*/
protected $locker;
/**
* AsyncActionRunner constructor.
*
* @param QueueRunnerAsyncRequest $async_request
* @param ActionScheduler_Lock $locker
*/
public function __construct( QueueRunnerAsyncRequest $async_request, ActionScheduler_Lock $locker ) {
$this->async_request = $async_request;
$this->locker = $locker;
}
/**
* Attach async runner shutdown hook before ActionScheduler shutdown hook.
*
* The shutdown hook should only be attached if an async event has been created in the current request.
* The hook is only attached if it hasn't already been attached.
*
* @see ActionScheduler_QueueRunner::hook_dispatch_async_request
*/
public function attach_shutdown_hook() {
if ( $this->has_attached_shutdown_hook ) {
return;
}
$this->has_attached_shutdown_hook = true;
add_action( 'shutdown', [ $this, 'maybe_dispatch_async_request' ], 9 );
}
/**
* Dispatches an async queue runner request if various conditions are met.
*
* Note: This is a temporary solution. In the future (probably ActionScheduler 3.2) we should use the filter
* added in https://github.com/woocommerce/action-scheduler/pull/628.
*/
public function maybe_dispatch_async_request() {
if ( is_admin() ) {
// ActionScheduler will dispatch an async runner request on it's own.
return;
}
if ( $this->locker->is_locked( 'async-request-runner' ) ) {
// An async runner request has already occurred in the last 60 seconds.
return;
}
$this->locker->set( 'async-request-runner' );
$this->async_request->maybe_dispatch();
}
}