Skip to content

Commit 6400d12

Browse files
authored
Fix Error: write after end (#44)
* Fix circleci configuration to push README.md docs
2 parents 5127261 + 1830301 commit 6400d12

File tree

14 files changed

+1991
-1161
lines changed

14 files changed

+1991
-1161
lines changed

.eslintrc.js

Lines changed: 5 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,6 @@
1-
'use strict';
2-
3-
const OFF = 'off';
4-
const ERROR = 'error';
5-
const WARN = 'warn';
6-
const ALWAYS = 'always';
7-
const NEVER = 'never';
8-
91
module.exports = {
10-
'parserOptions': {
11-
ecmaVersion: 2019
12-
},
13-
'env': {
14-
es6: true,
15-
node: true,
16-
mocha: true
17-
},
18-
'plugins': [
19-
'mocha'
20-
],
21-
'extends': 'eslint:recommended',
22-
'rules': {
23-
'indent': [
24-
ERROR,
25-
4,
26-
{
27-
SwitchCase: 1
28-
}
29-
],
30-
'linebreak-style': ERROR,
31-
'quotes': [
32-
WARN,
33-
'single',
34-
{
35-
avoidEscape: true,
36-
allowTemplateLiterals: true
37-
}
38-
],
39-
'semi': [
40-
ERROR,
41-
ALWAYS
42-
],
43-
'func-names': ERROR,
44-
'no-var': ERROR,
45-
'no-empty': ERROR,
46-
'no-empty-function': ERROR,
47-
'brace-style': [
48-
ERROR,
49-
'1tbs',
50-
{ allowSingleLine: true }
51-
],
52-
'no-multiple-empty-lines': ERROR,
53-
'no-multi-spaces': ERROR,
54-
'one-var': [
55-
ERROR,
56-
NEVER
57-
],
58-
'quote-props': [
59-
WARN,
60-
'consistent-as-needed'
61-
],
62-
'key-spacing': ERROR,
63-
'space-unary-ops': [
64-
ERROR,
65-
{
66-
words: true,
67-
nonwords: false
68-
}
69-
],
70-
'no-spaced-func': ERROR,
71-
'space-before-function-paren': [
72-
ERROR,
73-
{
74-
anonymous: ALWAYS,
75-
named: NEVER
76-
}
77-
],
78-
'arrow-body-style': [
79-
WARN,
80-
'as-needed'
81-
],
82-
'array-bracket-spacing': ERROR,
83-
'space-in-parens': ERROR,
84-
'comma-dangle': ERROR,
85-
'no-trailing-spaces': ERROR,
86-
'yoda': ERROR,
87-
'max-len': [
88-
ERROR,
89-
120
90-
],
91-
'camelcase': [
92-
ERROR,
93-
{
94-
properties: 'never'
95-
}
96-
],
97-
'new-cap': [
98-
WARN,
99-
{
100-
capIsNewExceptions: ['Q']
101-
}
102-
],
103-
'comma-style': ERROR,
104-
'curly': ERROR,
105-
'object-curly-spacing': [
106-
WARN,
107-
ALWAYS
108-
],
109-
'object-curly-newline': [
110-
ERROR,
111-
{
112-
ObjectExpression: {
113-
minProperties: 1
114-
},
115-
ObjectPattern: {
116-
multiline: true,
117-
minProperties: 5
118-
}
119-
}
120-
],
121-
'object-property-newline': ERROR,
122-
'template-curly-spacing': ERROR,
123-
'dot-notation': ERROR,
124-
'dot-location': [
125-
ERROR,
126-
'property'
127-
],
128-
'func-style': [
129-
ERROR,
130-
'declaration',
131-
{
132-
allowArrowFunctions: true
133-
}
134-
],
135-
'eol-last': ERROR,
136-
'space-infix-ops': ERROR,
137-
'keyword-spacing': ERROR,
138-
'space-before-blocks': ERROR,
139-
'no-invalid-this': ERROR,
140-
'consistent-this': ERROR,
141-
'no-this-before-super': ERROR,
142-
'no-unreachable': ERROR,
143-
'no-sparse-arrays': ERROR,
144-
'array-callback-return': ERROR,
145-
'strict': [
146-
WARN,
147-
'global'
148-
],
149-
'eqeqeq': ERROR,
150-
'no-use-before-define': WARN,
151-
'no-undef': ERROR,
152-
'no-unused-vars': WARN,
153-
'no-mixed-spaces-and-tabs': ERROR,
154-
'operator-linebreak': [
155-
ERROR,
156-
'before'
157-
],
158-
'no-console': [
159-
OFF
160-
],
161-
"mocha/no-exclusive-tests": "error"
162-
}
163-
};
2+
'extends': 'airbnb-base',
3+
'env': {
4+
'mocha': true
5+
}
6+
};

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ build/Release
2727

2828
# Dependency directory
2929
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
30-
node_modules
30+
node_modules
31+
32+
.env

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0 (October 3, 2019)
2+
3+
* Remove generators, improve eslint
4+
15
## 1.1.6 (September 10, 2019)
26

37
* Migrate from TravisCI to CircleCI

