Plugin Directory

Changeset 2639397


Ignore:
Timestamp:
12/04/2021 07:53:46 AM (4 years ago)
Author:
wkhayrattee
Message:

feat: new feature (see real-time output in log) + enhancement (prevent multiple run if a sync process is already running)

Location:
b2-sync/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • b2-sync/trunk/src/B2Sync_plugin_helper.php

    r2636570 r2639397  
    1818 * @throws \Exception
    1919 */
    20 function B2Sync_infologthis($message)
     20function B2Sync_logthis($message)
    2121{
    22     $log = new Logger('B2Sync_plugin_infolog');
    23     $stream = new StreamHandler(WP_CONTENT_DIR . B2Sync_DS . Enum::LOG_FILE_MESSAGE, Logger::INFO);
     22    $log = new Logger('B2Sync_LOG');
     23    $stream = new StreamHandler(WP_CONTENT_DIR . B2Sync_DS . Enum::LOG_FILE_ERROR, Logger::INFO);
    2424    $log->pushHandler($stream);
    2525    $log->info($message);
     
    2727    unset($stream);
    2828}
    29 function B2Sync_errorlogthis($message)
    30 {
    31     $log = new Logger('B2Sync_plugin_errorlog');
    32     $stream = new StreamHandler(WP_CONTENT_DIR . B2Sync_DS . Enum::LOG_FILE_ERROR, Logger::ERROR);
    33     $log->pushHandler($stream);
    34     $log->error($message);
    35     unset($log);
    36     unset($stream);
    37 }
  • b2-sync/trunk/src/Core/AdminLogPage.php

    r2636570 r2639397  
    8989    {
    9090        $log_file = $log_file_path;
    91         $max_lines = 10;
     91        $max_lines = 100;
    9292        $log_data = '';
    9393        $log_data_array = [];
  • b2-sync/trunk/src/Core/PluginClass.php

    r2636570 r2639397  
    138138        // Check for nonce security
    139139        if (!wp_verify_nonce($_POST['nonce'], 'ajax-nonce')) {
    140             B2Sync_errorlogthis('ajax nonce failed while sync button was clicked');
     140            B2Sync_logthis('[ERROR] ajax nonce failed while sync button was clicked');
    141141            wp_send_json_error('An error occurred');
    142142        } else {
    143             B2Sync_errorlogthis('Sync process started via ajax action button');
    144             $error_msg = Utils::doSync();
     143            $error_msg = Utils::doSync('manual_ajax_action');
    145144            wp_send_json_success('Sync process completed.');
    146145        }
  • b2-sync/trunk/src/Core/SyncClass.php

    r2636570 r2639397  
    5656        if (!Utils::notEmptyOrNull($this->key_id)) {
    5757            $error .= 'key_id empty ||';
    58             B2Sync_errorlogthis('[fields] KeyID cannot be empty!');
     58            B2Sync_logthis('[fields] KeyID cannot be empty!');
    5959        }
    6060        if (!Utils::notEmptyOrNull($this->application_key)) {
    6161            $error .= 'application_key empty ||';
    62             B2Sync_errorlogthis('[fields] ApplicationKey cannot be empty!');
     62            B2Sync_logthis('[fields] ApplicationKey cannot be empty!');
    6363        }
    6464        if (!Utils::notEmptyOrNull($this->bucket_name)) {
    6565            $error .= 'bucket_name empty ||';
    66             B2Sync_errorlogthis('[fields] BucketName cannot be empty!');
     66            B2Sync_logthis('[fields] BucketName cannot be empty!');
    6767        }
    6868
    6969        if (mb_strlen($error) > 0) {
    7070            $this->field_status = Enum::FIELD_STATUS_VALUE_OFF;
    71             B2Sync_errorlogthis('[WARNING] setting b2-sync to OFF - by rule, as some field(s) mentioned above is empty');
     71            B2Sync_logthis('[WARNING] setting b2-sync to OFF - by rule, as some field(s) mentioned above is empty');
    7272        }
    7373    }
     
    8282
    8383        if ($rclonePath == Enum::NOT_FOUND) {
    84             B2Sync_errorlogthis('RCLONE was not found on this server');
     84            B2Sync_logthis('RCLONE was not found on this server');
    8585
    8686            return false;
     
    8888
    8989        return true;
     90    }
     91
     92    /**
     93     * To check is there's any currently running rclone process
     94     *
     95     * @throws \Exception
     96     *
     97     * @return bool
     98     */
     99    public static function checkAnyCurrentRunningProcess()
     100    {
     101        $process_pgrep = new Process([
     102            'pgrep',
     103            '-f',
     104            'rclone',
     105        ]);
     106        $process_pgrep->run();
     107        $is_running = $process_pgrep->getOutput();
     108        unset($process_pgrep);
     109
     110        if ($is_running > 0) {
     111            return true;
     112        }
     113
     114        return false;
    90115    }
    91116
     
    108133            $remote_path = ':b2,account="' . $this->key_id . '",key="' . $this->application_key . '":' . $this->bucket_name . '/' . $this->uploads_folder_name;
    109134
    110             B2Sync_errorlogthis('Has started the syncing process..');
     135            B2Sync_logthis('[INFO] The syncing process has started..');
    111136            /*
    112137             * Command we are trying to execute on Bash:
     
    115140            $process = new Process([
    116141                'rclone',
    117                 '-q',
     142                '-v',
    118143                'sync',
    119144                $path_to_uploads,
     
    122147            $process->start();
    123148
    124             $item_count = 1;
    125             while ($process->isRunning()) {
    126                 //waiting for process to finish
    127                 B2Sync_errorlogthis('Still syncing.. Process check count->' . $item_count);
    128                 $item_count++;
    129             }
     149            /**
     150             * Getting real-time Process Output (from rclone)
     151             * Waits until the given anonymous function returns true
     152             */
     153            $process->waitUntil(function ($type, $output) {
     154                B2Sync_logthis('[rclone]: ' . $output);
    130155
    131             if ($process->isRunning() == false) {
    132                 B2Sync_errorlogthis('Syncing done!');
    133             }
     156                return $output === 'Ready. Waiting for commands...';
     157            });
    134158
    135159            if ($process->isSuccessful()) {
    136                 $output = $process->getOutput();
    137                 B2Sync_errorlogthis('Syncing seems to be successful!');
    138                 B2Sync_errorlogthis($output);
     160                B2Sync_logthis('[DONE] Syncing has completed and seems to be successful!');
    139161            } else {
    140                 B2Sync_errorlogthis('There seems to be an issue, see output below');
    141                 B2Sync_errorlogthis($process->getErrorOutput());
     162                B2Sync_logthis('[ERROR] There seems to be an issue, see output below');
     163                B2Sync_logthis($process->getErrorOutput());
    142164            }
    143165        }
  • b2-sync/trunk/src/Core/Utils.php

    r2636570 r2639397  
    9797    public static function doSync($action = 'action_button')
    9898    {
    99         //Clear log file before starting next sync
    100         $error_log_file = WP_CONTENT_DIR . B2Sync_DS . Enum::LOG_FILE_ERROR;
    101         AdminLogPage::clearErrorLog($error_log_file);
     99        $error_msg = '';
    102100
    103         $error_msg = '';
    104         B2Sync_errorlogthis('A Sync was triggered by action: ' . $action);
    105         if (SyncClass::checkRclone() === false) {
    106             $error_msg = 'WARNING: the software "rclone" does not seem to be present on your server, please ask your server admin to install it before using this plugin';
     101        /**
     102         * To check is there's any currently running rclone process
     103         */
     104        $is_running = SyncClass::checkAnyCurrentRunningProcess();
     105        if ($is_running === true) {
     106            B2Sync_logthis('[WARNING] You have invoked the sync more than once, Allow some time to let the current syncing complete!');
     107
     108            return false;
    107109        } else {
    108             $sync = new SyncClass();
    109             if ($sync->field_status == Enum::FIELD_STATUS_VALUE_OFF) {
    110                 $error_msg = 'Please enable the sync below in the dropdown or one of the field(s) is empty!';
     110            if (SyncClass::checkRclone() === false) {
     111                $error_msg = '[ERROR] the software "rclone" does not seem to be present on your server, please ask your server admin to install it before using this plugin';
    111112            } else {
    112                 $error_msg = 'Syncing has started, check the log on the sub-menu ' . Enum::ADMIN_LOG_MENU_TITLE . ' page';
    113                 $sync->start();
     113                B2Sync_logthis('[INFO] A Sync was triggered by action: ' . $action);
     114
     115                //Clear log file before starting next sync
     116                $error_log_file = WP_CONTENT_DIR . B2Sync_DS . Enum::LOG_FILE_ERROR;
     117                AdminLogPage::clearErrorLog($error_log_file);
     118
     119                $sync = new SyncClass();
     120                if ($sync->field_status == Enum::FIELD_STATUS_VALUE_OFF) {
     121                    $error_msg = '[ERROR] Please enable the sync below in the dropdown or one of the field(s) is empty!';
     122                } else {
     123                    $error_msg = '[INFO] Syncing has started, check the log on the sub-menu ' . Enum::ADMIN_LOG_MENU_TITLE . ' page';
     124                    $sync->start();
     125                }
    114126            }
     127
     128            return $error_msg;
    115129        }
    116 
    117         return $error_msg;
    118130    }
    119131
  • b2-sync/trunk/views/admin/page_log.twig

    r2636570 r2639397  
    55        <table class="form-table">
    66            <tr>
    7                 <td>(Only the latest 10 entries will be displayed - pay attention to the DATE!)</td>
     7                <td>(<u>Only the latest 100 entries will be displayed</u> - pay attention to the DATE!)</td>
    88            </tr>
    99            <tr>
Note: See TracChangeset for help on using the changeset viewer.