-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-elasticsearch-tasks): create task index before triggering task (
- Loading branch information
1 parent
76c74c3
commit 49a33e7
Showing
11 changed files
with
229 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/api-elasticsearch-tasks/src/tasks/createIndexes/OnBeforeTrigger.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Context } from "~/types"; | ||
import type { IndexManager } from "~/settings"; | ||
import { listIndexes } from "./listIndexes"; | ||
import { createIndexFactory } from "~/tasks/createIndexes/createIndex"; | ||
import { listCreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/listCreateElasticsearchIndexTaskPlugin"; | ||
|
||
export interface IOnBeforeTriggerParams { | ||
indexManager: IndexManager; | ||
context: Context; | ||
} | ||
|
||
export class OnBeforeTrigger { | ||
private readonly context: Context; | ||
private readonly indexManager: IndexManager; | ||
|
||
public constructor(params: IOnBeforeTriggerParams) { | ||
this.context = params.context; | ||
this.indexManager = params.indexManager; | ||
} | ||
|
||
public async run(targets: string[] | undefined): Promise<void> { | ||
const plugins = listCreateElasticsearchIndexTaskPlugin<Context>(this.context.plugins); | ||
|
||
try { | ||
const allIndexes = await listIndexes({ | ||
context: this.context, | ||
plugins | ||
}); | ||
const indexes = allIndexes.filter(index => { | ||
if (!targets?.length) { | ||
return true; | ||
} | ||
for (const t of targets) { | ||
if (index.index.includes(t)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
if (indexes.length === 0) { | ||
console.warn( | ||
"There are no indexes to create before triggering the Create indexes task.", | ||
{ | ||
targets | ||
} | ||
); | ||
return; | ||
} | ||
|
||
const createIndex = createIndexFactory(this.indexManager); | ||
|
||
for (const { index, settings } of indexes) { | ||
try { | ||
console.log("Creating index", index); | ||
await createIndex.createIfNotExists(index, settings); | ||
} catch (ex) { | ||
console.error(`Failed to create index "${index}".`, ex); | ||
} | ||
} | ||
} catch (ex) { | ||
console.error(ex); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/api-elasticsearch-tasks/src/tasks/createIndexes/createIndex.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IndexManager } from "~/settings"; | ||
|
||
export const createIndexFactory = (manager: IndexManager) => { | ||
return { | ||
create: async (index: string, settings?: Record<string, any>): Promise<void> => { | ||
return manager.createIndex(index, settings); | ||
}, | ||
createIfNotExists: async (index: string, settings?: Record<string, any>): Promise<void> => { | ||
try { | ||
const exists = await manager.indexExists(index); | ||
if (exists) { | ||
return; | ||
} | ||
} catch (ex) { | ||
return; | ||
} | ||
|
||
return await manager.createIndex(index, settings); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...api-elasticsearch-tasks/src/tasks/createIndexes/listCreateElasticsearchIndexTaskPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CreateElasticsearchIndexTaskPlugin } from "~/tasks/createIndexes/CreateElasticsearchIndexTaskPlugin"; | ||
import type { Context } from "~/types"; | ||
import type { PluginsContainer } from "@webiny/plugins"; | ||
|
||
export const listCreateElasticsearchIndexTaskPlugin = <C extends Context = Context>( | ||
plugins: PluginsContainer | ||
): CreateElasticsearchIndexTaskPlugin<C>[] => { | ||
return plugins.byType<CreateElasticsearchIndexTaskPlugin<Context>>( | ||
CreateElasticsearchIndexTaskPlugin.type | ||
); | ||
}; |
Oops, something went wrong.