Scheduled task: node-schedule
1. Specify a specific time to execute the task
1var schedule = require ('node-schedule ');
2var date = new Date (2016, 10, 11, 23, 59, 59 );
3 // The month starts from 0.
4var j = schedule. scheduleJob (date, function (){
5 console. log ('this shouldn't be a dog abuse season! ');
6 });
2. Send an email that expires at a specified time every day.
1var registerTask = function (hour, minute, taskname ){
2 return new Promise (function (resolve, reject ){
3 var rule = new schedule. RecurrenceRule ();
4 // perform tasks at this time every day
5 rule. dayOfWeek = [0, new schedule. Range (1, 6)];
6 rule. hour = hour;
7 rule. minute = minute;
8 var j = schedule. scheduleJob (rule, function (){
9 console. log ("start sending:" + taskname );
10 resolve ();
11 });
12 });
13}
3. The time setting depends on the rule configuration.
(1), per second: rule. second = [, 2,... 59];
(2) per minute: rule. minute = [, 2,... 59];
(3) hour: rule. hour = [0, 1, 2,... 23];
(4) number of weeks: rule. dayOfWeek = [0, 1,... 7];
(5) number of months: rule. dayOfMonth = [1, 2, 3,... 31];
(6) month: rule. month = [1, 2,... 12];
4. Assign a new value upon expiration
1var schedule = require ('node-schedule ');
2var date = new Date (2016, 10, 11, 23, 59, 59 );
3var x = 'old value! ';
4var j = schedule. scheduleJob (date, function (y ){
5 console. log (y );
6}. bind (null, x ));
7x = 'New Value ';
Without an accident, the scheduled task can run;
If you are using pm2, multiple processes (such as pm2 start) must be enabled based on the number of CPU cores. /app. js-I 4). Congratulations, you will encounter an interesting problem: your scheduled task will send the same email four times;
It is suggested that the scheduled task should be run independently. This independent site only executes the scheduled task and does not start with pm2. It makes sense to think about it. The funny thing is, if multiple processes are not enabled on an independent site, emails are sent multiple times. Because mongoose is used, require in this independent site cannot reach models in the main site, therefore, I added two GET requests to the main site. The site that sent the email regularly requests the data of the emails to be sent from the main site. After sending the requests, the callback will send a request to update the sending status, the final result will still be sent repeatedly; think about it, and then I am stuck!
About a month later, I suddenly found that as a front-end, to learn Node well, the database is the biggest shortcoming! Now let's look at mongodb again. We don't need mongoose, and models on the require Main site can access the same database on these two different sites. So we can solve the previously stuck problem; ---- jump to email sending!
Email sending: nodemailer
This is a lot of problems, but it won't be a problem if it is fixed. Generally, the password problem occurs. Repeated sending will be considered as spam. The following is the full version, so far, it can be used smoothly. We recommend the following:
1var emailManage = {};
2var nodemailer = require ("nodemailer ");
3var smtpTransport = require ('nodemailer-smtp-transport ');
4
5emailManage. send = function (_ data, geter, title, htmlbody ){
6 return new Promise (function (resolve, reject ){
7 var transport = nodemailer. createTransport (smtpTransport ({
8 host: "smtp.qq.com", // host
9 port: 465, // SMTP port
10 auth :{
11 user: "915905174@qq.com", // account
12 pass: "obtained after mailbox settings, not the QQ password"
13}
14 }));
15 transport. sendMail ({
16 from: "915905174@qq.com", // sender address
17 to: geter, // receiving list
18 subject: title, // title
19 html: htmlbody
20}, function (error, response ){
21 error & reject (error );
22 resolve (_ data );
23 // transport. close (); // If not, close the connection pool.
24 });
25 });
26}
27module. exports = emailManage;
Jump back to the scheduled task. The independent site can be connected to the database of the master site, so you don't have to go to the models of the require master site. Now, the scheduled mail sending task of the slow mail section is complete;
1var mongodb = require ('mongodb ');
2var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3var db = new mongodb. Db ('dbname', server, {safe: true });
4var dbColl = null;
5var openDB = function (){
6 return new Promise (function (resolve, reject ){
7 db. open (function (err, dab ){
8 err & reject (err );
9 resolve (dab );
10 });
11 });
12}
13var getCollection = function (db, coll ){
14 return new Promise (function (resolve, reject ){
15 db. collection (coll, {safe: true}, function (err, collection ){
16 err & reject (err );
17 dbColl = collection;
18 resolve (collection );
19 });
20 });
21}
22var getFus = function (collection ){
23 return new Promise (function (resolve, reject ){
24 var dateDayStamp = new Date (). getFullYear () + '-' + (new Date (). getMonth () + 1) + '-' + new Date (). getDate ()). getTime () + '';
25 collection. find ({timeStamp: {$ lte: dateDayStamp}, sended: 0}). toArray (function (err, fus ){
26 err & reject (err );
27 resolve (fus );
28 });
29 });
30}
31var fusPromises = function (fus ){
32 if (fus. length ){
33 return fus. map (function (f ){
34 return emailManage. send (
35 {
36 fid: f. fid,
37 getter: f. getter,
38 sender: f. sender,
39 emailto: f. emailto
40 },
41 f. emailto,
42 'Hello, this is a message '+ f. timeName +' + f. sender + 'sent to you-time delivery slow ',
43 decodeURIComponent (f. body)
44 );
45 });
46}
47 return [Promise. reject ('need not to send... ')];
48 // jump directly to catch
49}
50var updateFus = function (ifs ){
51 var updateOne = function (f ){
52 return new Promise (function (resolve, reject ){
53 dbColl. update ({fid: f. fid}, {$ set: {sended: 1 }},{}, function (err ){
54 err & reject (err );
55 console. log ('sended to '+ f. emailto );
56 console. log ('+ --------------------------------- + ');
57 resolve ();
58 });
59 });
60}
61 return ifs. map (function (f ){
62 return updateOne (f );
63 });
64}
65
66 registerTask (22, 45, 'slow post ')
67. then (function (){
68 return openDB ();
69 })
70. then (function (dba ){
71 return getCollection (dba, 'Futures ');
72 })
73. then (function (collection ){
74 return getFus (collection );
75 })
76. then (function (fus ){
77 return Promise. all (fusPromises (fus ));
78 })
79. then (function (ifs ){
80 return Promise. all (updateFus (ifs ));
81 })
82. then (function (){
83 console. log ('All sended ');
84 })
85. catch (function (err ){
86 console. log (err );
87 });
Now, give Promise a thumbs up!