component.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"actions" : {
2828
"read_action": {
29-
"main": "./lib/read.js",
29+
"main": "./lib/triggers/read.js",
3030
"title": "Read CSV attachment",
3131
"fields": {
3232
"emitAll": {

lib/actions/write.js

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
'use strict';
2-
31
const axios = require('axios');
4-
const co = require('co');
52
const csv = require('csv');
63
const _ = require('lodash');
7-
const messages = require('elasticio-node').messages;
4+
const { messages } = require('elasticio-node');
85
const client = require('elasticio-rest-node')();
96

107
const util = require('../util/util');
@@ -21,79 +18,86 @@ let timeout;
2118
let rowCount = 0;
2219
let ax;
2320
let putUrl;
21+
22+
let readyFlag = false;
23+
2424
exports.init = async function init(cfg) {
25-
return co(function* gen() {
26-
27-
const delimiter = cfg.writer.separator || ',';
28-
const header = cfg.includeHeaders !== 'No';
29-
console.log('Using delimiter: \'%s\'', delimiter);
30-
const options = {
31-
header,
32-
delimiter
33-
};
34-
35-
if (cfg.writer.columns) {
36-
37-
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
38-
39-
console.log('Configured column names:', columns);
40-
options.columns = columns;
41-
}
42-
43-
stringifier = csv.stringify(options);
44-
signedUrl = yield client.resources.storage.createSignedUrl();
45-
putUrl = signedUrl.put_url;
46-
console.log('CSV file to be uploaded file to uri=%s', putUrl);
47-
ax = axios.create();
48-
util.addRetryCountInterceptorToAxios(ax);
49-
});
25+
const delimiter = cfg.writer.separator || ',';
26+
const header = cfg.includeHeaders !== 'No';
27+
console.log('Using delimiter: \'%s\'', delimiter);
28+
const options = {
29+
header,
30+
delimiter,
31+
};
32+
33+
if (cfg.writer.columns) {
34+
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
35+
36+
console.log('Configured column names:', columns);
37+
options.columns = columns;
38+
}
39+
40+
stringifier = csv.stringify(options);
41+
signedUrl = await client.resources.storage.createSignedUrl();
42+
putUrl = signedUrl.put_url;
43+
console.log('CSV file to be uploaded file to uri=%s', putUrl);
44+
ax = axios.create();
45+
util.addRetryCountInterceptorToAxios(ax);
46+
readyFlag = true;
5047
};
5148

5249
exports.process = async function ProcessAction(msg, cfg) {
53-
// eslint-disable-next-line consistent-this
54-
const self = this;
55-
56-
if (timeout) {
57-
clearTimeout(timeout);
58-
}
59-
60-
61-
timeout = setTimeout(() => {
62-
console.log('Closing the stream due to inactivity');
63-
co(function* gen() {
64-
const finalRowCount = rowCount;
65-
console.log('The resulting CSV file contains %s rows', finalRowCount);
66-
ax.put(putUrl, stringifier ,{
67-
method: 'PUT',
68-
timeout: REQUEST_TIMEOUT,
69-
retry: REQUEST_MAX_RETRY,
70-
delay: REQUEST_RETRY_DELAY,
71-
maxContentLength: REQUEST_MAX_CONTENT_LENGTH
72-
});
73-
stringifier.end();
74-
const messageToEmit = messages.newMessageWithBody({
75-
rowCount: finalRowCount
76-
});
77-
const fileName = messageToEmit.id + '.csv';
78-
messageToEmit.attachments[fileName] = {
79-
'content-type': 'text/csv',
80-
'url': signedUrl.get_url
81-
};
82-
signedUrl = null;
83-
rowCount = 0;
84-
console.log('Emitting message %j', messageToEmit);
85-
yield self.emit('data', messageToEmit);
86-
yield exports.init(cfg);
87-
});
88-
}, TIMEOUT_BETWEEN_EVENTS);
89-
let row = msg.body.writer;
90-
console.log(`Incoming data: ${JSON.stringify(row)}`);
91-
if (cfg.writer.columns) {
92-
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
93-
row = _.pick(row, columns);
94-
}
95-
console.log(`Writing Row: ${JSON.stringify(row)}`);
96-
stringifier.write(row);
97-
rowCount++;
98-
await this.emit('end');
50+
// eslint-disable-next-line consistent-this
51+
const self = this;
52+
53+
while (!readyFlag) {
54+
// eslint-disable-next-line no-loop-func,no-await-in-loop
55+
await new Promise(resolve => timeout(resolve, 100));
56+
}
57+
58+
if (timeout) {
59+
clearTimeout(timeout);
60+
}
61+
62+
timeout = setTimeout(async () => {
63+
readyFlag = false;
64+
65+
console.log('Closing the stream due to inactivity');
66+
67+
const finalRowCount = rowCount;
68+
console.log('The resulting CSV file contains %s rows', finalRowCount);
69+
ax.put(putUrl, stringifier, {
70+
method: 'PUT',
71+
timeout: REQUEST_TIMEOUT,
72+
retry: REQUEST_MAX_RETRY,
73+
delay: REQUEST_RETRY_DELAY,
74+
maxContentLength: REQUEST_MAX_CONTENT_LENGTH,
75+
});
76+
stringifier.end();
77+
78+
const messageToEmit = messages.newMessageWithBody({
79+
rowCount: finalRowCount,
80+
});
81+
const fileName = `${messageToEmit.id}.csv`;
82+
messageToEmit.attachments[fileName] = {
83+
'content-type': 'text/csv',
84+
url: signedUrl.get_url,
85+
};
86+
signedUrl = null;
87+
rowCount = 0;
88+
console.log('Emitting message %j', messageToEmit);
89+
await self.emit('data', messageToEmit);
90+
}, TIMEOUT_BETWEEN_EVENTS);
91+
92+
let row = msg.body.writer;
93+
console.log(`Incoming data: ${JSON.stringify(row)}`);
94+
if (cfg.writer.columns) {
95+
const columns = Object.keys(_.keyBy(cfg.writer.columns, 'property'));
96+
row = _.pick(row, columns);
97+
}
98+
console.log(`Writing Row: ${JSON.stringify(row)}`);
99+
stringifier.write(row);
100+
rowCount += 1;
101+
102+
await self.emit('end');
99103
};

0 commit comments

Comments
 (0)