This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
forked from adempiere/proxy-adempiere-api
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add scheduler end-points to
dKron
. (#227)
* feat: Add scheduler end-points to `dKron`. * fix axios types. * upload yarn.lock. * fix axios version. * axios to 1.1.0 * axios to 0.27.2
- Loading branch information
1 parent
0c4a6f6
commit a6851a9
Showing
10 changed files
with
178 additions
and
6 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
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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/************************************************************************************ | ||
* Copyright (C) 2018-present E.R.P. Consultores y Asociados, C.A. * | ||
* Contributor(s): Edwin Betancourt EdwinBetanc0urt@outlook.com * | ||
* This program is free software: you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation, either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* GNU General Public License for more details. * | ||
* You should have received a copy of the GNU General Public License * | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. * | ||
************************************************************************************/ | ||
|
||
import { StorefrontApiModule, registerExtensions } from '@storefront-api/lib/module'; | ||
import { StorefrontApiContext } from '@storefront-api/lib/module/types'; | ||
import { Router } from 'express'; | ||
|
||
import path from 'path'; | ||
import axios from 'axios'; | ||
|
||
function convertJobFromDKron (jobToConvert) { | ||
if (!jobToConvert) { | ||
return undefined; | ||
} | ||
return { | ||
...jobToConvert, | ||
executor_config: undefined | ||
} | ||
} | ||
|
||
function convertExecutionFromDKron (executionToConvert) { | ||
if (!executionToConvert) { | ||
return undefined; | ||
} | ||
|
||
const output = executionToConvert.output; | ||
const searhValue = '\n'; | ||
const start = executionToConvert.output.lastIndexOf(searhValue); | ||
let parsedOutput = '{}'; | ||
if (start >= 0) { | ||
parsedOutput = output.slice(start + searhValue.length, output.length); | ||
} | ||
if (parsedOutput === '""') { | ||
parsedOutput = '{}'; | ||
} | ||
|
||
return { | ||
...executionToConvert, | ||
output: JSON.parse(parsedOutput) | ||
} | ||
} | ||
|
||
export const ADempiereScheduler: StorefrontApiModule = new StorefrontApiModule({ | ||
key: 'adempiere-scheduler', | ||
|
||
initApi: ({ config, db, app }: StorefrontApiContext): void => { | ||
const api = Router(); | ||
const schedulerHost = config.get('adempiereScheduler.host') | ||
|
||
// version | ||
// perhaps expose some API metadata at the root | ||
api.get('/', (req, res) => { | ||
res.json({ ...config.get('modules.adempiereScheduler') }); | ||
}); | ||
|
||
/** | ||
* GET List Jobs | ||
*/ | ||
api.get('/v1/jobs', async (req, res, next) => { | ||
try { | ||
const response = await axios.get(schedulerHost + '/v1/jobs') | ||
.then(resp => { | ||
return resp.data.map(job => { | ||
return convertJobFromDKron(job); | ||
}); | ||
}); | ||
res.json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
api.post('/v1/jobs/:id', async (req, res, next) => { | ||
try { | ||
const response = await axios.post( | ||
schedulerHost + req.url, | ||
req.body | ||
// { | ||
// params: req.params | ||
// } | ||
).then(resp => convertJobFromDKron(resp.data)); | ||
res.json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
api.get('/v1/jobs/:id/executions', async (req, res, next) => { | ||
try { | ||
const response = await axios.get(schedulerHost + req.url) // '/v1/jobs/:name/executions') | ||
.then(resp => { | ||
return resp.data.map(execution => { | ||
return convertExecutionFromDKron(execution); | ||
}); | ||
}); | ||
res.json(response); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
|
||
registerExtensions({ | ||
app, | ||
config, | ||
db, | ||
registeredExtensions: config.get('modules.adempiereScheduler.registeredExtensions'), | ||
rootPath: path.join(__dirname, 'api', 'extensions') | ||
}); | ||
|
||
// api router | ||
app.use('/scheduler', api); | ||
} | ||
|
||
}); |
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