Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This was typed up quickly from memory. I haven't tested this code yet, but you get the general idea. I'm also brand new at JavaScript so please don't judge.

'use strict' var cluster = require('cluster'); var spawn = require('child_process').spawn; var stream = require('stream'); var glob = require('glob'); var files = glob.sync('/data/logs/*.log.gz'); function Parser() { var t = new stream.Transform( { objectMode: true } ); var transform = t._transform; var flush = t._flush; var parse = t.parse; return t; } function transform(chunk, enc, done) { var data = chunk.toString(); //if flush gives us remaining data from last line, prepend to next + chunk if (this._lastLineData) data = this._lastLineData + data; var lines = data.split('\n'); this._lastLineData = lines[lines.length-1]; for (var i = 0; i < lines.length; i++) { this.push(parse.lines[i]); } done(); } function flush(done) { //If remaining data, pass to transform to prepend to next chunk if (this._lastLineData) this.push(this._lastLineData); this._lastLineData = null done() } function parse(line) { //Split line into fields, or apply regex to filter results return line; } if (cluster.isMaster) { var workers = 24; // number of CPUs var logsPerWorker = workers / files; var remainder = workers % files; var logSlice {}; var sliceStart = 0; var sliceEnd = 0; for (var i = 0; i < workers; i++) { if (remainder) { sliceEnd++; remainder--; } logSlice[i] = files.slice(sliceStart,sliceEnd); sliceStart++; sliceEnd = sliceStart; if (logsPerWorker === 0 && remainder === 0) { //if no more logs to distribute, break out of loop break; } } for (var i = 0; i < workers; i++) { //Fork and pass list of logs for child to process cluster.fork({logs: logSlice[i]}); } } else { var liner = Parser(); var logs = split.env.logs(','); //read logs passed by parent for (var i = 0; i < logs.length; i++){ var child = spawn('zcat', [ logs[i] ]); child.stdout.pipe(liner); //pipe this to stdout or to writeStr +eam to write to a file } }

In reply to Re^2: Help me beat NodeJS by rickyw59
in thread Help me beat NodeJS by rickyw59

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found