Skip to content

Commit

Permalink
use promise instead of callback accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Feb 6, 2020
1 parent b3d6019 commit b8820a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { registerMigrateRoute } from './migrate';

interface RegisterRouteOptions {
http: InternalHttpServiceSetup;
getMigrator: () => IKibanaMigrator | undefined;
migratorPromise: Promise<IKibanaMigrator>;
}

export function registerRoutes({ http, getMigrator }: RegisterRouteOptions) {
export function registerRoutes({ http, migratorPromise }: RegisterRouteOptions) {
const router = http.createRouter('');

registerMigrateRoute(router, getMigrator);
registerMigrateRoute(router, migratorPromise);
}
7 changes: 2 additions & 5 deletions src/core/server/saved_objects/routes/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ import { IKibanaMigrator } from '../migrations';

export const registerMigrateRoute = (
router: IRouter,
getMigrator: () => IKibanaMigrator | undefined
migratorPromise: Promise<IKibanaMigrator>
) => {
router.post(
{
path: '/api/saved_objects/_migrate',
validate: false,
},
async (context, req, res) => {
const migrator = getMigrator();
if (!migrator) {
return res.badRequest({ body: 'Migrator is not available yet.' });
}
const migrator = await migratorPromise;
const result = await migrator.runMigrations({ rerun: true });
return res.ok({
body: {
Expand Down
17 changes: 10 additions & 7 deletions src/core/server/saved_objects/saved_objects_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

import { CoreService } from 'src/core/types';
import { Subject } from 'rxjs';
import { first, filter, take } from 'rxjs/operators';
import { CoreService } from '../../types';
import {
SavedObjectsClient,
SavedObjectsClientProvider,
Expand Down Expand Up @@ -220,7 +221,7 @@ export class SavedObjectsService
private clientFactoryProvider?: SavedObjectsClientFactoryProvider;
private clientFactoryWrappers: WrappedClientFactoryWrapper[] = [];

private migrator?: KibanaMigrator;
private migrator$ = new Subject<KibanaMigrator>();
private typeRegistry = new SavedObjectTypeRegistry();
private validations: PropertyValidators = {};

Expand All @@ -242,7 +243,7 @@ export class SavedObjectsService

registerRoutes({
http: setupDeps.http,
getMigrator: () => this.migrator,
migratorPromise: this.migrator$.pipe(first()).toPromise(),
});

return {
Expand Down Expand Up @@ -284,7 +285,9 @@ export class SavedObjectsService
.pipe(first())
.toPromise();
const adminClient = this.setupDeps!.elasticsearch.adminClient;
this.migrator = this.createMigrator(kibanaConfig, savedObjectsConfig, migrationsRetryDelay);
const migrator = this.createMigrator(kibanaConfig, savedObjectsConfig, migrationsRetryDelay);

this.migrator$.next(migrator);

/**
* Note: We want to ensure that migrations have completed before
Expand Down Expand Up @@ -313,12 +316,12 @@ export class SavedObjectsService
).toPromise();

this.logger.info('Starting saved objects migrations');
await this.migrator.runMigrations();
await migrator.runMigrations();
}

const createRepository = (callCluster: APICaller, extraTypes: string[] = []) => {
return SavedObjectsRepository.createRepository(
this.migrator!,
migrator,
this.typeRegistry,
kibanaConfig.index,
callCluster,
Expand Down Expand Up @@ -348,7 +351,7 @@ export class SavedObjectsService
});

return {
migrator: this.migrator,
migrator,
clientProvider,
typeRegistry: this.typeRegistry,
getScopedClient: clientProvider.getClient.bind(clientProvider),
Expand Down

0 comments on commit b8820a0

Please sign in to comment.