-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use rdf-connect/ldes-client
#45
Open
sergiofenoll
wants to merge
18
commits into
master
Choose a base branch
from
feature/rdf-connect-ldes-client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d92df4e
chore: move files to lib folder
sergiofenoll 54e4983
chore: add logger
sergiofenoll 0df8034
chore: add custom fetch to support the features we need
sergiofenoll d6df6b8
feat: replace member processor implementation for new client
sergiofenoll 302fc85
chore: remove ldes-pipeline
sergiofenoll 720b795
chore: remove LDES_STREAM from Dockerfile
sergiofenoll 64395b2
docs: update README
sergiofenoll a442167
feat: replace actor-init-ldes-client with ldes-client
sergiofenoll 9083532
docs: mention volume mount for state file in README
sergiofenoll e9cf41f
chore: update ldes-client to latest version and consume metadata
sergiofenoll a373e97
feat: support providing LDES_VERSION_OF_PATH and TIMESTAMP_PATH
sergiofenoll 0f6bc05
chore: remove debug logging of headers
sergiofenoll 33b01da
chore: update rdf-connect/ldes-client
sergiofenoll 5cfee97
chore: enable "safe" mode for fetch
sergiofenoll a43f397
chore: log client errors as stacktrace
sergiofenoll 0d6049b
chore: update ldes-client
sergiofenoll 4acde57
Update README.md
nvdk 6cdc1f7
chore: bump ldes-client
sergiofenoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ FROM semtech/mu-javascript-template:1.7.0 | |
LABEL maintainer="[email protected]" | ||
ENV PERSIST_STATE "false" | ||
ENV LDES_ENDPOINT_VIEW "http://ldes-time-fragmenter:3000/example/1" | ||
ENV LDES_STREAM "http://example.org/example-stream" | ||
ENV REPLACE_VERSIONS "true" | ||
ENV DEBUG_AUTH_HEADERS "false" | ||
ENV LOG_SPARQL_ALL "false" |
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 |
---|---|---|
@@ -1,69 +1,119 @@ | ||
import { CronJob } from "cron"; | ||
import fs from 'fs'; | ||
import { enhanced_fetch, intoConfig, LDESInfo, replicateLDES } from 'ldes-client'; | ||
import { | ||
CRON_PATTERN, | ||
LDES_DEREFERENCE_MEMBERS, | ||
LDES_ENDPOINT_HEADER_PREFIX, | ||
INGEST_MODE, | ||
REPLACE_VERSIONS, | ||
PERSIST_STATE, | ||
LDES_ENDPOINT_VIEW, | ||
LDES_POLLING_INTERVAL, | ||
LDES_REQUESTS_PER_MINUTE, | ||
LDES_STREAM, | ||
LDES_TIMESTAMP_PATH, | ||
RUN_ONCE, | ||
NODE_ENV, | ||
logConfig, | ||
LDES_VERSION_OF_PATH, | ||
REPLACE_VERSIONS, | ||
RUNONCE | ||
} from "./config"; | ||
import { ConfigurableLDESOptions } from "./consumer"; | ||
import LdesPipeline from "./ldes-pipeline"; | ||
import { NamedNode } from "n3"; | ||
let taskIsRunning = false; | ||
LDES_TIMESTAMP_PATH, | ||
} from "./cfg"; | ||
import { memberProcessor } from './lib/member-processor'; | ||
import { custom_fetch } from './lib/fetch/custom-fetch'; | ||
import { getLoggerFor } from './lib/logger'; | ||
import { DataFactory } from "n3"; | ||
|
||
const consumerJob = new CronJob(CRON_PATTERN, async () => { | ||
nvdk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (taskIsRunning) { | ||
console.log("Another task is still running"); | ||
return; | ||
} | ||
const { namedNode } = DataFactory; | ||
|
||
logConfig(); | ||
|
||
if (NODE_ENV === "production") { | ||
main(); | ||
} else { | ||
const timeout = 10_000; // Make this configurable? | ||
console.log(`Starting LDES consumer in ${timeout}ms, connect to your debugger now :)`); | ||
setTimeout(main, timeout); | ||
} | ||
|
||
async function main() { | ||
let stateFilePath; | ||
try { | ||
taskIsRunning = true; | ||
const endpoint = LDES_ENDPOINT_VIEW; | ||
if (endpoint) { | ||
const ldesOptions: ConfigurableLDESOptions = { | ||
dereferenceMembers: LDES_DEREFERENCE_MEMBERS, | ||
pollingInterval: LDES_POLLING_INTERVAL | ||
}; | ||
if (LDES_REQUESTS_PER_MINUTE) { | ||
ldesOptions.requestsPerMinute = LDES_REQUESTS_PER_MINUTE; | ||
} | ||
const datasetIri = new NamedNode(LDES_STREAM); | ||
const consumer = new LdesPipeline({ datasetIri, endpoint, ldesOptions }); | ||
console.log("Started processing " + endpoint); | ||
await consumer.consumeStream(); | ||
console.log("Finished processing " + endpoint); | ||
if (RUNONCE) { | ||
console.log("Job is complete."); | ||
process.exit(); | ||
const url = new URL(LDES_ENDPOINT_VIEW); | ||
stateFilePath = `/data/${url.host}-state.json`; | ||
} catch (e: any) { | ||
throw new Error("Provided endpoint couldn't be parsed as URL, double check your settings."); | ||
} | ||
|
||
let shapeFile; | ||
if (fs.existsSync('/config/shape.ttl')) { | ||
shapeFile = '/config/shape.ttl'; | ||
} | ||
const client = replicateLDES( | ||
intoConfig({ | ||
url: LDES_ENDPOINT_VIEW, | ||
urlIsView: true, | ||
polling: !RUN_ONCE, | ||
pollInterval: LDES_POLLING_INTERVAL, | ||
stateFile: PERSIST_STATE ? stateFilePath : undefined, | ||
materialize: INGEST_MODE === 'MATERIALIZE', | ||
lastVersionOnly: REPLACE_VERSIONS, // Won't emit members if they're known to be older than what is already in the state file | ||
loose: true, // Make this configurable? IPDC needs this to be true | ||
shapeFile, | ||
fetch: enhanced_fetch({ | ||
safe: true, // In case of an exception being thrown by fetch, this will just retry the call in a while (true) loop until it stops throwing? Not great. | ||
/* In comment are the default values, perhaps we want to make these configurable | ||
concurrent: 10, // Amount of concurrent requests to a single domain | ||
retry: { | ||
codes: [408, 425, 429, 500, 502, 503, 504], // Which faulty HTTP status codes will trigger retry | ||
base: 500, // Seems to be unused in the client code | ||
maxRetries: 5, | ||
}*/ | ||
}, custom_fetch), | ||
|
||
}), | ||
"none", | ||
); | ||
|
||
client.on("error", (error: any) => { | ||
logger.info("Received an error from the LDES client!"); | ||
logger.error(error); | ||
logger.error(error.stack); | ||
}) | ||
|
||
const getLDESInfo = async (): Promise<LDESInfo> => { | ||
return new Promise( | ||
(resolve, reject) => { | ||
try { | ||
client.on('description', (info: LDESInfo) => { | ||
resolve(info); | ||
}); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
) | ||
}; | ||
|
||
const logger = getLoggerFor('main'); | ||
logger.info('Starting stream...'); | ||
const ldesStream = client.stream({ highWaterMark: 10 }); | ||
try { | ||
logger.info('Waiting for LDES info...'); | ||
const { isVersionOfPath: versionOfPath, timestampPath } = await getLDESInfo(); | ||
if (versionOfPath !== undefined && timestampPath !== undefined) { | ||
logger.info(`Received LDES info: ${JSON.stringify({ versionOfPath, timestampPath })}`); | ||
} else if (LDES_VERSION_OF_PATH !== undefined && LDES_TIMESTAMP_PATH !== undefined) { | ||
logger.info(`LDES feed info contained no versionOfPath & timestampPath, using provided values: ${JSON.stringify({ LDES_VERSION_OF_PATH, LDES_TIMESTAMP_PATH })}`); | ||
} else { | ||
throw new Error("No endpoint provided"); | ||
throw new Error('LDES feed info contained no versionOfPath & timestampPath and no LDES_VERSION_OF_PATH & LDES_TIMESTAMP_PATH were provided to service, exiting.'); | ||
} | ||
|
||
await ldesStream.pipeTo( | ||
memberProcessor( | ||
versionOfPath ?? namedNode(LDES_VERSION_OF_PATH as string), | ||
timestampPath ?? namedNode(LDES_TIMESTAMP_PATH as string), | ||
) | ||
); | ||
|
||
logger.info('Finished processing stream'); | ||
} catch (e) { | ||
console.error(e); | ||
logger.error('Processing stream failed'); | ||
logger.error(e); | ||
} finally { | ||
taskIsRunning = false; | ||
ldesStream.cancel(); | ||
} | ||
}); | ||
|
||
console.log("config", { | ||
CRON_PATTERN, | ||
LDES_DEREFERENCE_MEMBERS, | ||
LDES_ENDPOINT_HEADER_PREFIX, | ||
LDES_ENDPOINT_VIEW, | ||
LDES_POLLING_INTERVAL, | ||
LDES_REQUESTS_PER_MINUTE, | ||
LDES_STREAM, | ||
LDES_TIMESTAMP_PATH, | ||
LDES_VERSION_OF_PATH, | ||
REPLACE_VERSIONS, | ||
RUNONCE | ||
}); | ||
|
||
consumerJob.start(); | ||
} |
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,69 @@ | ||
import env from "env-var"; | ||
import { getLoggerFor } from "./lib/logger"; | ||
|
||
const logger = getLoggerFor("config"); | ||
|
||
export const LDES_ENDPOINT_VIEW = env.get("LDES_ENDPOINT_VIEW").required().asString(); | ||
export const LDES_POLLING_INTERVAL = env.get("LDES_POLLING_INTERVAL").default(60000).asIntPositive(); | ||
export const LDES_REQUESTS_PER_MINUTE = env.get("LDES_REQUESTS_PER_MINUTE").default(0).asIntPositive(); | ||
export const LDES_ENDPOINT_HEADERS_STRING = env.get("LDES_ENDPOINT_HEADERS").default("{}").asString(); | ||
export const LDES_VERSION_OF_PATH = env.get("LDES_VERSION_OF_PATH").asString(); | ||
export const LDES_TIMESTAMP_PATH = env.get("LDES_TIMESTAMP_PATH").asString(); | ||
export let LDES_ENDPOINT_HEADERS = {}; | ||
|
||
try { | ||
LDES_ENDPOINT_HEADERS = JSON.parse(LDES_ENDPOINT_HEADERS_STRING); | ||
} catch (e: any) { | ||
logger.error(`Failed to parse contents of LDES_HEADERS. Faulty content: ${LDES_ENDPOINT_HEADERS_STRING}`); | ||
logger.error(e); | ||
throw e; | ||
} | ||
|
||
|
||
export const INGEST_MODE = env.get("INGEST_MODE").default("ALL").asEnum(["ALL", "MATERIALIZE"]); | ||
export const REPLACE_VERSIONS = env.get("REPLACE_VERSIONS").default("true").asBool(); | ||
export const PERSIST_STATE = env.get("PERSIST_STATE").default("false").asBool() | ||
|
||
export const SPARQL_ENDPOINT_HEADER_PREFIX = "SPARQL_ENDPOINT_HEADER_"; | ||
|
||
export const SPARQL_BATCH_SIZE = env.get("SPARQL_BATCH_SIZE").default(0).asIntPositive(); | ||
export const ENABLE_SPARQL_BATCHING = SPARQL_BATCH_SIZE > 0; | ||
|
||
export const SPARQL_AUTH_USER = env.get("SPARQL_AUTH_USER").asString(); | ||
export const SPARQL_AUTH_PASSWORD = env.get("SPARQL_AUTH_PASSWORD").asString(); | ||
|
||
export const BLANK_NODE_NAMESPACE = env.get("BLANK_NODE_NAMESPACE").default("http://mu.semte.ch/blank#").asString(); | ||
export const CRON_PATTERN = env.get("CRON_PATTERN").default("* 0 * * * *").asString(); | ||
|
||
const RUN_ONCE_VAR = env.get("RUN_ONCE").default("false").asBool(); | ||
const RUNONCE_VAR = env.get("RUNONCE").default("false").asBool(); | ||
export const RUN_ONCE = RUN_ONCE_VAR || RUNONCE_VAR; | ||
|
||
export const MU_APPLICATION_GRAPH = env.get("MU_APPLICATION_GRAPH").required().asString(); // Provided by template | ||
export const MU_SPARQL_ENDPOINT = env.get("MU_SPARQL_ENDPOINT").required().asString(); // Provided by template | ||
export const DEBUG_AUTH_HEADERS = env.get("DEBUG_AUTH_HEADERS").default("false").asBool(); | ||
|
||
export const NODE_ENV = env.get("NODE_ENV").default("production").asEnum(["development", "production"]); | ||
|
||
export function logConfig() { | ||
// Should this use the logger instead? | ||
console.log("Config", { | ||
INGEST_MODE, | ||
REPLACE_VERSIONS, | ||
PERSIST_STATE, | ||
LDES_ENDPOINT_VIEW, | ||
LDES_POLLING_INTERVAL, | ||
LDES_REQUESTS_PER_MINUTE, | ||
SPARQL_BATCH_SIZE, | ||
SPARQL_AUTH_USER, | ||
SPARQL_AUTH_PASSWORD, | ||
BLANK_NODE_NAMESPACE, | ||
CRON_PATTERN, | ||
RUN_ONCE, | ||
MU_APPLICATION_GRAPH, | ||
MU_SPARQL_ENDPOINT, | ||
DEBUG_AUTH_HEADERS, | ||
LDES_VERSION_OF_PATH, | ||
LDES_TIMESTAMP_PATH, | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guessing this will also need to mvoe to a json structure, similar to LDES_ENDPOINT_HEADERS.