multi-tasks 1.0.7 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,22 @@
1
1
  # multi-tasks
2
2
  multi-tasks
3
+
4
+ Install:
5
+
6
+ ```javascript
7
+ npm install multi-tasks
8
+ ```
9
+
10
+ Have a try:
11
+
12
+ ```shell
13
+ node example/run
14
+ ```
15
+
16
+ Changelog:
17
+
18
+ - 1.1.1 Rename files, updated changelog
19
+ - 1.1.0 Simplified the usage of a customized Consumer, see example0
20
+ - 1.0.8 Fix examples
21
+ - 1.0.7 Remove moment
22
+ - 1.0.6 Performance optimization
@@ -0,0 +1,16 @@
1
+ let SuperConsumer = require('../index').SuperConsumer;
2
+
3
+ class TestConsumer extends SuperConsumer {
4
+
5
+ process(taskInfo, actionData){
6
+ console.log(`process data:`, actionData);
7
+ return {
8
+ result: {
9
+ message: 'this is a result from a demo, random=' + Math.random(),
10
+ ...taskInfo,
11
+ ...actionData
12
+ }
13
+ }
14
+ }
15
+ }
16
+ module.exports = TestConsumer;
@@ -0,0 +1,35 @@
1
+ let SuperProducer = require('../index').SuperProducer;
2
+ class TestProducer extends SuperProducer {
3
+
4
+ // constructor
5
+ constructor(taskname, parentfolder) {
6
+ super(taskname, parentfolder)
7
+
8
+ }
9
+ setTaskTrunkInfo(trunkInfo){
10
+ this.trunkInfo = trunkInfo;
11
+ }
12
+ getAllActions() {
13
+ /**
14
+ * Generate all your tasks in this method, return as an array;
15
+ * These data will be allocated to multiple threads (workers) for running;
16
+ * You can obtain each of the data and process it in the 'process' method of your consumer.
17
+ *
18
+ * **/
19
+
20
+ return new Promise((resolve, reject)=>{
21
+ let trunkInfo = this.trunkInfo;
22
+ let alltasks = []
23
+ for(let i=0;i<200;i++){
24
+ let task_props = {
25
+ index: i,
26
+ name: `task-${i}`,
27
+ description: `This is a subtask's prop`
28
+ };
29
+ alltasks.push(task_props);
30
+ }
31
+ resolve(alltasks);//return all your tasks as an array
32
+ })
33
+ }
34
+ }
35
+ module.exports = TestProducer;
@@ -1,7 +1,5 @@
1
- let fs = require('fs');
2
- let pathutil = require('path');
3
- let RunnerProducer = require('./RunnerProducer')
4
- let RunnerConsumer = require('./RunnerConsumer')
1
+ let RunnerProducer = require('./RunnerProducer');//switch to require from node_modules in your project
2
+ let RunnerConsumer = require('./RunnerConsumer');//switch to require from node_modules in your project
5
3
 
6
4
  let taskId = Math.random()+'';
7
5
  let multi_task_parent_folder = `C:/tmp-demo`;
@@ -3,9 +3,14 @@ let SuperConsumer = require('../index').SuperConsumer;
3
3
 
4
4
  let _do = (mainTaskInfo, actions, callback)=>{
5
5
  if(actions.length === 0) return callback({data:'hahaha'+Math.random()});
6
- let act = actions.shift();
6
+ let action = actions.shift();
7
+
8
+ //start to write your process code here
9
+
10
+ console.log('action', action)
11
+
12
+ //end of your process code.
7
13
 
8
- console.log('act', act)
9
14
  _do(mainTaskInfo, actions, callback);
10
15
  };
11
16
 
