Skip to content

Commit

Permalink
update on license changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Dec 7, 2022
1 parent b11ab86 commit cd47c50
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.
11 changes: 3 additions & 8 deletions x-pack/plugins/ml/common/license/ml_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ export class MlLicense {
private _isMinimumLicense: boolean = false;
private _isFullLicense: boolean = false;
private _isTrialLicense: boolean = false;
private _initialized: boolean = false;

public setup(
license$: Observable<ILicense>,
postInitFunctions?: Array<(lic: MlLicense) => void>
) {
public setup(license$: Observable<ILicense>, callback?: (lic: MlLicense) => void) {
this._licenseSubscription = license$.subscribe(async (license) => {
const { isEnabled: securityIsEnabled } = license.getFeature('security');

Expand All @@ -45,10 +41,9 @@ export class MlLicense {
this._isFullLicense = isFullLicense(this._license);
this._isTrialLicense = isTrialLicense(this._license);

if (this._initialized === false && postInitFunctions !== undefined) {
postInitFunctions.forEach((f) => f(this));
if (callback !== undefined) {
callback(this);
}
this._initialized = true;
});
}

Expand Down
13 changes: 6 additions & 7 deletions x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ export const renderApp = (

appMountParams.onAppLeave((actions) => actions.default());

const mlLicense = setLicenseCache(deps.licensing, coreStart.application, [
() =>
ReactDOM.render(
<App coreStart={coreStart} deps={deps} appMountParams={appMountParams} />,
appMountParams.element
),
]);
const mlLicense = setLicenseCache(deps.licensing, coreStart.application, () =>
ReactDOM.render(
<App coreStart={coreStart} deps={deps} appMountParams={appMountParams} />,
appMountParams.element
)
);

return () => {
mlLicense.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ let mlLicense: MlClientLicense | null = null;
export function setLicenseCache(
licensingStart: LicensingPluginStart,
application: CoreStart['application'],
postInitFunctions?: Array<(lic: MlLicense) => void>
callback?: (lic: MlLicense) => void
) {
mlLicense = new MlClientLicense(application);
mlLicense.setup(licensingStart.license$, postInitFunctions);
mlLicense.setup(licensingStart.license$, callback);
return mlLicense;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ describe('MlClientLicense', () => {

const license$ = new Subject();

mlLicense.setup(license$ as Observable<ILicense>, [
(license) => {
// when passed in via postInitFunction callback, the license should be valid
// even if the license$ observable gets triggered after this setup.
expect(license.isFullLicense()).toBe(true);
done();
},
]);
mlLicense.setup(license$ as Observable<ILicense>, (license) => {
// when passed in via postInitFunction callback, the license should be valid
// even if the license$ observable gets triggered after this setup.
expect(license.isFullLicense()).toBe(true);
done();
});

license$.next({
check: () => ({ state: 'valid' }),
Expand Down
42 changes: 21 additions & 21 deletions x-pack/plugins/ml/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,28 +257,28 @@ export class MlServerPlugin
this.savedObjectsStart = coreStart.savedObjects;
this.dataViews = plugins.dataViews;

this.mlLicense.setup(plugins.licensing.license$, [
(mlLicense: MlLicense) => {
if (mlLicense.isMlEnabled() === false) {
return;
}
if (this.home) {
initSampleDataSets(mlLicense, this.home);
}
this.mlLicense.setup(plugins.licensing.license$, (mlLicense: MlLicense) => {
if (mlLicense.isMlEnabled() === false || mlLicense.isFullLicense() === false) {
this.savedObjectsSyncService.unscheduleSyncTask(plugins.taskManager);
return;
}

// check whether the job saved objects exist
// and create them if needed.
const { initializeJobs } = jobSavedObjectsInitializationFactory(
coreStart,
this.security,
this.spacesPlugin !== undefined
);
initializeJobs().finally(() => {
this.setMlReady();
});
this.savedObjectsSyncService.scheduleSyncTask(plugins.taskManager, coreStart);
},
]);
if (this.home) {
initSampleDataSets(mlLicense, this.home);
}

// check whether the job saved objects exist
// and create them if needed.
const { initializeJobs } = jobSavedObjectsInitializationFactory(
coreStart,
this.security,
this.spacesPlugin !== undefined
);
initializeJobs().finally(() => {
this.setMlReady();
});
this.savedObjectsSyncService.scheduleSyncTask(plugins.taskManager, coreStart);
});
}

public stop() {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/ml/server/saved_objects/sync_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export class SavedObjectsSyncService {
return null;
}
}

public async unscheduleSyncTask(taskManager: TaskManagerStartContract) {
await taskManager.removeIfExists(SAVED_OBJECTS_SYNC_TASK_ID);
}
}

function createLocalLogger(logger: Logger, preText: string) {
Expand Down

0 comments on commit cd47c50

Please sign in to comment.