Skip to content

Commit 73264db

Browse files
John Gilbert - HomeJohn Gilbert - Home
authored andcommitted
add scheduler flavor
1 parent 99b2933 commit 73264db

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

src/flavors/scheduler.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import _ from 'highland';
2+
3+
import {
4+
filterOnEventType, filterOnContent,
5+
} from '../filters';
6+
import {
7+
printStartPipeline,
8+
printEndPipeline,
9+
faulty,
10+
} from '../utils';
11+
12+
import { scheduleEvent } from '../sinks/scheduler';
13+
14+
export const scheduler = (rule) => (s) => s
15+
.filter(onEventType(rule))
16+
.filter(onContent(rule))
17+
.tap(printStartPipeline)
18+
19+
.map(toScheduleRequest(rule))
20+
.through(scheduleEvent(rule))
21+
22+
.tap(printEndPipeline);
23+
24+
const onEventType = (rule) => faulty((uow) => filterOnEventType(rule, uow));
25+
const onContent = (rule) => faulty((uow) => filterOnContent(rule, uow));
26+
27+
const toScheduleRequest = (rule) => faulty((uow) => ({
28+
...uow,
29+
scheduleRequest:
30+
rule.toScheduleRequest
31+
? rule.toScheduleRequest(uow, rule)
32+
: /* istanbul ignore next */ undefined,
33+
}));
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
import sinon from 'sinon';
4+
5+
import {
6+
initialize, initializeFrom,
7+
} from '../../../src';
8+
9+
import { toDynamodbRecords, fromDynamodb } from '../../../src/from/dynamodb';
10+
11+
import Connector from '../../../src/connectors/scheduler';
12+
import { scheduler } from '../../../src/flavors/scheduler';
13+
14+
describe('flavors/scheduler.js', () => {
15+
beforeEach(() => {
16+
sinon.stub(Connector.prototype, 'schedule').resolves({});
17+
});
18+
19+
afterEach(() => {
20+
sinon.restore();
21+
});
22+
23+
it('should execute', (done) => {
24+
const events = toDynamodbRecords([
25+
{
26+
timestamp: 1572832690,
27+
keys: {
28+
pk: '1',
29+
sk: 'thing',
30+
},
31+
newImage: {
32+
pk: '1',
33+
sk: 'thing',
34+
discriminator: 'thing',
35+
name: 'Thing One',
36+
description: 'This is thing one',
37+
otherThing: 'thing|2',
38+
ttl: 1549053422,
39+
timestamp: 1548967022000,
40+
},
41+
},
42+
{
43+
timestamp: 1572832690,
44+
keys: {
45+
pk: '1',
46+
sk: 'other',
47+
},
48+
newImage: {
49+
pk: '1',
50+
sk: 'other',
51+
discriminator: 'other',
52+
name: 'Other One',
53+
description: 'This is other one',
54+
ttl: 1549053422,
55+
timestamp: 1548967022000,
56+
},
57+
},
58+
]);
59+
60+
initialize({
61+
...initializeFrom(rules),
62+
})
63+
.assemble(fromDynamodb(events), false)
64+
.collect()
65+
// .tap((collected) => console.log(JSON.stringify(collected, null, 2)))
66+
.tap((collected) => {
67+
expect(collected.length).to.equal(1);
68+
expect(collected[0].pipeline).to.equal('schedule1');
69+
expect(collected[0].event.type).to.equal('thing-created');
70+
expect(collected[0].scheduleRequest).to.deep.equal({
71+
Name: 'test schedule',
72+
ActionAfterCompletion: 'DELETE',
73+
FlexibleTimeWindow: {
74+
Mode: 'OFF',
75+
},
76+
});
77+
expect(collected[0].scheduleResponse).to.deep.equal({});
78+
})
79+
.done(done);
80+
});
81+
});
82+
83+
export const toScheduleRequest = (uow) => ({
84+
Name: 'test schedule',
85+
ActionAfterCompletion: 'DELETE',
86+
FlexibleTimeWindow: {
87+
Mode: 'OFF',
88+
},
89+
});
90+
91+
const rules = [
92+
{
93+
id: 'schedule1',
94+
flavor: scheduler,
95+
eventType: /thing-*/,
96+
filters: [() => true],
97+
toScheduleRequest,
98+
},
99+
{
100+
id: 'update-other1',
101+
flavor: scheduler,
102+
eventType: 'x9',
103+
},
104+
];

0 commit comments

Comments
 (0)