Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions includes/Framework/BatchLogHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* @package FacebookCommerce
*/

namespace WooCommerce\Facebook\Framework;

use WC_Facebookcommerce_Utils;
use WooCommerce\Facebook\Utilities\Heartbeat;

defined( 'ABSPATH' ) || exit;


/**
* The BatchLog handler.
*
* @since 3.5.0
*/
class BatchLogHandler {

/**
* Constructs a new BatchLog handler.
*
* @since 3.5.0
*/
public function __construct() {
add_action( Heartbeat::EVERY_5_MINUTES, array( $this, 'process_telemetry_logs_batch' ) );
}

/**
* Function that runs every minute.
*
* @internal
*
* @since 3.5.0
*/
public function process_telemetry_logs_batch() {
if ( get_transient( 'global_telemetry_message_queue' ) !== false ) {
$logs = get_transient( 'global_telemetry_message_queue' );

// TODO: Replace with send batch logging request to Meta function.
WC_Facebookcommerce_Utils::log( wp_json_encode( $logs ) );
}

set_transient( 'global_telemetry_message_queue', [], HOUR_IN_SECONDS );
}
}
15 changes: 15 additions & 0 deletions includes/Framework/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ abstract class Plugin {
/** @var AdminNoticeHandler the admin notice handler class */
private $admin_notice_handler;

/** @var BatchLogHandler the batch log handler class */
private $batch_log_handler;


/**
* Initialize the plugin.
Expand Down Expand Up @@ -118,6 +121,9 @@ public function __construct( $id, $version, $args = [] ) {

// add the action & filter hooks
$this->add_hooks();

// build the batch log handler instance
$this->init_batch_log_handler();
}


Expand Down Expand Up @@ -176,6 +182,15 @@ protected function init_lifecycle_handler() {
$this->lifecycle_handler = new \WooCommerce\Facebook\Lifecycle( $this );
}

/**
* Builds the batch log handler instance.
*
* @since 3.5.0
*/
protected function init_batch_log_handler() {
$this->batch_log_handler = new BatchLogHandler();
}


/**
* Adds the action & filter hooks.
Expand Down
40 changes: 40 additions & 0 deletions includes/Utilities/Heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
class Heartbeat {

/**
* Hook name for hourly heartbeat.
*/
const EVERY_5_MINUTES = 'facebook_for_woocommerce_5_minute_heartbeat';

/**
* Hook name for hourly heartbeat.
*/
Expand All @@ -36,6 +41,11 @@ class Heartbeat {
*/
protected $daily_cron_name = 'facebook_for_woocommerce_daily_heartbeat_cron';

/**
* @var string
*/
protected $every_5_minute_cron_name = 'facebook_for_woocommerce_5_minute_heartbeat_cron';

/**
* @var WC_Queue_Interface
*/
Expand All @@ -54,9 +64,11 @@ public function __construct( WC_Queue_Interface $queue ) {
* Add hooks.
*/
public function init() {
add_filter( 'cron_schedules', array( $this, 'five_minutes_cron_schedules' ) );
add_action( 'init', array( $this, 'schedule_cron_events' ) );
add_action( $this->hourly_cron_name, array( $this, 'schedule_hourly_action' ) );
add_action( $this->daily_cron_name, array( $this, 'schedule_daily_action' ) );
add_action( $this->every_5_minute_cron_name, array( $this, 'schedule_every_5_minute_action' ) );
}

/**
Expand All @@ -72,6 +84,27 @@ public function schedule_cron_events() {
if ( ! wp_next_scheduled( $this->daily_cron_name ) ) {
wp_schedule_event( time(), 'daily', $this->daily_cron_name );
}
if ( ! wp_next_scheduled( $this->every_5_minute_cron_name ) ) {
wp_schedule_event( time(), 'five_minutes', $this->every_5_minute_cron_name );
}
}

/**
* Function that add a defination of interval for cron job
*
* @param string $schedules pluin system data
*
* @since 3.5.0
*
* @internal
*/
public function five_minutes_cron_schedules( $schedules ) {
$schedules['five_minutes'] = array(
'interval' => 300,
'display' => __( 'Five Minutes', 'facebook-for-woocommerce' ),
);

return $schedules;
}

/**
Expand All @@ -90,4 +123,11 @@ public function schedule_hourly_action() {
public function schedule_daily_action() {
$this->queue->add( self::DAILY );
}

/**
* Schedule the every 5 minute heartbeat action to run immediately.
*/
public function schedule_every_5_minute_action() {
$this->queue->add( self::EVERY_5_MINUTES );
}
}
13 changes: 8 additions & 5 deletions includes/fbutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ public static function prepare_product_variation_data_items_batch( $product ) {

/**
* Utility function for sending exception logs to Meta.
* @since 3.5.0
*/
public static function logExceptionImmediatelyToMeta(Throwable $error, array $context = []) {
/**
Expand All @@ -876,18 +877,20 @@ public static function logExceptionImmediatelyToMeta(Throwable $error, array $co

/**
* Utility function for sending telemetry logs to Meta.
* @since 3.5.0
*/
public static function logTelemetryToMeta(string $message, array $context = []) {
/**
* WIP: This is a dummy function to send telemetry logs to Meta.
* $context is an array of data that will be sent to Meta, includes commerce_merchant_settings_id,
* catalog_id, order_id, promotion_id, flow_name, flow_step, extra_data and etc.
*/

// TODO: Implement push logging request to global message queue function.
$response = null;

return $response;

// Push logging request to global message queue function.
$context['extra_data'] = ['message' => $message];
$logs = get_transient( 'global_telemetry_message_queue' );
$logs[] = $context;
set_transient( 'global_telemetry_message_queue', $logs, HOUR_IN_SECONDS );
}

/**
Expand Down