Skip to content
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

Adding workers option for specifying the number of workers #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ window.HAML['templates/example']

*Defined only `placement == 'global'`.*

#### workers
Type: ```number```
Default: ```twice the number of CPUs```

Limit the number of workers when running the task

Set this to 1 if you want the task to be executed
serially.

### Usage examples

``` javascript
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"node": "0.10.x"
},
"dependencies": {
"async": "^0.9.0",
"haml": "0.4.x",
"haml-coffee": "1.14.x"
},
Expand Down
12 changes: 8 additions & 4 deletions tasks/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function(grunt) {
'use strict';

var path = require('path');
var async = grunt.util.async;
var async = require('async');
var _ = grunt.util._;

grunt.registerMultiTask('haml', 'Compile Haml files', function() {
Expand Down Expand Up @@ -41,7 +41,11 @@ module.exports = function(grunt) {

// Precompile templates; if false (and target == 'js'), place rendered
// HTML in js variables.
precompile: true
precompile: true,

// Limit the number of workers to this number when running the task
// Default is twice the number of CPUs
workers: require('os').cpus().length * 2

});

Expand All @@ -52,7 +56,7 @@ module.exports = function(grunt) {
grunt.verbose.writeflags(options, 'Options');

// Transpile each src/dest group of files.
async.forEach(this.files, function(file, callback) {
async.eachLimit(this.files, options.workers, function(file, callback) {
var opts;
// Get only files that are actually there.
var validFiles = file.src.filter(function(filepath) {
Expand Down Expand Up @@ -92,7 +96,7 @@ module.exports = function(grunt) {
};

// Transpile each file.
async.map(validFiles, processFile, function (err, results) {
async.mapLimit(validFiles, options.workers, processFile, function (err, results) {
// Write the new file.
grunt.file.write(file.dest, results.join('\n'));
grunt.log.writeln('File ' + file.dest.cyan + ' created.');
Expand Down