File tree Expand file tree Collapse file tree 6 files changed +152
-0
lines changed Expand file tree Collapse file tree 6 files changed +152
-0
lines changed Original file line number Diff line number Diff line change 1+ 2022-09-01T23:52:45.356Z, 6.6314544677734375, 11%
2+ 2022-09-01T23:53:00.489Z, 6.702445983886719, 13%
3+ 2022-09-01T23:53:15.609Z, 6.74859619140625, 14%
4+ 2022-09-01T23:53:30.727Z, 6.795112609863281, 11%
5+ 2022-09-01T23:53:45.817Z, 6.841529846191406, 11%
6+ 2022-09-01T23:54:00.899Z, 6.8905181884765625, 11%
Original file line number Diff line number Diff line change 1+ // Scheduling a simple task with node cron.
2+
3+ const cron = require ( "node-cron" ) ;
4+ const express = require ( "express" ) ;
5+
6+ const app = express ( ) ;
7+
8+ cron . schedule ( "*/15 * * * * *" , function ( ) {
9+ console . log ( "---------------------" ) ;
10+ console . log ( "running a task every 10 second" ) ;
11+ //replace with any task
12+ } ) ;
13+
14+ app . listen ( 3000 ) ;
15+
16+ /*
17+ * Run the script: `node example1.js`
18+ * To log on application
19+ * at scheduled time
20+ */
Original file line number Diff line number Diff line change 1+ const express = require ( "express" ) ;
2+ const cron = require ( "node-cron" ) ;
3+ const nodemailer = require ( "nodemailer" ) ;
4+ app = express ( ) ;
5+
6+ //send email after 1 minute
7+ cron . schedule ( "1 * * * *" , function ( ) {
8+ mailService ( ) ;
9+ } ) ;
10+
11+ function mailService ( ) {
12+ let mailTransporter = nodemailer . createTransport ( {
13+ service : "gmail" ,
14+ auth : {
15+ user : "<your-email>@gmail.com" ,
16+ // use generated app password for gmail
17+ pass : "***********" ,
18+ } ,
19+ } ) ;
20+
21+ // setting credentials
22+ let mailDetails = {
23+ from : "<your-email>@gmail.com" ,
24+ to : "<user-email>@gmail.com" ,
25+ subject : "Test Mail using Cron Job" ,
26+ text : "Node.js Cron Job Email Demo Test from Reflectoring Blog" ,
27+ } ;
28+
29+ // sending email
30+ mailTransporter . sendMail ( mailDetails , function ( err , data ) {
31+ if ( err ) {
32+ console . log ( "error occurred" , err . message ) ;
33+ } else {
34+ console . log ( "---------------------" ) ;
35+ console . log ( "email sent successfully" ) ;
36+ }
37+ } ) ;
38+ }
39+
40+ app . listen ( 3000 , ( ) => {
41+ console . log ( "application listening....." ) ;
42+ } ) ;
43+
44+ /*
45+ * Run the script: `node example2.js`
46+ * To send E-mail
47+ * at scheduled time
48+ */
Original file line number Diff line number Diff line change 1+ // Writing to a log file
2+
3+ // Importing required packages
4+ const process = require ( "process" ) ;
5+ const fs = require ( "fs" ) ;
6+ const os = require ( "os" ) ;
7+ const cron = require ( "node-cron" ) ;
8+ const express = require ( "express" ) ;
9+
10+ app = express ( ) ;
11+
12+ // setting a cron job for every 15 seconds
13+ cron . schedule ( "*/15 * * * * *" , function ( ) {
14+ let heap = process . memoryUsage ( ) . heapUsed / 1024 / 1024 ;
15+ let date = new Date ( ) . toISOString ( ) ;
16+ const freeMemory = Math . round ( ( os . freemem ( ) * 100 ) / os . totalmem ( ) ) + "%" ;
17+
18+ // date | heap used | free memory
19+ let csv = `${ date } , ${ heap } , ${ freeMemory } \n` ;
20+
21+ // storing log In .csv file
22+ fs . appendFile ( "demo.csv" , csv , function ( err ) {
23+ if ( err ) throw err ;
24+ console . log ( "server details logged!" ) ;
25+ } ) ;
26+ } ) ;
27+
28+ app . listen ( 3000 , ( ) => {
29+ console . log ( "application listening....." ) ;
30+ } ) ;
31+
32+ /*
33+ * Run the script: `node example3.js`
34+ * To generate logstatus.txt file
35+ * at scheduled time
36+ */
Original file line number Diff line number Diff line change 1+ // Deleting / Refreshing A log file.
2+ const express = require ( "express" ) ;
3+ const cron = require ( "node-cron" ) ;
4+ const fs = require ( "fs" ) ;
5+
6+ app = express ( ) ;
7+
8+ // remove the demo.csv file every twenty-first day of the month.
9+ cron . schedule ( "0 0 25 * *" , function ( ) {
10+ console . log ( "---------------------" ) ;
11+ console . log ( "deleting logged status" ) ;
12+ fs . unlink ( "./demo.csv" , err => {
13+ if ( err ) throw err ;
14+ console . log ( "deleted successfully" ) ;
15+ } ) ;
16+ } ) ;
17+
18+ app . listen ( 3000 , ( ) => {
19+ console . log ( "application listening....." ) ;
20+ } ) ;
21+
22+ /*
23+ * Run the script: `node example4.js`
24+ * To delete demo.csv file
25+ * at scheduled time
26+ */
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " new-folder-(3)" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " " ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "start" : " nodemon index.js"
8+ },
9+ "keywords" : [],
10+ "author" : " " ,
11+ "license" : " ISC" ,
12+ "dependencies" : {
13+ "node-cron" : " ^3.0.2" ,
14+ "node-mailer" : " ^0.1.1"
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments