paiJobRestServer.ts 2.3 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
3

4
import { Request, Response, Router } from 'express';
5
import { ClusterJobRestServer } from '../common/clusterJobRestServer';
6
7
import { PAITrainingService } from './paiTrainingService';

8
9
10
11
12
13
export interface ParameterFileMeta {
    readonly experimentId: string;
    readonly trialId: string;
    readonly filePath: string;
}

14
15
/**
 * PAI Training service Rest server, provides rest API to support pai job metrics update
16
 *
17
 */
18
export class PAIJobRestServer extends ClusterJobRestServer {
19
    protected parameterFileMetaList: ParameterFileMeta[] = [];
20

21
    protected readonly paiTrainingService: PAITrainingService;
22
23
24
25

    /**
     * constructor to provide NNIRestServer's own rest property, e.g. port
     */
26
    constructor (paiTrainingService: PAITrainingService) {
27
        super();
28
        this.paiTrainingService = paiTrainingService;
29
30
    }

chicm-ms's avatar
chicm-ms committed
31
    protected handleTrialMetrics(jobId: string, metrics: any[]): void {
32
33
34
35
36
37
38
        // Split metrics array into single metric, then emit
        // Warning: If not split metrics into single ones, the behavior will be UNKNOWN
        for (const singleMetric of metrics) {
            this.paiTrainingService.MetricsEmitter.emit('metric', {
                id : jobId,
                data : singleMetric
            });
39
        }
40
    }
41
42
43
44
45
46

    protected createRestHandler(): Router {
        const router: Router = super.createRestHandler();

        router.post(`/parameter-file-meta`, (req: Request, res: Response) => {
            try {
liuzhe-lz's avatar
liuzhe-lz committed
47
                this.log.info('POST /parameter-file-meta, body is', req.body);
48
49
50
51
52
53
54
55
56
                this.parameterFileMetaList.push(req.body);
                res.send();
            } catch (err) {
                this.log.error(`POST parameter-file-meta error: ${err}`);
                res.status(500);
                res.send(err.message);
            }
        });

57
        router.get(`/parameter-file-meta`, (_req: Request, res: Response) => {
58
59
60
61
62
63
64
65
66
67
68
69
            try {
                this.log.info(`GET /parameter-file-meta`);
                res.send(this.parameterFileMetaList);
            } catch (err) {
                this.log.error(`GET parameter-file-meta error: ${err}`);
                res.status(500);
                res.send(err.message);
            }
        });

        return router;
    }
70
}