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

Add an option to configure concurrent transfers for async requests #1

Open
wants to merge 2 commits 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ smartling_status: {
}
```

#### options.concurrentTransfers
Type: `Integer`
Default value: `10`

When `concurrentTransfers` is provided, it will limit the number of current transfers to the provided number. This is useful to avoid hitting Smartling's API limits by requesting too many files at a time.

```js
smartling_get: {
options: {
smartling: { ... }
concurrentTransfers: 5
},
default: {
src: 'path/to/translations/*.json'
}
}
```

### Usage Examples

See Gruntfile.js for usage examples
Expand Down
3 changes: 3 additions & 0 deletions tasks/config_defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
concurrentTransfers: 10
};
10 changes: 6 additions & 4 deletions tasks/smartling_delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
'use strict';

module.exports = function (grunt) {
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
DeleteStats = require('../lib/delete-stats');
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
DeleteStats = require('../lib/delete-stats'),
configDefaults = require('./config_defaults');

grunt.registerMultiTask('smartling_delete', 'Delete files from Smartling',
SmartlingTask.make(function (task, options, sdk, done, logJson) {
var stats = new DeleteStats();
var concurrentTransfers = options.concurrentTransfers || configDefaults.concurrentTransfers;

var fileUris = task.getFileUris();

asyncUtil.eachLimit(fileUris, 10, function (fileUri, callback) {
asyncUtil.eachLimit(fileUris, concurrentTransfers, function (fileUri, callback) {
sdk.delete(fileUri)
.then(function () {
//file deleted successfully
Expand Down
15 changes: 9 additions & 6 deletions tasks/smartling_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
'use strict';

module.exports = function (grunt) {
var SmartlingTask = require('../lib/smartling-task'),
asyncUtil = require('async'),
_ = require('lodash'),
path = require('path'),
GetStats = require('../lib/get-stats');
var SmartlingTask = require('../lib/smartling-task'),
asyncUtil = require('async'),
_ = require('lodash'),
path = require('path'),
GetStats = require('../lib/get-stats'),
configDefaults = require('./config_defaults');


grunt.registerMultiTask('smartling_get', 'Get files from Smartling',
SmartlingTask.make(function (task, options, sdk, done, logJson) {
var stats = new GetStats();
var outputDirectory = options.dest;
var concurrentTransfers = options.concurrentTransfers || configDefaults.concurrentTransfers;

var fileUris = task.getFileUris();
var destFileUriFunc = options.destFileUriFunc || function(fileUri) {
Expand All @@ -44,7 +47,7 @@ module.exports = function (grunt) {
}

asyncUtil.each(locales, function(locale, localeCallback) {
asyncUtil.eachLimit(fileUris, 10, function (fileUri, fileCallback) {
asyncUtil.eachLimit(fileUris, concurrentTransfers, function (fileUri, fileCallback) {
var destFilepath;
if (outputDirectory) {
//check to modufy the destination fileUri
Expand Down
12 changes: 7 additions & 5 deletions tasks/smartling_rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
'use strict';

module.exports = function (grunt) {
var SmartlingTask = require('../lib/smartling-task'),
asyncUtil = require('async'),
path = require('path'),
RenameStats = require('../lib/rename-stats');
var SmartlingTask = require('../lib/smartling-task'),
asyncUtil = require('async'),
path = require('path'),
RenameStats = require('../lib/rename-stats'),
configDefaults = require('./config_defaults');

grunt.registerMultiTask('smartling_rename', 'Rename files in Smartling',
SmartlingTask.make(function (task, options, sdk, done, logJson) {
var stats = new RenameStats();
var concurrentTransfers = options.concurrentTransfers || configDefaults.concurrentTransfers;

var fileUris = task.getFileUris();

asyncUtil.eachLimit(fileUris, 10,
asyncUtil.eachLimit(fileUris, concurrentTransfers,
function (fileUri, callback) {
var newFileUri = options.newFileUriFunc(fileUri);

Expand Down
10 changes: 6 additions & 4 deletions tasks/smartling_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
'use strict';

module.exports = function (grunt) {
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
StatusStats = require('../lib/status-stats');
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
StatusStats = require('../lib/status-stats'),
configDefaults = require('./config_defaults');

grunt.registerMultiTask('smartling_status', 'Get status of files in Smartling',
SmartlingTask.make(function (task, options, sdk, done, logJson) {
var stats = new StatusStats();
var concurrentTransfers = options.concurrentTransfers || configDefaults.concurrentTransfers;

var fileUris = task.getFileUris();

asyncUtil.eachLimit(fileUris, 10, function(fileUri, callback) {
asyncUtil.eachLimit(fileUris, concurrentTransfers, function(fileUri, callback) {
sdk.status(fileUri, options.operation.locale)
.then(function(statusInfo) {
if (options.verbose) {
Expand Down
10 changes: 6 additions & 4 deletions tasks/smartling_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
'use strict';

module.exports = function (grunt) {
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
UploadStats = require('../lib/upload-stats');
var asyncUtil = require('async'),
SmartlingTask = require('../lib/smartling-task'),
UploadStats = require('../lib/upload-stats'),
configDefaults = require('./config_defaults');

grunt.registerMultiTask('smartling_upload', 'Upload files to Smartling',
SmartlingTask.make(function (task, options, sdk, done, logJson) {
var stats = new UploadStats();
var concurrentTransfers = options.concurrentTransfers || configDefaults.concurrentTransfers;

if (task.files) {
task.files.forEach(function (file) {
//logJson(file);

asyncUtil.eachLimit(file.src, 10, function (filepath, callback) {
asyncUtil.eachLimit(file.src, concurrentTransfers, function (filepath, callback) {
var fileUri = options.fileUriFunc(filepath);

sdk.upload(filepath, fileUri, options.operation.fileType, options.operation)
Expand Down