@@ -0,0 +1,35 @@
1
+ let SuperProducer = require('../index').SuperProducer;
2
+ class TestProducer extends SuperProducer {
3
+
4
+ // constructor
5
+ constructor(taskname, parentfolder) {
6
+ super(taskname, parentfolder)
7
+
8
+ }
9
+ setTaskTrunkInfo(trunkInfo){
10
+ this.trunkInfo = trunkInfo;
11
+ }
12
+ getAllActions() {
13
+ /**
14
+ * Generate all your tasks in this method, return as an array;
15
+ * These data will be allocated to multiple threads (workers) for running;
16
+ * You can obtain each of the data and process it in the 'process' method of your consumer.
17
+ *
18
+ * **/
19
+
20
+ return new Promise((resolve, reject)=>{
21
+ let trunkInfo = this.trunkInfo;
22
+ let alltasks = []
23
+ for(let i=0;i<200;i++){
24
+ let task_props = {
25
+ index: i,
26
+ name: `task-${i}`,
27
+ description: `This is a subtask's prop`
28
+ };
29
+ alltasks.push(task_props);
30
+ }
31
+ resolve(alltasks);//return all your tasks as an array
32
+ })
33
+ }
34
+ }
35
+ module.exports = TestProducer;
@@ -0,0 +1,23 @@
1
+ let RunnerProducer = require('./RunnerProducer');//switch to require from node_modules in your project
2
+ let RunnerConsumer = require('./RunnerConsumer');//switch to require from node_modules in your project
3
+
4
+ let taskId = Math.random()+'';
5
+ let multi_task_parent_folder = `C:/tmp-demo`;
6
+
7
+ let producer = new RunnerProducer(`workers_of_${taskId}`, multi_task_parent_folder)
8
+ let consumer = new RunnerConsumer();
9
+ let master = require('../index').master;
10
+
11
+ console.log(producer.getTaskName())
12
+ //return;
13
+
14
+ let taskConfig = {
15
+ multi_task_parent_folder
16
+ }
17
+ //console.log(resp)
18
+ producer.setTaskTrunkInfo(taskConfig);
19
+ consumer.setTaskTrunkInfo(taskConfig)
20
+ master.run(producer, consumer, {
21
+ quotaOfEachTask:2,
22
+ numberOfWorks: 6
23
+ });
@@ -1,5 +1,4 @@
1
1
  let fs = require('fs');
2
- const jsonFormat = require('json-format');
3
2
  let pathutil = require('path');
4
3
  const { spawn, exec } = require('child_process');
5
4
  let SuperConsumer = require('../index').SuperConsumer;
