@constructive-io/knative-job-server 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE +23 -0
- package/README.md +1 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +112 -0
- package/dist/run.d.ts +2 -0
- package/dist/run.js +15 -0
- package/jest.config.js +18 -0
- package/package.json +41 -0
- package/src/index.ts +129 -0
- package/src/run.ts +13 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +9 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## 0.3.7 (2025-12-18)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @constructive-io/knative-job-server
|
|
9
|
+
|
|
10
|
+
## [0.3.6](https://github.com/constructive-io/jobs/compare/@launchql/knative-job-server@0.3.5...@launchql/knative-job-server@0.3.6) (2025-12-17)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @launchql/knative-job-server
|
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# knative-job-server
|
package/dist/env.d.ts
ADDED
package/dist/env.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const envalid_1 = require("envalid");
|
|
4
|
+
exports.default = (0, envalid_1.cleanEnv)(process.env, {
|
|
5
|
+
INTERNAL_JOBS_CALLBACK_PORT: (0, envalid_1.port)({ default: 12345 })
|
|
6
|
+
}, { dotEnvPath: null });
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
const express_1 = __importDefault(require("express"));
|
|
40
|
+
const body_parser_1 = __importDefault(require("body-parser"));
|
|
41
|
+
const jobs = __importStar(require("@constructive-io/job-utils"));
|
|
42
|
+
const job_pg_1 = __importDefault(require("@constructive-io/job-pg"));
|
|
43
|
+
exports.default = (pgPool = job_pg_1.default.getPool()) => {
|
|
44
|
+
const app = (0, express_1.default)();
|
|
45
|
+
app.use(body_parser_1.default.json());
|
|
46
|
+
const withClient = (cb) => async (req, res, next) => {
|
|
47
|
+
const client = (await pgPool.connect());
|
|
48
|
+
try {
|
|
49
|
+
await cb(client, req, res, next);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
next(e);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
client.release();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const complete = withClient(async (client, req, res) => {
|
|
59
|
+
const workerId = req.get('X-Worker-Id');
|
|
60
|
+
const jobIdHeader = req.get('X-Job-Id');
|
|
61
|
+
if (!workerId || !jobIdHeader) {
|
|
62
|
+
console.log('server: missing worker/job headers on completion callback', { workerId, jobIdHeader });
|
|
63
|
+
res.status(400).json({ error: 'Missing X-Worker-Id or X-Job-Id header' });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
console.log(`server: Completed task ${jobIdHeader} with success`);
|
|
67
|
+
await jobs.completeJob(client, { workerId, jobId: jobIdHeader });
|
|
68
|
+
res
|
|
69
|
+
.set({
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
})
|
|
72
|
+
.status(200)
|
|
73
|
+
.send({ workerId, jobId: jobIdHeader });
|
|
74
|
+
});
|
|
75
|
+
const fail = withClient(async (client, req, res) => {
|
|
76
|
+
const workerId = req.get('X-Worker-Id');
|
|
77
|
+
const jobIdHeader = req.get('X-Job-Id');
|
|
78
|
+
if (!workerId || !jobIdHeader) {
|
|
79
|
+
console.log('server: missing worker/job headers on failure callback', { workerId, jobIdHeader });
|
|
80
|
+
res.status(400).json({ error: 'Missing X-Worker-Id or X-Job-Id header' });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const errorMessage = req.body.error || req.body.message || 'UNKNOWN_ERROR';
|
|
84
|
+
console.log(`server: Failed task ${jobIdHeader} with error: \n${errorMessage}\n\n`);
|
|
85
|
+
await jobs.failJob(client, {
|
|
86
|
+
workerId,
|
|
87
|
+
jobId: jobIdHeader,
|
|
88
|
+
message: errorMessage
|
|
89
|
+
});
|
|
90
|
+
res.status(200).json({ workerId, jobId: jobIdHeader });
|
|
91
|
+
});
|
|
92
|
+
app.post('*', async (req, res, next) => {
|
|
93
|
+
const jobId = req.get('X-Job-Id');
|
|
94
|
+
if (typeof jobId === 'undefined') {
|
|
95
|
+
console.log('server: undefined JOB, what is this? healthcheck?');
|
|
96
|
+
console.log(req.url);
|
|
97
|
+
console.log(req.originalUrl);
|
|
98
|
+
return res.status(200).json('OK');
|
|
99
|
+
}
|
|
100
|
+
if (req.get('X-Job-Error') === 'true') {
|
|
101
|
+
await fail(req, res, next);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
await complete(req, res, next);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
app.use((error, req, res, next) => {
|
|
108
|
+
// TODO check headers for jobId and call fail?
|
|
109
|
+
res.status(500).json({ error });
|
|
110
|
+
});
|
|
111
|
+
return app;
|
|
112
|
+
};
|
package/dist/run.d.ts
ADDED
package/dist/run.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const index_1 = __importDefault(require("./index"));
|
|
8
|
+
const job_pg_1 = __importDefault(require("@constructive-io/job-pg"));
|
|
9
|
+
const job_utils_1 = require("@constructive-io/job-utils");
|
|
10
|
+
const pgPool = job_pg_1.default.getPool();
|
|
11
|
+
const port = (0, job_utils_1.getJobsCallbackPort)();
|
|
12
|
+
(0, index_1.default)(pgPool).listen(port, () => {
|
|
13
|
+
// eslint-disable-next-line no-console
|
|
14
|
+
console.log(`listening ON ${port}`);
|
|
15
|
+
});
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
preset: 'ts-jest',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.tsx?$': [
|
|
7
|
+
'ts-jest',
|
|
8
|
+
{
|
|
9
|
+
babelConfig: false,
|
|
10
|
+
tsconfig: 'tsconfig.json',
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
},
|
|
14
|
+
transformIgnorePatterns: [`/node_modules/*`],
|
|
15
|
+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
|
|
16
|
+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
|
|
17
|
+
modulePathIgnorePatterns: ['dist/*'],
|
|
18
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constructive-io/knative-job-server",
|
|
3
|
+
"version": "0.3.7",
|
|
4
|
+
"description": "knative job server",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"homepage": "https://github.com/constructive-io/jobs/tree/master/packages/knative-job-server#readme",
|
|
7
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"directories": {
|
|
10
|
+
"lib": "src",
|
|
11
|
+
"test": "__tests__"
|
|
12
|
+
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"faas-job-server": "src/run.ts"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/constructive-io/jobs"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "jest",
|
|
25
|
+
"test:watch": "jest --watch",
|
|
26
|
+
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
|
|
27
|
+
"build": "tsc -p tsconfig.json",
|
|
28
|
+
"build:watch": "tsc -p tsconfig.json -w"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/constructive-io/jobs/issues"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@constructive-io/job-pg": "^0.3.5",
|
|
35
|
+
"@constructive-io/job-utils": "^0.5.0",
|
|
36
|
+
"body-parser": "1.19.0",
|
|
37
|
+
"express": "4.17.1",
|
|
38
|
+
"pg": "8.16.3"
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "86d74dc4fce9051df0d2b5bcc163607aba42f009"
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import bodyParser from 'body-parser';
|
|
3
|
+
import type { Pool, PoolClient } from 'pg';
|
|
4
|
+
import * as jobs from '@constructive-io/job-utils';
|
|
5
|
+
import poolManager from '@constructive-io/job-pg';
|
|
6
|
+
|
|
7
|
+
type JobRequestBody = {
|
|
8
|
+
message?: string;
|
|
9
|
+
error?: string;
|
|
10
|
+
// allow additional fields without typing everything
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type JobRequest = {
|
|
15
|
+
get(name: string): string | undefined;
|
|
16
|
+
body: JobRequestBody;
|
|
17
|
+
url: string;
|
|
18
|
+
originalUrl: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type JobResponse = {
|
|
22
|
+
set(headers: Record<string, string>): JobResponse;
|
|
23
|
+
status(code: number): JobResponse;
|
|
24
|
+
json(body: unknown): JobResponse;
|
|
25
|
+
send(body: unknown): JobResponse;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type NextFn = (err?: unknown) => void;
|
|
29
|
+
|
|
30
|
+
type WithClientHandler = (
|
|
31
|
+
client: PoolClient,
|
|
32
|
+
req: JobRequest,
|
|
33
|
+
res: JobResponse,
|
|
34
|
+
next: NextFn
|
|
35
|
+
) => Promise<void>;
|
|
36
|
+
|
|
37
|
+
export default (pgPool: Pool = poolManager.getPool()) => {
|
|
38
|
+
const app = express();
|
|
39
|
+
app.use(bodyParser.json());
|
|
40
|
+
|
|
41
|
+
const withClient =
|
|
42
|
+
(cb: WithClientHandler) =>
|
|
43
|
+
async (req: JobRequest, res: JobResponse, next: NextFn) => {
|
|
44
|
+
const client = (await pgPool.connect()) as PoolClient;
|
|
45
|
+
try {
|
|
46
|
+
await cb(client as PoolClient, req, res, next);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
next(e);
|
|
49
|
+
} finally {
|
|
50
|
+
client.release();
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const complete = withClient(async (client, req, res) => {
|
|
55
|
+
const workerId = req.get('X-Worker-Id');
|
|
56
|
+
const jobIdHeader = req.get('X-Job-Id');
|
|
57
|
+
|
|
58
|
+
if (!workerId || !jobIdHeader) {
|
|
59
|
+
console.log(
|
|
60
|
+
'server: missing worker/job headers on completion callback',
|
|
61
|
+
{ workerId, jobIdHeader }
|
|
62
|
+
);
|
|
63
|
+
res.status(400).json({ error: 'Missing X-Worker-Id or X-Job-Id header' });
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
console.log(`server: Completed task ${jobIdHeader} with success`);
|
|
68
|
+
await jobs.completeJob(client, { workerId, jobId: jobIdHeader });
|
|
69
|
+
|
|
70
|
+
res
|
|
71
|
+
.set({
|
|
72
|
+
'Content-Type': 'application/json'
|
|
73
|
+
})
|
|
74
|
+
.status(200)
|
|
75
|
+
.send({ workerId, jobId: jobIdHeader });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const fail = withClient(async (client, req, res) => {
|
|
79
|
+
const workerId = req.get('X-Worker-Id');
|
|
80
|
+
const jobIdHeader = req.get('X-Job-Id');
|
|
81
|
+
|
|
82
|
+
if (!workerId || !jobIdHeader) {
|
|
83
|
+
console.log(
|
|
84
|
+
'server: missing worker/job headers on failure callback',
|
|
85
|
+
{ workerId, jobIdHeader }
|
|
86
|
+
);
|
|
87
|
+
res.status(400).json({ error: 'Missing X-Worker-Id or X-Job-Id header' });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const errorMessage = req.body.error || req.body.message || 'UNKNOWN_ERROR';
|
|
92
|
+
|
|
93
|
+
console.log(
|
|
94
|
+
`server: Failed task ${jobIdHeader} with error: \n${errorMessage}\n\n`
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
await jobs.failJob(client, {
|
|
98
|
+
workerId,
|
|
99
|
+
jobId: jobIdHeader,
|
|
100
|
+
message: errorMessage
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
res.status(200).json({ workerId, jobId: jobIdHeader });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
app.post('*', async (req: JobRequest, res: JobResponse, next: NextFn) => {
|
|
107
|
+
const jobId = req.get('X-Job-Id');
|
|
108
|
+
|
|
109
|
+
if (typeof jobId === 'undefined') {
|
|
110
|
+
console.log('server: undefined JOB, what is this? healthcheck?');
|
|
111
|
+
console.log(req.url);
|
|
112
|
+
console.log(req.originalUrl);
|
|
113
|
+
return res.status(200).json('OK');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (req.get('X-Job-Error') === 'true') {
|
|
117
|
+
await fail(req, res, next);
|
|
118
|
+
} else {
|
|
119
|
+
await complete(req, res, next);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
app.use((error: unknown, req: JobRequest, res: JobResponse, next: NextFn) => {
|
|
124
|
+
// TODO check headers for jobId and call fail?
|
|
125
|
+
res.status(500).json({ error });
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return app;
|
|
129
|
+
};
|
package/src/run.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import server from './index';
|
|
4
|
+
import poolManager from '@constructive-io/job-pg';
|
|
5
|
+
import { getJobsCallbackPort } from '@constructive-io/job-utils';
|
|
6
|
+
|
|
7
|
+
const pgPool = poolManager.getPool();
|
|
8
|
+
const port = getJobsCallbackPort();
|
|
9
|
+
|
|
10
|
+
server(pgPool).listen(port, () => {
|
|
11
|
+
// eslint-disable-next-line no-console
|
|
12
|
+
console.log(`listening ON ${port}`);
|
|
13
|
+
});
|