@@ -15,23 +15,35 @@ import { getInternalSavedObjectsClient } from './util';
1515import { repairFactory } from './repair' ;
1616import { jobSavedObjectServiceFactory } from './service' ;
1717import { mlLog } from '../lib/log' ;
18+ import { ML_SAVED_OBJECT_TYPE } from '../../common/types/saved_objects' ;
1819
19- export function jobInitializationFactory ( core : CoreStart ) {
20- const client = core . elasticsearch . client ;
20+ /**
21+ * Creates initializeJobs function which is used to check whether
22+ * ml job saved objects exist and creates them if needed
23+ *
24+ * @param core: CoreStart
25+ */
26+ export function jobSavedObjectsInitializationFactory ( core : CoreStart ) {
27+ const client = ( core . elasticsearch . client as unknown ) as IScopedClusterClient ;
2128
29+ /**
30+ * Check whether ML saved objects exist.
31+ * If they don't, check to see whether ML jobs exist.
32+ * If jobs exist, but the saved objects do not, create the saved objects.
33+ *
34+ */
2235 async function initializeJobs ( ) {
2336 try {
24- if ( await _needsInitializing ( ) ) {
25- mlLog . info ( 'Initializing job saved objects' ) ;
26- const savedObjectsClient = getInternalSavedObjectsClient ( core ) ;
27- const jobSavedObjectService = jobSavedObjectServiceFactory ( savedObjectsClient ) ;
28- const { initSavedObjects } = repairFactory (
29- ( client as unknown ) as IScopedClusterClient ,
30- jobSavedObjectService
31- ) ;
32- await initSavedObjects ( ) ;
33- mlLog . info ( 'Job saved objects initialized for * space' ) ;
37+ if ( ( await _needsInitializing ( ) ) === false ) {
38+ // ml job saved objects have already been initialized
39+ return ;
3440 }
41+ mlLog . info ( 'Initializing job saved objects' ) ;
42+ const savedObjectsClient = getInternalSavedObjectsClient ( core ) ;
43+ const jobSavedObjectService = jobSavedObjectServiceFactory ( savedObjectsClient ) ;
44+ const { initSavedObjects } = repairFactory ( client , jobSavedObjectService ) ;
45+ await initSavedObjects ( ) ;
46+ mlLog . info ( 'Job saved objects initialized for * space' ) ;
3547 } catch ( error ) {
3648 mlLog . error ( 'Error Initializing jobs' ) ;
3749 }
@@ -64,7 +76,7 @@ export function jobInitializationFactory(core: CoreStart) {
6476 filter : [
6577 {
6678 term : {
67- type : 'ml-job' ,
79+ type : ML_SAVED_OBJECT_TYPE ,
6880 } ,
6981 } ,
7082 ] ,
0 commit comments