platform/services/ai-bot/pod-ai-bot/src/start.ts
Kristina 36b2a0f8c6
Move bot (#6268)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
2024-08-06 21:40:13 +07:00

64 lines
2.2 KiB
TypeScript

//
// Copyright © 2024 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { setMetadata } from '@hcengineering/platform'
import serverToken from '@hcengineering/server-token'
import serverAiBot from '@hcengineering/server-ai-bot'
import { MeasureMetricsContext } from '@hcengineering/core'
import serverClient from '@hcengineering/server-client'
import config from './config'
import { closeDB, getDB } from './storage'
import { AIBotController } from './controller'
import { createBotAccount } from './account'
export const start = async (): Promise<void> => {
setMetadata(serverToken.metadata.Secret, config.ServerSecret)
setMetadata(serverAiBot.metadata.SupportWorkspaceId, config.SupportWorkspace)
setMetadata(serverClient.metadata.UserAgent, config.ServiceID)
setMetadata(serverClient.metadata.Endpoint, config.AccountsURL)
const ctx = new MeasureMetricsContext('ai-bot-service', {})
ctx.info('AI Bot Service started', { firstName: config.FirstName, lastName: config.LastName })
const db = await getDB(config.MongoURL, config.ConfigurationDB)
for (let i = 0; i < 5; i++) {
ctx.info('Creating bot account', { attempt: i })
try {
await createBotAccount()
break
} catch (e) {
ctx.error('Error during account creation', { error: e })
}
await new Promise((resolve) => setTimeout(resolve, 3000))
}
const aiController = new AIBotController(db, ctx)
const onClose = (): void => {
void aiController.close()
void closeDB()
}
process.on('SIGINT', onClose)
process.on('SIGTERM', onClose)
process.on('uncaughtException', (e: Error) => {
console.error(e)
})
process.on('unhandledRejection', (e: Error) => {
console.error(e)
})
}