@@ -120,7 +119,7 @@ let _do = (mainTaskInfo, actions, callback)=>{
120
119
  console.log('processing...', process.pid, fpath);
121
120
  processTask(moduleName, requestVersion, (succ, logJson)=>{
122
121
  if(succ){
123
- fs.writeFileSync(succfPath, jsonFormat(logJson));
122
+ fs.writeFileSync(succfPath, JSON.stringify(logJson));
124
123
  logJson.response.forEach((item)=>{
125
124
  if(item.dependencies){
126
125
  for(let module2 in item.dependencies){
@@ -132,7 +131,7 @@ let _do = (mainTaskInfo, actions, callback)=>{
132
131
  });
133
132
  }else{
134
133
  console.log('ERR! failed', moduleName, requestVersion);
135
- fs.writeFileSync(pathutil.resolve(dynamicTasksFolderFail, `${fullnameMd5}.json`), jsonFormat({
134
+ fs.writeFileSync(pathutil.resolve(dynamicTasksFolderFail, `${fullnameMd5}.json`), JSON.stringify({
136
135
  moduleName, requestVersion,
137
136
  msg: logJson
138
137
  }));
@@ -1,7 +1,6 @@
1
1
  let SuperProducer = require('../index').SuperProducer;
2
2
  const fs = require('fs');
3
3
  let pathutil = require('path');
4
- const jsonFormat = require('json-format');
5
4
 
6
5
  let thisroot = pathutil.parse(__filename).dir;
7
6
 
@@ -1,8 +1,8 @@
1
1
  let fs = require('fs');
2
2
  let pathutil = require('path');
3
3
  let makeDir = require('make-dir');
4
- let RunnerProducer = require('./RunnerProducer')
5
- let RunnerConsumer = require('./RunnerConsumer')
4
+ let RunnerProducer = require('./RunnerProducer');//switch to require from node_modules in your project
5
+ let RunnerConsumer = require('./RunnerConsumer');//switch to require from node_modules in your project
6
6
 
7
7
  let thisroot = pathutil.parse(__filename).dir;
8
8
  let taskId = `workers_of_${Math.random()}`;
@@ -1,7 +1,6 @@
1
1
  const md5 = require('md5');
2
2
  const fs = require('fs');
3
3
  let pathutil = require('path');
4
- const jsonFormat = require('json-format');
5
4
 
6
5
  let getFileName = (moduleName, requestVersion)=>{
7
6
  let fullname = `${moduleName}@${requestVersion}`;
@@ -10,7 +9,7 @@ let getFileName = (moduleName, requestVersion)=>{
10
9
  };
11
10
  let createNewTask = (moduleName, requestVersion, dynamicTasksFolderNew)=>{
12
11
  let fullnameMd5 = getFileName(moduleName, requestVersion);
13
- fs.writeFileSync(pathutil.resolve(dynamicTasksFolderNew, `./${fullnameMd5}.json`), jsonFormat({
12
+ fs.writeFileSync(pathutil.resolve(dynamicTasksFolderNew, `./${fullnameMd5}.json`), JSON.stringify({
14
13
  info:{
15
14
  moduleName,
16
15
  requestVersion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "multi-tasks",
3
- "version": "1.0.7",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -1,18 +1,44 @@
1
1
  let fs = require('fs');
2
2
  let pathutil = require('path');
3
+
3
4
  class SuperConsumer {
4
5
  // constructor
5
6
  constructor(mainid, subid) {
6
7
  this.mainid = mainid;
7
8
  this.subid = subid;
9
+ this.results = [];
10
+ }
11
+
12
+ setTaskTrunkInfo(trunkInfo){
13
+ this.trunkInfo = trunkInfo;
8
14
  }
9
- run (taskInfo, callback){
10
- //rewrite this
11
- let succ = (Math.random()*1000)%3===0?true:false;
12
- let result = {
13
- succ
15
+ _run (mainTaskInfo, actions, callback){
16
+ if(actions.length === 0) return callback({
17
+ finishedAt: new Date()*1,
18
+ results:this.results
19
+ });
20
+ let taskInfo = {
21
+ mainid: this.mainid,
22
+ subid: this.subid
23
+ };
24
+ let actionData = actions.shift();
25
+
26
+ if(this.process){
27
+ let result = this.process(taskInfo, actionData);//rewrite this method in subclass
28
+ if(result){
29
+ this.results.push(result);
30
+ }
14
31
  }
15
- callback(result);
32
+
33
+ this._run(mainTaskInfo, actions, callback);
34
+ };
35
+ run (mainTaskInfo, subTaskInfo, taskInfo, callback){
36
+ let actions = subTaskInfo.actions;
37
+ mainTaskInfo.defination = this.trunkInfo.defination;
38
+ this._run(mainTaskInfo, actions, (result)=>{
39
+ callback(result)
40
+ })
41
+
16
42
  }
17
43
  runTasks (mainTaskInfo, subTaskInfo, message){
18
44
  // console.log(`mainTaskInfo:`, mainTaskInfo)
@@ -1,29 +0,0 @@
1
- let SuperProducer = require('../index').SuperProducer;
2
- class TestProducer extends SuperProducer {
3
-
4
- // constructor
5
- constructor(taskname, parentfolder) {
6
- super(taskname, parentfolder)
7
-
8
- }
9
- setTaskTrunkInfo(trunkInfo){
10
- this.trunkInfo = trunkInfo;
11
- }
12
- getAllActions() {
13
-
14
- return new Promise((resolve, reject)=>{
15
- let trunkInfo = this.trunkInfo;
16
- //console.log(this.trunkInfo)
17
- //throw repos.length+'ff'+repos2.length
18
- //here, IGNORED_repoRegexStringList
19
- let alltasks = []
20
- for(let i=0;i<200;i++){
21
- alltasks.push({
22
- num:i
23
- })
24
- }
25
- resolve(alltasks);
26
- })
27
- }
28
- }
29
- module.exports = TestProducer;