Skip to content

Commit 59e9f6e

Browse files
Added documentation for CPUs and memory, fixes #25 (#27) (#28)
* refactor: Accept a single object parameter options instead of 3 different positional parameters * fix: Move 100000 to Runner.ts * docs: Updated documentation about CPUs and memory for worker * docs: Added example for options in worker * feat: v0.1.6 Co-authored-by: Rahil Kabani <[email protected]> Co-authored-by: Rahil Kabani <[email protected]>
1 parent 90b2c1f commit 59e9f6e

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,21 @@ import { Worker } from 'code-executor';
182182
const worker = new Worker('myExecutor', 'redis://127.0.0.1:6379');
183183
```
184184

185-
You can create a new `Worker` object and listen with the same `name` and `redis` string you passed to the master class. There is another optional parameter called `folderPath`, which we will discuss later.
186-
<br />
185+
You can create a new `Worker` object and listen with the same `name` and `redis` string you passed to the master class. There is another optional parameter called `options`, which is an object that may consist of the following parameters:
186+
187+
- `folderPath`, _string_: Will be discusses later.
188+
- `memory`, _number_: The amount of memory assigned to every Docker container spawned by this worker, in MB. The default is 0 (no limit).
189+
- `CPUs`, _number_: The number of CPUs assigned to every Docker container spawned by this worker. The default is 0.5.
190+
191+
For example, you could pass these values to the constructor.
192+
193+
```js
194+
const worker = new Worker('myExecutor', 'redis://127.0.0.1:6379', {
195+
folderPath: '/tmp/myFolder',
196+
memory: 100,
197+
CPUs: 1,
198+
});
199+
```
187200

188201
An object of the `Worker` class has the following important functions:
189202

examples/worker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { Worker, languages } from '../src/CodeExecutor';
22
import logger from '../src/utils/logger';
33

44
/**
5-
* name, redis, folderPath ( path to mount, default /tmp/code-exec ),
5+
* name, redis, options
6+
*
7+
* Options:
8+
*
9+
* folderPath ( path to mount, default /tmp/code-exec ),
610
* memory (in MB, default 0, ie no limit),
7-
* CPUs (no. of CPUs, default 0.5)
11+
* CPUs (no. of CPUs, default 0.5),
812
*/
913
const worker = new Worker('myExecutor', 'redis://127.0.0.1:6379');
1014

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-executor",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "A CLI/library to execute code against test cases in various languages and obtain relevant results.",
55
"main": "dist/src/CodeExecutor.js",
66
"keywords": [

src/Runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export default class Runner {
5050
promisesToKeep.push(this.docker.run(tag, ['bash', '/start.sh', `${i}`, `${timeout}`], null, {
5151
HostConfig: {
5252
CpuPeriod: 100000,
53-
CpuQuota: CPUs * 100000,
54-
Memory: memory,
53+
CpuQuota: CPUs * 1000000,
54+
Memory: memory * 1000000,
5555
NetworkMode: 'none',
5656
AutoRemove: true,
5757
Mounts: [{

src/Worker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Bull from 'bull';
44
import Runner from './Runner';
55
import Builder from './Builder';
66

7-
import { Code, Result } from './models/models';
7+
import { Code, Result, WorkerOptions } from './models/models';
88
import logger from './utils/logger';
99

1010
export default class Worker {
@@ -22,13 +22,16 @@ export default class Worker {
2222

2323
private CPUs?: number;
2424

25-
constructor(name: string, redis: string, folderPath?: string, memory?: number, CPUs?: number) {
25+
constructor(name: string, redis: string, options?: WorkerOptions) {
2626
this.docker = new Docker();
2727
this.runner = new Runner(this.docker);
2828
this.builder = new Builder(this.docker);
2929
this.queue = new Bull(name, redis);
30+
31+
const { folderPath, memory, CPUs } = options;
32+
3033
this.folderPath = folderPath || '/tmp/code-exec';
31-
this.memory = (memory || 0) * 1000000;
34+
this.memory = memory || 0;
3235
this.CPUs = CPUs || 0.5;
3336
}
3437

src/models/models.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ export interface Result {
2929
id: string;
3030
tests: Tests[];
3131
}
32+
33+
export interface WorkerOptions {
34+
folderPath?: string;
35+
memory?: number;
36+
CPUs?: number;
37+
}

0 commit comments

Comments
 (0)