Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from ckeditor/t/7
Browse files Browse the repository at this point in the history
Feature: Introduced the observable `Watchdog#state` property. Introduced the `minimumNonErrorTimePeriod` configuration which defaults to 5 seconds and will be used to prevent infinite restart loops while allowing a larger number of random crashes as long as they do not happen too often. Renamed `waitingTime` configuration option to `saveInterval`. Closes #7. Closes #15.

BREAKING CHANGE: Renamed `waitingTime` configuration option to `saveInterval`.
  • Loading branch information
Piotr Jasiun authored Aug 2, 2019
2 parents 69aef8b + e85adca commit 5bdbfe5
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 83 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Internal changes only (updated dependencies, documentation, etc.).

## [10.0.0](https://github.com/ckeditor/ckeditor5-watchdog/tree/v10.0.0) (2019-07-04)

The initial font feature implementation.
The initial watchdog feature implementation.
40 changes: 38 additions & 2 deletions docs/features/watchdog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ watchdog.create( document.querySelector( '#editor' ), {
In other words, your goal is to create a watchdog instance and make the watchdog create an instance of the editor you want to use. Watchdog will then create a new editor and if it ever crashes restart it by creating a new editor.

<info-box>
A new editor instance is created every time the watchdog detects a crash. Thus, the editor instance should not be kept in your application's state. Use the {@link module:watchdog/watchdog~Watchdog#editor Watchdog#editor`} property instead.
A new editor instance is created every time the watchdog detects a crash. Thus, the editor instance should not be kept in your application's state. Use the {@link module:watchdog/watchdog~Watchdog#editor `Watchdog#editor`} property instead.

It also means that any code that should be executed for any new editor instance should be either loaded as an editor plugin or executed in the callbacks defined by {@link module:watchdog/watchdog~Watchdog#setCreator `Watchdog#setCreator()`} and {@link module:watchdog/watchdog~Watchdog#setDestructor `Watchdog#setDestructor()`}. Read more about controlling editor creation/destruction in the next section.
</info-box>
Expand Down Expand Up @@ -85,7 +85,7 @@ watchdog.create( elementOrData, editorConfig );

Other useful {@link module:watchdog/watchdog~Watchdog methods, properties and events}:

```js
```js
watchdog.on( 'error', () => { console.log( 'Editor crashed.' ) } );
watchdog.on( 'restart', () => { console.log( 'Editor was restarted.' ) } );

Expand All @@ -95,9 +95,45 @@ watchdog.destroy();
// The current editor instance.
watchdog.editor;

// The current state of the editor.
// The editor might be in one of the following states:
// * `initializing` - before the first initialization, and after crashes, before the editor is ready,
// * `ready` - a state when a user can interact with the editor,
// * `crashed` - a state when an error occurs - it quickly changes to `initializing` or `crashedPermanently` depending on how many and how frequency errors have been caught recently,
// * `crashedPermanently` - a state when the watchdog stops reacting to errors and keeps the editor crashed,
// * `destroyed` - a state when the editor is manually destroyed by the user after calling `watchdog.destroy()`.
// This property is observable.
watchdog.state;

// Listen to state changes.
watchdog.on( 'change:state' ( evt, name, currentState, prevState ) => {
console.log( `State changed from ${ currentState } to ${ prevState }` );

if ( currentState === 'crashedPermanently' ) {
watchdog.editor.isReadOnly = true;
}
} );

// An array of editor crashes info.
watchdog.crashes.forEach( crashInfo => console.log( crashInfo ) );
```

### Configuration

Both, the {@link module:watchdog/watchdog~Watchdog#constructor `Watchdog#constructor`} and the {@link module:watchdog/watchdog~Watchdog.for `Watchdog.for`} methods accept a {{@link module:watchdog/watchdog~WatchdogConfig configuration object} with the following optional properties:

* `crashNumberLimit` - A threshold specifying the number of editor errors (defaults to `3`). After this limit is reached and the time between last errors is shorter than `minimumNonErrorTimePeriod` the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
* `minimumNonErrorTimePeriod` - An average amount of milliseconds between last editor errors (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
* `saveInterval` - A minimum number of milliseconds between saving editor data internally (defaults to 5000). Note that for large documents this might have an impact on the editor performance.

```js
const watchdog = new Watchdog( {
minimumNonErrorTimePeriod: 2000,
crashNumberLimit: 4,
saveInterval: 1000
} )
```

## Limitations

The CKEditor 5 watchdog listens to uncaught errors which can be associated with the editor instance created by that watchdog. Currently, these errors are {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} so ones explicitly thrown by the editor (by its internal checks). This means that not every runtime error which crashed the editor can be caught which, in turn, means that not every crash can be detected.
Expand Down
154 changes: 118 additions & 36 deletions src/watchdog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* globals console, window */

import mix from '@ckeditor/ckeditor5-utils/src/mix';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import { throttle, cloneDeepWith, isElement } from 'lodash-es';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import areConnectedThroughProperties from '@ckeditor/ckeditor5-utils/src/areconnectedthroughproperties';
Expand All @@ -23,19 +23,17 @@ import areConnectedThroughProperties from '@ckeditor/ckeditor5-utils/src/areconn
*/
export default class Watchdog {
/**
* @param {Object} [config] The watchdog plugin configuration.
* @param {Number} [config.crashNumberLimit=3] A threshold specifying the number of crashes
* when the watchdog stops restarting the editor in case of errors.
* @param {Number} [config.waitingTime=5000] A minimum amount of milliseconds between saving editor data internally.
* @param {module:watchdog/watchdog~WatchdogConfig} [config] The watchdog plugin configuration.
*/
constructor( { crashNumberLimit, waitingTime } = {} ) {
constructor( config = {} ) {
/**
* An array of crashes saved as an object with the following properties:
*
* * `message`: `String`,
* * `source`: `String`,
* * `lineno`: `String`,
* * `colno`: `String`
* * `colno`: `String`,
* * `date`: `Number`,
*
* @public
* @readonly
Expand All @@ -44,13 +42,34 @@ export default class Watchdog {
this.crashes = [];

/**
* Crash number limit (defaults to `3`). After this limit is reached the editor is not restarted by the watchdog.
* This is to prevent an infinite crash loop.
* Specifies the state of the editor handled by the watchdog. The state can be one of the following values:
*
* * `initializing` - before the first initialization, and after crashes, before the editor is ready,
* * `ready` - a state when a user can interact with the editor,
* * `crashed` - a state when an error occurs - it quickly changes to `initializing` or `crashedPermanently`
* depending on how many and how frequency errors have been caught recently,
* * `crashedPermanently` - a state when the watchdog stops reacting to errors and keeps the editor crashed,
* * `destroyed` - a state when the editor is manually destroyed by the user after calling `watchdog.destroy()`
*
* @public
* @observable
* @member {'initializing'|'ready'|'crashed'|'crashedPermanently'|'destroyed'} #state
*/
this.set( 'state', 'initializing' );

/**
* @private
* @type {Number}
* @see module:watchdog/watchdog~WatchdogConfig
*/
this._crashNumberLimit = crashNumberLimit || 3;
this._crashNumberLimit = typeof config.crashNumberLimit === 'number' ? config.crashNumberLimit : 3;

/**
* @private
* @type {Number}
* @see module:watchdog/watchdog~WatchdogConfig
*/
this._minimumNonErrorTimePeriod = typeof config.minimumNonErrorTimePeriod === 'number' ? config.minimumNonErrorTimePeriod : 5000;

/**
* Checks if the event error comes from the editor that is handled by the watchdog (by checking the error context)
Expand All @@ -62,13 +81,13 @@ export default class Watchdog {
this._boundErrorHandler = this._handleGlobalErrorEvent.bind( this );

/**
* Throttled save method. The `save()` method is called the specified `waitingTime` after `throttledSave()` is called,
* Throttled save method. The `save()` method is called the specified `saveInterval` after `throttledSave()` is called,
* unless a new action happens in the meantime.
*
* @private
* @type {Function}
*/
this._throttledSave = throttle( this._save.bind( this ), waitingTime || 5000 );
this._throttledSave = throttle( this._save.bind( this ), config.saveInterval || 5000 );

/**
* The current editor instance.
Expand Down Expand Up @@ -214,15 +233,28 @@ export default class Watchdog {

this._lastDocumentVersion = editor.model.document.version;
this._data = editor.data.get();
this.state = 'ready';
} );
}

/**
* Destroys the current editor instance by using the destructor passed to the {@link #setDestructor `setDestructor()`} method.
* Destroys the current editor instance by using the destructor passed to the {@link #setDestructor `setDestructor()`} method
* and sets state to `destroyed`.
*
* @returns {Promise}
*/
destroy() {
this.state = 'destroyed';

return this._destroy();
}

/**
* Destroys the current editor instance by using the destructor passed to the {@link #setDestructor `setDestructor()`} method.
*
* @private
*/
_destroy() {
window.removeEventListener( 'error', this._boundErrorHandler );
this.stopListening( this._editor.model.document, 'change:data', this._throttledSave );

Expand Down Expand Up @@ -269,50 +301,83 @@ export default class Watchdog {
* @param {Event} evt Error event.
*/
_handleGlobalErrorEvent( evt ) {
if ( !evt.error.is || !evt.error.is( 'CKEditorError' ) ) {
return;
}

if ( evt.error.context === undefined ) {
if ( evt.error.is && evt.error.is( 'CKEditorError' ) && evt.error.context === undefined ) {
console.error( 'The error is missing its context and Watchdog cannot restart the proper editor.' );

return;
}

// In some cases the editor should not be restarted - e.g. in case of the editor initialization.
// That's why the `null` was introduced as a correct error context which does cause restarting.
if ( evt.error.context === null ) {
return;
}

if ( this._isErrorComingFromThisEditor( evt.error ) ) {
if ( this._shouldReactToErrorEvent( evt ) ) {
this.crashes.push( {
message: evt.error.message,
source: evt.source,
lineno: evt.lineno,
colno: evt.colno
colno: evt.colno,
date: Date.now()
} );

this.fire( 'error' );
this.fire( 'error', { error: evt.error } );
this.state = 'crashed';

if ( this.crashes.length <= this._crashNumberLimit ) {
if ( this._shouldRestartEditor() ) {
this._restart();
} else {
this.state = 'crashedPermanently';
}
}
}

/**
* Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event.
* Checks whether the error event should be handled.
*
* @private
* @param {Event} evt Error event.
*/
_shouldReactToErrorEvent( evt ) {
return (
evt.error.is &&
evt.error.is( 'CKEditorError' ) &&
evt.error.context !== undefined &&

// In some cases the editor should not be restarted - e.g. in case of the editor initialization.
// That's why the `null` was introduced as a correct error context which does cause restarting.
evt.error.context !== null &&

// Do not react to errors if the watchdog is in states other than `ready`.
this.state === 'ready' &&

this._isErrorComingFromThisEditor( evt.error )
);
}

/**
* Checks if the editor should be restared or if it should be marked as crashed.
*/
_shouldRestartEditor() {
if ( this.crashes.length <= this._crashNumberLimit ) {
return true;
}

const lastErrorTime = this.crashes[ this.crashes.length - 1 ].date;
const firstMeaningfulErrorTime = this.crashes[ this.crashes.length - 1 - this._crashNumberLimit ].date;

const averageNonErrorTimePeriod = ( lastErrorTime - firstMeaningfulErrorTime ) / this._crashNumberLimit;

return averageNonErrorTimePeriod > this._minimumNonErrorTimePeriod;
}

/**
* Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
* the state to `initializing`.
*
* @private
* @fires restart
* @returns {Promise}
*/
_restart() {
this.state = 'initializing';
this._throttledSave.flush();

return Promise.resolve()
.then( () => this.destroy() )
.then( () => this._destroy() )
.catch( err => console.error( 'An error happened during the editor destructing.', err ) )
.then( () => {
if ( typeof this._elementOrData === 'string' ) {
Expand Down Expand Up @@ -352,9 +417,10 @@ export default class Watchdog {
* watchdog.create( elementOrData, config );
*
* @param {*} Editor The editor class.
* @param {module:watchdog/watchdog~WatchdogConfig} [watchdogConfig] The watchdog plugin configuration.
*/
static for( Editor ) {
const watchdog = new Watchdog();
static for( Editor, watchdogConfig ) {
const watchdog = new Watchdog( watchdogConfig );

watchdog.setCreator( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
watchdog.setDestructor( editor => editor.destroy() );
Expand All @@ -363,7 +429,8 @@ export default class Watchdog {
}

/**
* Fired when an error occurs and the watchdog will be restarting the editor.
* Fired when a new {@link module:utils/ckeditorerror~CKEditorError `CKEditorError`} error connected to the watchdog editor occurs
* and the watchdog will react to it.
*
* @event error
*/
Expand All @@ -375,4 +442,19 @@ export default class Watchdog {
*/
}

mix( Watchdog, EmitterMixin );
mix( Watchdog, ObservableMixin );

/**
* The watchdog plugin configuration.
*
* @typedef {Object} WatchdogConfig
*
* @property {Number} [crashNumberLimit=3] A threshold specifying the number of editor errors (defaults to `3`).
* After this limit is reached and the time between last errors is shorter than `minimumNonErrorTimePeriod`
* the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
* @property {Number} [minimumNonErrorTimePeriod=5000] An average amount of milliseconds between last editor errors
* (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached
* the watchdog changes its state to `crashedPermanently` and it stops restarting the editor. This prevents an infinite restart loop.
* @property {Number} [saveInterval=5000] A minimum number of milliseconds between saving editor data internally, (defaults to 5000).
* Note that for large documents this might have an impact on the editor performance.
*/
26 changes: 19 additions & 7 deletions tests/manual/watchdog.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<button id="random-error">Simulate a random `Error`</button>

<button id="restart">Restart both editors</button>
<div class="editor">
<div>First editor state: <span id='editor-1-state'></span></div>

<div id="editor-1">
<h2>Heading 1</h2>
<p>Paragraph</p>
<div id="editor-1">
<h2>Heading 1</h2>
<p>Paragraph</p>
</div>
</div>

<div id="editor-2">
<h2>Heading 1</h2>
<p>Paragraph</p>
<div class="editor">
<div>Second editor state: <span id='editor-2-state'></span></div>

<div id="editor-2">
<h2>Heading 1</h2>
<p>Paragraph</p>
</div>
</div>

<style>
.editor {
margin-top: 20px;
}
</style>
Loading

0 comments on commit 5bdbfe5

Please sign in to comment.