Skip to content

Commit

Permalink
feat(memory): add ability to limit memory (RAM) limit per job
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Sep 21, 2017
1 parent 98915f7 commit 9d4f01e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
9 changes: 0 additions & 9 deletions src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ import { readdir, readFile } from 'fs';
import * as yaml from 'yamljs';
import * as temp from 'temp';

export enum CacheType {
bundler,
yarn,
pip,
ccache,
packages,
cargo
}

export enum CommandType {
git = 'git',
before_install = 'before_install',
Expand Down
28 changes: 18 additions & 10 deletions src/api/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const docker = new dockerode();
export function createContainer(
name: string,
image: string,
envs?: string[]
envs: string[],
memoryLimitPerJob: number
): Observable<ProcessOutput> {
return new Observable(observer => {
docker.createContainer({
Expand All @@ -33,6 +34,9 @@ export function createContainer(
PortBindings: {
'22/tcp': [{ HostPort: '' }],
'5900/tcp': [{ HostPort: '' }]
},
HostConfig: {
Memory: memoryLimitPerJob * 1024 * 1024
}
} as any)
.then(container => container.start())
Expand Down Expand Up @@ -241,16 +245,19 @@ export function getContainersStats(): Observable<any> {
let rawJson = json + buf.toString();
try {
let data = JSON.parse(rawJson);
const stats = {
id: container.Id,
name: container.Names[0].substr(1) || '',
cpu: getCpuData(data),
network: getNetworkData(data),
memory: getMemory(data)
};

stream.destroy();
resolve(stats);
if (data && data.precpu_stats.system_cpu_usage) {
const stats = {
id: container.Id,
name: container.Names[0].substr(1) || '',
cpu: getCpuData(data),
network: getNetworkData(data),
memory: getMemory(data)
};

stream.destroy();
resolve(stats);
}
} catch (e) {
json = rawJson;
}
Expand All @@ -271,6 +278,7 @@ function getCpuData(json: any): { usage: string, cores: number } {
const total = preCpuStats.cpu_usage.total_usage - postCpuStats.cpu_usage.total_usage;
const curr = preCpuStats.system_cpu_usage - postCpuStats.system_cpu_usage;
const perc = isNaN(total / (total + curr) * 100) ? 0 : total / (total + curr) * 100;

return {
usage: perc.toFixed(2) + '%',
cores: postCpuStats.online_cpus
Expand Down
4 changes: 3 additions & 1 deletion src/api/process-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ export function startJob(proc: JobProcess): Promise<void> {

const jobTimeout = config.jobTimeout ? config.jobTimeout * 1000 : 3600000;
const idleTimeout = config.idleTimeout ? config.idleTimeout * 1000 : 3600000;
startBuildProcess(proc, envVariables, jobTimeout, idleTimeout)
const memoryLimitPerJob = config.memoryLimitPerJob || 1024;

startBuildProcess(proc, envVariables, jobTimeout, idleTimeout, memoryLimitPerJob)
.subscribe(event => {
const msg: JobProcessEvent = {
build_id: proc.build_id,
Expand Down
5 changes: 3 additions & 2 deletions src/api/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export function startBuildProcess(
proc: JobProcess,
variables: string[],
jobTimeout: number,
idleTimeout: number
idleTimeout: number,
memoryLimitPerJob: number
): Observable<ProcessOutput> {
return new Observable(observer => {
const image = proc.image_name;
Expand Down Expand Up @@ -99,7 +100,7 @@ export function startBuildProcess(
]);
}

const sub = docker.createContainer(name, image, envs)
const sub = docker.createContainer(name, image, envs, memoryLimitPerJob)
.concat(...gitCommands.map(cmd => docker.attachExec(name, cmd)))
.concat(restoreCache)
.concat(...installCommands.map(cmd => docker.attachExec(name, cmd)))
Expand Down
1 change: 1 addition & 0 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const defaultConfig = {
concurrency: 10,
idleTimeout: 600,
jobTimeout: 3600,
memoryLimitPerJob: 2048,
ssl: false,
sslcert: null,
sslkey: null,
Expand Down

0 comments on commit 9d4f01e

Please sign in to comment.