-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl instance controller sevice;
- Loading branch information
Showing
19 changed files
with
278 additions
and
846 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,118 @@ | ||
import Config from "./config" | ||
import { getApplicationsInStatus, InstanceStatus, updateApplicationStatus } from "./support/application" | ||
import { ApplicationInstanceOperator } from "./support/instance-operator" | ||
import { InstanceOperator } from "./support/instance-operator" | ||
import { logger } from "./support/logger" | ||
|
||
|
||
export async function start_schedular() { | ||
export function start_schedular() { | ||
logger.info('start schedular loop') | ||
setInterval(loop, Config.SCHEDULER_INTERVAL) | ||
} | ||
|
||
function loop() { | ||
const tick = new Date() | ||
logger.info('enter loop ' + tick) | ||
|
||
handle_prepared_start(tick) | ||
handle_starting(tick) | ||
handle_prepared_stop(tick) | ||
handle_prepared_stop(tick) | ||
handle_stopping(tick) | ||
handle_prepared_restart(tick) | ||
handle_restarting_stopping(tick) | ||
handle_restarting(tick) | ||
} | ||
|
||
|
||
async function handle_prepared_start(tick: Date) { | ||
logger.info('processing `prepared_start` status with tick: ', tick) | ||
async function handle_prepared_start(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.PREPARED_START) | ||
for (let app of apps) { | ||
try { | ||
const res = await ApplicationInstanceOperator.start(app) | ||
if (res) await updateApplicationStatus(app.appid, app.status, InstanceStatus.STARTING) | ||
const res = await InstanceOperator.start(app) | ||
if (res) { | ||
logger.info(tick, `update ${app.appid} status from 'PREPARED_START' to 'STARTING'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.STARTING) | ||
} | ||
} catch (error) { | ||
logger.error(`handle_prepared_start got error for app ${app.appid} with tick: ${tick}`) | ||
logger.error(tick, `handle_prepared_start(${app.appid}) error: `, error) | ||
} | ||
} | ||
} | ||
|
||
async function handle_starting(tick: Date) { | ||
logger.info('processing `starting` status with tick: ', tick) | ||
async function handle_starting(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.STARTING) | ||
for (let app of apps) { | ||
try { | ||
// const res = await ApplicationInstanceOperator.start(app) | ||
// if (res) await updateApplicationStatus(app.appid, app.status, InstanceStatus.STARTING) | ||
const status = await InstanceOperator.status(app) | ||
if (status === InstanceStatus.RUNNING) { | ||
logger.info(tick, `update ${app.appid} status from 'STARTING' to 'RUNNING'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.RUNNING) | ||
} | ||
} catch (error) { | ||
logger.error(`handle_starting got error for app ${app.appid} with tick: ${tick}`) | ||
logger.error(tick, `handle_starting(${app.appid}) error: `, error) | ||
} | ||
} | ||
} | ||
|
||
async function handle_prepared_stop(tick: Date) { | ||
logger.info('processing `prepared_stop` status with tick: ', tick) | ||
async function handle_prepared_stop(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.PREPARED_STOP) | ||
for (let app of apps) { | ||
try { | ||
const res = await InstanceOperator.stop(app) | ||
if (res) { | ||
logger.info(tick, `update ${app.appid} status from 'PREPARED_STOP' to 'STOPPING'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.STOPPING) | ||
} | ||
} catch (error) { | ||
logger.error(tick, `handle_prepared_stop(${app.appid}) error: `, error) | ||
} | ||
} | ||
} | ||
|
||
|
||
async function handle_stopping(tick: Date) { | ||
logger.info('processing `stopping` status with tick: ', tick) | ||
async function handle_stopping(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.STOPPING) | ||
for (let app of apps) { | ||
try { | ||
const status = await InstanceOperator.status(app) | ||
if (status === InstanceStatus.STOPPED) { | ||
logger.info(tick, `update ${app.appid} status from 'STOPPING' to 'STOPPED'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.STOPPED) | ||
} | ||
} catch (error) { | ||
logger.error(tick, `handle_stopping(${app.appid}) error: `, error) | ||
} | ||
} | ||
} | ||
|
||
|
||
async function handle_prepared_restart(tick: Date) { | ||
logger.info('processing `prepared_restart` status with tick: ', tick) | ||
async function handle_prepared_restart(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.PREPARED_RESTART) | ||
for (let app of apps) { | ||
try { | ||
const res = await InstanceOperator.stop(app) | ||
if (res) { | ||
logger.info(tick, `update ${app.appid} status from 'PREPARED_RESTART' to 'RESTARTING'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.RESTARTING) | ||
} | ||
} catch (error) { | ||
logger.error(tick, `handle_prepared_restart(${app.appid}) error: `, error) | ||
} | ||
} | ||
} | ||
|
||
async function handle_restarting_stopping(tick: Date) { | ||
logger.info('processing `restarting:stopping` status with tick: ', tick) | ||
async function handle_restarting(tick: any) { | ||
const apps = await getApplicationsInStatus(InstanceStatus.RESTARTING) | ||
for (let app of apps) { | ||
try { | ||
const status = await InstanceOperator.status(app) | ||
if (status !== InstanceStatus.STOPPED) continue | ||
|
||
logger.info(tick, `start stopped ${app.appid} for restarting`) | ||
const res = await InstanceOperator.start(app) | ||
if (res) { | ||
logger.info(tick, `update ${app.appid} status from 'RESTARTING' to 'STARTING'`) | ||
await updateApplicationStatus(app.appid, app.status, InstanceStatus.STARTING) | ||
} | ||
} catch (error) { | ||
logger.error(tick, `handle_stopping(${app.appid}) error: `, error) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.