3
私はノードJSコードをPromiseを非同期的に使用するのではなく、ステップバイステップで処理しようとしています。ノードJS次の順序でチェーン化することを約束します
まず、すべてのスプレッドシートを削除してから、それらを圧縮してから、zipを暗号化し、電子メールでzipを送信してからスプレッドシートを削除します。
//Schedules the job at a specific time
var start = schedule.scheduleJob({
hour: 19,
minute: 26,
dayOfWeek: 3
}, function() {
sendIt();
});
//Starts the Promise Chain
function sendIt() {
return deleteSpreadsheets().then(generateSpeadsheets).then(zipSpreadsheets).then(encrypt).then(sendEmail).then(deleteSpreadsheets);
}
//Deletes the current Spreadsheets in the folder
function deleteSpreadsheets() {
var promise = new Promise(function(resolve, reject) {
console.log('Deleting Spreadsheets');
var locationSpread = ['Location.xlsx'];
locationSpread.forEach(function(filename) {
if (fs.existsSync("./Spreadsheets/" + filename)) {
fs.unlink("./Spreadsheets/" + filename, (err) => {
if (err) {
console.log('Spreadsheet ' + filename + ' not found');
} else {
console.log('Spreadsheet ' + filename + ' successfully deleted');
}
});
}
});
resolve();
});
return promise;
}
//Generates the new Spreadsheets
function generateSpeadsheets() {
var promise = new Promise(function(resolve, reject) {
console.log('Generating Spreadsheets');
var locationSpread = ['Location.xlsx'];
locationSpread.forEach(function(filename) {
var query = connection.query('SELECT * from ' + filename.slice(0, -5), function(err, rows) {
var workbook = excelbuilder.createWorkbook('./Spreadsheets/', filename);
if (workbook == null) {
console.log('workbook null')
};
var sheet = workbook.createSheet(filename.slice(0, -5), 3, rows.length + 1);
if (sheet == null) {
console.log('sheet null')
};
sheet.set(1, 1, 'First Name');
sheet.set(2, 1, 'Last Name');
sheet.set(3, 1, 'Company');
for (var j = 2, z = 0; z < rows.length; j++, z++) {
sheet.set(1, j, rows[z].firstName);
sheet.set(2, j, rows[z].lastName);
sheet.set(3, j, rows[z].company);
}
workbook.save(function(err) {
console.log('workbook saved ' + (err ? 'failed' : 'ok'));
});
});
});
resolve();
});
return promise;
}
//Generates a Zip file with all the Spreadsheets
function zipSpreadsheets() {
var promise = new Promise(function(resolve, reject) {
console.log('Zipping Spreadsheets');
var zipFolder = require('zip-folder');
zipFolder('./Spreadsheets/', './Spreadsheets.zip', function(err) {
if (err) {
console.log('Failed to zip folders', err);
reject();
} else {
console.log('Successfully zipped folder');
}
});
resolve();
});
return promise;
}
//Encrypts the Spreadsheet
function encrypt() {
var promise = new Promise(function(resolve, reject) {
console.log('Encrypting');
spawn = require('child_process').spawn;
zip = spawn('zip', ['-P', 'something', 'Encrypted.zip', './Spreadsheets.zip']);
zip.on('exit', function(code) {
console.log('Finished encrypting');
resolve();
});
});
return promise;
}
//Sends the Encryped Zip as an attached in an email
function sendEmail() {
var promise = new Promise(function(resolve, reject) {
console.log("MAIL SCHEDULE RUNNING");
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email', // Your email id
pass: 'password'
}
});
var content = 'something';
var mailOptions = {
from: 'email', // sender address
to: 'email', // list of receivers
subject: 'Title', // Subject line
text: content,
attachments: [{
// file on disk as an attachment
filename: 'Encrypted.zip',
path: './Encrypted.zip' // stream this file
}]
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
reject();
} else {
console.log('Message sent: ' + info.response);
resolve();
};
});
});
return promise;
}
これは、ログに見られるように計画通りに動作していないようです。ここで
Deleting Spreadsheets
Generating Spreadsheets
Zipping Spreadsheets
Encrypting
**Spreadsheet Location.xlsx successfully deleted**
**Finished encrypting**
MAIL SCHEDULE RUNNING
**Successfully zipped folder**
**workbook saved ok**
Message sent: -----------------------------------------------
Deleting Spreadsheets
Spreadsheet Location.xlsx successfully deleted
は、それがどうあるべきかです:
Deleting Spreadsheets
**Spreadsheet Location.xlsx successfully deleted**
Generating Spreadsheets
**workbook saved ok**
Zipping Spreadsheets
**Successfully zipped folder**
Encrypting
**Finished encrypting**
MAIL SCHEDULE RUNNING
Message sent: -----------------------------------------------
Deleting Spreadsheets
Spreadsheet Location.xlsx successfully deleted
非同期アクションを待たずに即座に 'resolve()'を呼び出します。それは約束事ではなく、非同期コールバックから呼び出さなければなりません。 – Bergi
すべての非同期関数( 'unlink'、' query'、 'save'など)をそれ自身で約束して、それらを作成し、並行して待つことができるそれらのループの約束を作成することができます。 – Bergi