-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
239 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
define(function (require) { | ||
var helper = require('./helper'); | ||
|
||
/** | ||
* @constructor | ||
* @param {string} bucketName The bucket name. | ||
* @param {string} key The object name. | ||
* @param {Blob} blob 需要上传的文件内容. | ||
* @param {Object=} options 上传文件的参数. | ||
* @param {sdk.BosClient=} client The bos client. | ||
*/ | ||
function Task(bucketName, key, blob, options, client) { | ||
this.bucketName = bucketName; | ||
this.key = key; | ||
this.blob = blob; | ||
this.options = options; | ||
this.client = client; | ||
} | ||
|
||
/** | ||
* @return {sdk.Q.promise} | ||
*/ | ||
Task.prototype.run = function () { | ||
return helper.upload(this.bucketName, this.key, this.blob, this.options, this.client); | ||
}; | ||
|
||
return Task; | ||
}); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* vim: set ts=4 sw=4 sts=4 tw=120: */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
define(function (require) { | ||
var u = require('underscore'); | ||
var sdk = require('baidubce-sdk'); | ||
|
||
var config = require('./config'); | ||
|
||
/** | ||
* 上传文件夹的时候,文件的列表是异步获取的,无法直接使用 async 的接口 | ||
* @constructor | ||
*/ | ||
function TaskManager() { | ||
/** | ||
* 最多可以同时运行的任务数目. | ||
* @type {number} | ||
*/ | ||
this.parallel = config.kParallel; | ||
|
||
/** | ||
* @type {number} | ||
* 当前运行的任务数目 | ||
*/ | ||
this.running = 0; | ||
|
||
/** | ||
* FIFO | ||
* @type {Array.<Task>} | ||
*/ | ||
this.tasks = []; | ||
|
||
this.deferred = sdk.Q.defer(); | ||
} | ||
|
||
/** | ||
* @param {Task} task 需要执行的任务. | ||
*/ | ||
TaskManager.prototype.addTask = function (task) { | ||
this.tasks.push(task); | ||
this.runNext(); | ||
}; | ||
|
||
TaskManager.prototype.runNext = function () { | ||
if (this.running >= this.parallel) { | ||
return; | ||
} | ||
|
||
if (this.tasks.length <= 0) { | ||
this.deferred.resolve(); | ||
return; | ||
} | ||
|
||
// FIFO | ||
var task = this.tasks.shift(); | ||
if (task && typeof task.run === 'function') { | ||
this.running ++; | ||
task.run() | ||
.done(u.bind(this.runTaskSuccess, this)) | ||
.catch(u.bind(this.runTaskFailure, this)); | ||
} | ||
}; | ||
|
||
TaskManager.prototype.runTaskFailure = function (err) { | ||
this.deferred.reject(err); | ||
}; | ||
|
||
TaskManager.prototype.runTaskSuccess = function () { | ||
this.running--; | ||
this.runNext(); | ||
}; | ||
|
||
/** | ||
* 开始执行 this.tasks 里面的任务. | ||
* @return {sdk.Q.promise} | ||
*/ | ||
TaskManager.prototype.startup = function () { | ||
return this.deferred.promise; | ||
}; | ||
|
||
return TaskManager; | ||
}); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* vim: set ts=4 sw=4 sts=4 tw=120: */ |
Oops, something went wrong.