Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
feat: Add scheduler end-points to dKron. (#227)
Browse files Browse the repository at this point in the history
* 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
EdwinBetanc0urt authored Sep 15, 2023
1 parent 0c4a6f6 commit a6851a9
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 6 deletions.
7 changes: 7 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@
"httpBased": false
}
},
"adempiereScheduler": {
"registeredExtensions": [
]
},
"sampleApi": {
"registeredExtensions": [
],
Expand Down Expand Up @@ -405,6 +409,9 @@
"version": "Vue-Store-Front"
}
},
"adempiereScheduler": {
"host": "http://localhost:8080"
},
"magento2": {
"imgUrl": "http://demo-magento2.vuestorefront.io/media/catalog/product",
"assetPath": "/../var/magento2-sample-data/pub/media",
Expand Down
1 change: 1 addition & 0 deletions docker/adempiere-api/development.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ENV \
REDIS_HOST="localhost" \
REDIS_PORT=6379 \
REDIS_DB=0 \
SCHEDULER_HOST="http://localhost:8080" \
AD_DEFAULT_HOST="localhost" \
AD_DEFAULT_PORT=50059 \
AD_TOKEN="" \
Expand Down
1 change: 1 addition & 0 deletions docker/adempiere-api/production-alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ENV \
REDIS_HOST="localhost" \
REDIS_PORT=6379 \
REDIS_DB=0 \
SCHEDULER_HOST="http://localhost:8080" \
AD_DEFAULT_HOST="localhost" \
AD_DEFAULT_PORT=50059 \
AD_TOKEN="" \
Expand Down
1 change: 1 addition & 0 deletions docker/adempiere-api/production.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ENV \
REDIS_HOST="localhost" \
REDIS_PORT=6379 \
REDIS_DB=0 \
SCHEDULER_HOST="http://localhost:8080" \
AD_DEFAULT_HOST="localhost" \
AD_DEFAULT_PORT=50059 \
AD_TOKEN="" \
Expand Down
4 changes: 4 additions & 0 deletions docker/adempiere-api/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ sed -i "s|STORE_URL_IMAGES|$STORE_URL_IMAGES|g" /var/www/proxy-adempiere-api/con
sed -i "s|\"STORE_HTTP_BASED\"|$STORE_HTTP_BASED|g" /var/www/proxy-adempiere-api/config/local.json


# Set env to scheduler
sed -i "s|SCHEDULER_HOST|$SCHEDULER_HOST|g" /var/www/proxy-adempiere-api/config/local.json


# Set env values to redis service
sed -i "s|REDIS_HOST|$REDIS_HOST|g" /var/www/proxy-adempiere-api/config/local.json
sed -i "s|REDIS_PORT|$REDIS_PORT|g" /var/www/proxy-adempiere-api/config/local.json
Expand Down
7 changes: 7 additions & 0 deletions docker/adempiere-api/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@
"httpBased": "STORE_HTTP_BASED"
}
},
"adempiereScheduler": {
"registeredExtensions": [
]
},
"sampleApi": {
"registeredExtensions": [
],
Expand Down Expand Up @@ -443,6 +447,9 @@
"version": "Vue-Store-Front"
}
},
"adempiereScheduler": {
"host": "SCHEDULER_HOST"
},
"magento2": {
"imgUrl": "http://demo-magento2.vuestorefront.io/media/catalog/product",
"assetPath": "/../var/magento2-sample-data/pub/media",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
"license": "MIT",
"dependencies": {
"@2fd/graphdoc": "^2.4.0",
"@grpc/grpc-js": "1.8.21",
"@adempiere/grpc-web-store-api": "1.5.7",
"@elastic/elasticsearch": "7.14.1",
"@google-cloud/storage": "^3.0.4",
"@grpc/grpc-js": "1.8.21",
"@storefront-api/core": "1.0.0-rc.4",
"@storefront-api/default-catalog": "1.0.0-rc.4",
"@storefront-api/default-img": "1.0.0-rc.4",
Expand All @@ -76,6 +76,7 @@
"@storefront-api/platform-magento2": "1.0.0-rc.4",
"ajv": "^6.4.0",
"ajv-keywords": "^3.4.1",
"axios": "0.27.2",
"bodybuilder": "^2.2.25",
"cli-color": "^2.0.3",
"commander": "^2.19.0",
Expand Down
12 changes: 7 additions & 5 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { DefaultVuestorefrontApiModule } from '@storefront-api/default-vsf'
import { DefaultCatalogModule } from '@storefront-api/default-catalog'
// import { DefaultVuestorefrontApiModule } from '@storefront-api/default-vsf'
// import { DefaultCatalogModule } from '@storefront-api/default-catalog'
// import { DefaultImgModule } from '@storefront-api/default-img'
// import { SampleApiModule } from './sample-api'
import { ADempiereApi } from './adempiere-api'
import { ADempiereStore } from './adempiere-store'
// import { ADempiereStore } from './adempiere-store'
import { ADempiereScheduler } from './scheduler'
// import { TemplateModule } from './template-module'
import { StorefrontApiModule } from '@storefront-api/lib/module'
import * as magento2 from '@storefront-api/platform-magento2'
// import * as magento2 from '@storefront-api/platform-magento2'

export let modules: StorefrontApiModule[] = [
// DefaultVuestorefrontApiModule({
Expand All @@ -19,7 +20,8 @@ export let modules: StorefrontApiModule[] = [
// DefaultImgModule(),
// SampleApiModule,
// TemplateModule,
ADempiereApi
ADempiereApi,
ADempiereScheduler
// ADempiereStore
]

Expand Down
126 changes: 126 additions & 0 deletions src/modules/scheduler/index.ts
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);
}

});
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4244,6 +4244,14 @@ aws4@^1.11.0, aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==

axios@0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"

axios@^0.19.0:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
Expand Down Expand Up @@ -7857,6 +7865,11 @@ follow-redirects@^1.0.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.2.tgz#cecb825047c00f5e66b142f90fed4f515dec789b"
integrity sha512-yLR6WaE2lbF0x4K2qE2p9PEXKLDjUjnR/xmjS3wHAYxtlsI9MLLBJUZirAHKzUZDGLxje7w/cXR49WOUo4rbsA==

follow-redirects@^1.14.9:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
Expand Down Expand Up @@ -7904,6 +7917,15 @@ form-data@^3.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down

0 comments on commit a6851a9

Please sign in to comment.