-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Operation APIs and Examples (#834)
* Use correct naming for JobConfig * Rename data-processor to processor * create slicer-core base class and other fixes * restructure operations to make more sense * add parallel slicer and more power to the slicer core class * add examples and simplify construction of the new processors * improvements to job-components * documentation and example improvements to job-components * bump job-components to 0.5.0 * fix tests * better docs, improved op apis and reorganized a few things * export default and doc improvements * Fix job-component tests and worker-allocation tests * Make DataEntity factory methods static methods, and remove them OperationCore * add base class for Schema * Expose a shim for legacy operations from within job-components * make operation apis only initialized when called to create * fix running tests inside typescript pkg * add slice events shim * improvements to operation apis * better operation api shimming and examples * remove unused lib * Rename makeDataEntity to make * Rename makeDataEntity to make in tests
- Loading branch information
1 parent
182242c
commit a176efe
Showing
80 changed files
with
4,403 additions
and
610 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
# `job-components` | ||
|
||
> TODO: description | ||
## Usage | ||
|
||
``` | ||
const { ... } = require('@terascope/job-components'); | ||
// TODO: DEMONSTRATE API | ||
``` | ||
> A teraslice library for validating jobs schemas, registering apis, and defining and running new Job APIs |
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,4 @@ | ||
{ | ||
"name": "job-components-example", | ||
"version": "v0.1.0" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/job-components/examples/asset/example-batch-op/index.js
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,9 @@ | ||
'use strict'; | ||
|
||
const { legacyProcessorShim } = require('@terascope/job-components'); | ||
const Processor = require('./processor'); | ||
const Schema = require('./schema'); | ||
|
||
// This file for backwards compatibility and functionality will be limited | ||
// but it should allow you to write processors using the new way today | ||
module.exports = legacyProcessorShim(Processor, Schema); |
14 changes: 14 additions & 0 deletions
14
packages/job-components/examples/asset/example-batch-op/processor.js
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,14 @@ | ||
'use strict'; | ||
|
||
const { BatchProcessor } = require('@terascope/job-components'); | ||
|
||
class ExampleBatchProcessor extends BatchProcessor { | ||
onBatch(batch) { | ||
return batch.map((data) => { | ||
data.batchedAt = new Date().toISOString(); | ||
return data; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ExampleBatchProcessor; |
17 changes: 17 additions & 0 deletions
17
packages/job-components/examples/asset/example-batch-op/schema.js
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,17 @@ | ||
'use strict'; | ||
|
||
const { ConvictSchema } = require('@terascope/job-components'); | ||
|
||
class Schema extends ConvictSchema { | ||
build() { | ||
return { | ||
example: { | ||
default: 'examples are quick and easy', | ||
doc: 'A random example schema property', | ||
format: 'String', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Schema; |
9 changes: 9 additions & 0 deletions
9
packages/job-components/examples/asset/example-filter-op/index.js
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,9 @@ | ||
'use strict'; | ||
|
||
const { legacyProcessorShim } = require('@terascope/job-components'); | ||
const Processor = require('./processor'); | ||
const Schema = require('./schema'); | ||
|
||
// This file for backwards compatibility and functionality will be limited | ||
// but it should allow you to write processors using the new way today | ||
module.exports = legacyProcessorShim(Processor, Schema); |
14 changes: 14 additions & 0 deletions
14
packages/job-components/examples/asset/example-filter-op/processor.js
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,14 @@ | ||
'use strict'; | ||
|
||
const { Processor } = require('@terascope/job-components'); | ||
|
||
class ExampleFilter extends Processor { | ||
onData(data) { | ||
if (data.statusCode > 400) { | ||
return null; | ||
} | ||
return data; | ||
} | ||
} | ||
|
||
module.exports = ExampleFilter; |
17 changes: 17 additions & 0 deletions
17
packages/job-components/examples/asset/example-filter-op/schema.js
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,17 @@ | ||
'use strict'; | ||
|
||
const { ConvictSchema } = require('@terascope/job-components'); | ||
|
||
class Schema extends ConvictSchema { | ||
build() { | ||
return { | ||
example: { | ||
default: 'examples are quick and easy', | ||
doc: 'A random example schema property', | ||
format: 'String', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Schema; |
9 changes: 9 additions & 0 deletions
9
packages/job-components/examples/asset/example-map-op/index.js
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,9 @@ | ||
'use strict'; | ||
|
||
const { legacyProcessorShim } = require('@terascope/job-components'); | ||
const Processor = require('./processor'); | ||
const Schema = require('./schema'); | ||
|
||
// This file for backwards compatibility and functionality will be limited | ||
// but it should allow you to write processors using the new way today | ||
module.exports = legacyProcessorShim(Processor, Schema); |
12 changes: 12 additions & 0 deletions
12
packages/job-components/examples/asset/example-map-op/processor.js
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,12 @@ | ||
'use strict'; | ||
|
||
const { Processor } = require('@terascope/job-components'); | ||
|
||
class MapProcessor extends Processor { | ||
onData(data) { | ||
data.touchedAt = new Date().toISOString(); | ||
return data; | ||
} | ||
} | ||
|
||
module.exports = MapProcessor; |
17 changes: 17 additions & 0 deletions
17
packages/job-components/examples/asset/example-map-op/schema.js
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,17 @@ | ||
'use strict'; | ||
|
||
const { ConvictSchema } = require('@terascope/job-components'); | ||
|
||
class Schema extends ConvictSchema { | ||
build() { | ||
return { | ||
example: { | ||
default: 'examples are quick and easy', | ||
doc: 'A random example schema property', | ||
format: 'String', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Schema; |
20 changes: 20 additions & 0 deletions
20
packages/job-components/examples/asset/example-reader/api.js
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,20 @@ | ||
'use strict'; | ||
|
||
const { OperationAPI } = require('@terascope/job-components'); | ||
|
||
class ExampleAPI extends OperationAPI { | ||
name() { | ||
return 'ExampleAPI'; | ||
} | ||
|
||
async handle(config) { | ||
return { | ||
config, | ||
say() { | ||
return 'hello'; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = ExampleAPI; |
34 changes: 34 additions & 0 deletions
34
packages/job-components/examples/asset/example-reader/fetcher.js
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,34 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const request = require('request'); | ||
const { Fetcher } = require('@terascope/job-components'); | ||
|
||
class ExampleFetcher extends Fetcher { | ||
async fetch(startingData) { | ||
const statusCode = await this.getStatusCode(startingData.fromUrl); | ||
return _.times(statusCode, n => ({ | ||
id: n, | ||
statusCode, | ||
data: [ | ||
_.random(), | ||
_.random(), | ||
_.random(), | ||
] | ||
})); | ||
} | ||
|
||
async getStatusCode(url) { | ||
return new Promise((resolve, reject) => { | ||
request.get(url, (err, response) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(response.statusCode); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ExampleFetcher; |
13 changes: 13 additions & 0 deletions
13
packages/job-components/examples/asset/example-reader/index.js
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,13 @@ | ||
'use strict'; | ||
|
||
const { legacyReaderShim } = require('@terascope/job-components'); | ||
const Fetcher = require('./fetcher'); | ||
const Slicer = require('./slicer'); | ||
const Schema = require('./schema'); | ||
const ExampleAPI = require('./api'); | ||
|
||
// This file for backwards compatibility and functionality will be limited | ||
// but it should allow you to write processors using the new way today | ||
module.exports = legacyReaderShim(Slicer, Fetcher, Schema, { | ||
'example-reader': ExampleAPI | ||
}); |
17 changes: 17 additions & 0 deletions
17
packages/job-components/examples/asset/example-reader/schema.js
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,17 @@ | ||
'use strict'; | ||
|
||
const { ConvictSchema } = require('@terascope/job-components'); | ||
|
||
class Schema extends ConvictSchema { | ||
build() { | ||
return { | ||
example: { | ||
default: 'examples are quick and easy', | ||
doc: 'A random example schema property', | ||
format: 'String', | ||
} | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Schema; |
15 changes: 15 additions & 0 deletions
15
packages/job-components/examples/asset/example-reader/slicer.js
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,15 @@ | ||
'use strict'; | ||
|
||
const uuidv4 = require('uuid/v4'); | ||
const { Slicer } = require('@terascope/job-components'); | ||
|
||
class ExampleSlicer extends Slicer { | ||
async slice() { | ||
return { | ||
id: uuidv4(), | ||
fetchFrom: 'https://httpstat.us/200' | ||
}; | ||
} | ||
} | ||
|
||
module.exports = ExampleSlicer; |
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,53 @@ | ||
import { OpAPI, OperationAPIConstructor } from './operations/operation-api'; | ||
import legacySliceEventsShim from './operations/shims/legacy-slice-events-shim'; | ||
import { Context, ExecutionConfig } from '@terascope/teraslice-types'; | ||
|
||
export class ExecutionContextAPI { | ||
private _registery: APIRegistry = {}; | ||
private _apis: APIS = {}; | ||
private _context: Context; | ||
private _executionConfig: ExecutionConfig; | ||
|
||
constructor(context: Context, executionConfig: ExecutionConfig) { | ||
this._context = context; | ||
this._executionConfig = executionConfig; | ||
} | ||
|
||
addToRegistry(name: string, api: OperationAPIConstructor) { | ||
this._registery[name] = api; | ||
} | ||
|
||
getAPI(name: string) { | ||
if (this._apis[name] == null) { | ||
throw new Error(`Unable to find API by name "${name}"`); | ||
} | ||
return this._apis[name]; | ||
} | ||
|
||
async initAPI(name: string, ...params: any[]) { | ||
if (this._registery[name] == null) { | ||
throw new Error(`Unable to find API by name "${name}"`); | ||
} | ||
|
||
if (this._apis[name] != null) { | ||
throw new Error(`API "${name}" can only be initalized once`); | ||
} | ||
|
||
const API = this._registery[name]; | ||
const api = new API(this._context, this._executionConfig); | ||
await api.initialize(); | ||
|
||
legacySliceEventsShim(api); | ||
|
||
this._apis[name] = await api.createAPI(...params); | ||
return this._apis[name]; | ||
} | ||
} | ||
|
||
interface APIS { | ||
[name: string]: OpAPI; | ||
} | ||
|
||
interface APIRegistry { | ||
[name: string]: OperationAPIConstructor; | ||
} |
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,7 +1,8 @@ | ||
export * from './config-validators'; | ||
export * from './execution-context-apis'; | ||
export * from './formats'; | ||
export * from './operation-loader'; | ||
export * from './register-apis'; | ||
export * from './operations'; | ||
export * from './job-validator'; | ||
export * from './job-schemas'; | ||
export * from './formats'; | ||
export * from './config-validators'; | ||
export * from './register-apis'; |
Oops, something went wrong.