log file not created when date change nodejs - javascript

i have problem when i try make log for everytime date change it make new file and stop log when end date , for example
20200709_Chatlog.txt for
2020-07-09 21:56:12:91 - connect null
2020-07-09 21:56:15:952 - a user connected
20200710_Chatlog.txt for
2020-07-10 21:56:12:91 - connect null
2020-07-10 21:56:15:952 - a user connected
but my file is always overwrite in the same file date like this
20200709_Chatlog.txt
2020-07-09 21:56:12:91 - connect null
2020-07-09 21:56:15:952 - a user connected
2020-07-10 21:56:12:91 - connect null
2020-07-10 21:56:15:952 - a user connected
, but everytime i restart the server new file created
my code is
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function logDate(){
var d = new Date();
var yy = addZero(d.getFullYear());
var mm = addZero(d.getMonth() + 1);
var dd = addZero(d.getDate());
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
var ms = addZero(d.getMilliseconds(), 3);
var tanggal = "";
return tanggal = +yy+ "-" +mm+ "-" +dd+ " " +h+ ":" +m+ ":" +s+ ":" +ms + " ";
}
function fileDate(){
var d = new Date();
var yy = addZero(d.getFullYear());
var mm = addZero(d.getMonth() + 1);
var dd = addZero(d.getDate());
var tanggal = "";
return tanggal = +yy+ "" +mm+ "" +dd+ "_";
}
var fs = require('fs');
var util = require('util');
var logFile = fs.createWriteStream(fileDate()+'Chatlog.txt', { flags: 'a' }); // 'w' to truncate
var logStdout = process.stdout;
console.log = function () {
logFile.write(logDate()+" - " + util.format.apply(null, arguments)+ '\n');
logStdout.write(logDate()+" - " + util.format.apply(null, arguments) + '\n');
}
console.error = console.log;
am i do something wrong, please help , and thanks for helping , sorry if my english bad

Related

solving the [object Object] issue

I'm trying to separate concerns using modules in Js eveything is going Ok except that I want to display the time of the system and I did that too but when I output it I got an [object Object] string and what proves me right that really I got the right system Time is that when I console logged the interCtrl.getTheTime I got the time of the system and here is a screenshot:
here is my code to let you understand better:
JS:
var controller = (function(interCtrl, UICtrl) {
//1.get the time
setInterval(document.querySelector(".Time").innerHTML = interCtrl.getTime(),100);
// console.log(interCtrl.getTime());
//2.get the date
document.querySelector(".subDate").innerHTML = interCtrl.date();
};
document.querySelector(".add__btn").addEventListener("click", ctrlAddPlans);
document.addEventListener("keypress", function(e) {
if (e.keyCode === 13) {
ctrlAddPlans();
}
});
})(internalController);
var internalController = (function() {
//some code
function getTheDate() {
var newDate;
var d = new Date();
var day = d.getDate();
var year = d.getFullYear();
var month = d.getMonth() + 1;
// var x=1;
if (month.toString().length < 2) {
month = " 0" + month + " ";
}
var date = " %11%/%04%/%2019%";
newDate = date.replace("%11%", day + " ");
newDate = newDate.replace("%04%", month);
newDate = newDate.replace("%2019%", " " + year);
return newDate;
}
function getTheTime() {
//.get the timeDiv
// const timeDiv =
//.get the time (h,m,s)
var date = new Date();
var hours = date.getHours();
if (hours.toString().length < 2) {
hours = `0${hours}`;
}
var mins = date.getMinutes();
if (mins.toString().length < 2) {
mins = `0${mins}`;
}
var seconds = date.getSeconds();
return {
time: hours+ " : "+ mins+" : "+seconds
};
}
return {
date: function getDate() {
return getTheDate();
},
getTime: function getTime() {
return getTheTime();
}
};
})();
I saw this question but it didn't help me what does [object Object] mean?
so any help please and thank you in advance
The output of the getTime function is an object. To make it output a string, you can change its return statement from
return {
time: hours+ " : "+ mins+" : "+seconds
};
to
return hours + " : " + mins+" : " + seconds;
Or, alternatively, if you want it to be an object, you'll have to access its time property to get the value. Like
var timeObj = interCtrl.getTheTime();
var timeString = timeObj.time;

JavaScript extract continues failing intermittently

I've taken on a new role in the tech support team and tasked to resolve an on going and intermittent issue with one of our nightly data extracts failing.
The extract process sends a data file every night containing a record of transactions completed that day. A scheduled task runs each night, with a second run if the first failed, then sends an email notification based on the status.
The scheduled task opens a CMD file which then runs a JS script command to execute a custom stored procedure on the SQL database.
Each night the file is replaced with a new file and this logic writes the status to file.
The below is the output log information which indicates an invalid call error:
Custom_Extract_Second_Run.js started at Tuesday, 19 January 2016 8:15:00 PM
C:\DataExtract\Custom_Extract_Second_Run.js(341, 4) Microsoft JScript runtime error: Invalid procedure call or argument
Custom_Extract.js started at Wednesday, 20 January 2016 8:03:00 PM
Script causes an exception.
Exception number: 0x5007
Description:'objRs.EOF' is null or not an object
Task ended at Wednesday, 20 January 2016 8:08:13 PM. Total Messages Sent: 0
Custom_Extract_Second_Run.js started at Wednesday, 20 January 2016 8:15:00 PM
Task ended at Wednesday, 20 January 2016 8:20:00 PM. Total Messages Sent: 1
Custom_Extract.js started at Thursday, 21 January 2016 8:03:01 PM
Script causes an exception.
Exception number: 0x5007
Description:'objRs.EOF' is null or not an object
Task ended at Thursday, 21 January 2016 8:07:56 PM. Total Messages Sent: 0
Custom_Extract_Second_Run.js started at Thursday, 21 January 2016 8:15:00 PM
Script causes an exception.
Exception number: 0x3704
Description:Operation is not allowed when the object is closed.
Task ended at Thursday, 21 January 2016 8:19:49 PM. Total Messages Sent: 0
Custom_Extract.js started at Friday, 22 January 2016 8:03:00 PM
C:\DataExtract\Custom_Extract.js(318, 4) Microsoft JScript runtime error: Invalid procedure call or argument
Custom_Extract_Second_Run.js started at Friday, 22 January 2016 8:15:00 PM
C:\DataExtract\Custom_Extract_Second_Run.js(341, 4) Microsoft JScript runtime error: Invalid procedure call or argument
We had to reverse-engineer the scripts used to work out how it actually behaves, however not at a confident level to make changes. The extract logic was developed a long time ago (10 to 12 years ago), therefore likely very old and outdated logic.
Below is the javascript logic used in the extract.
var QUERY_NAME_1 = "Custom_Extract";
var QUERY_NAME_2 = "Custom_Extract_UpdateDate";
var LOG_FILE_NAME = "DataExtract_error.log";
var EXTRACT_FILE_NAME = "Extract";
var DB_SERVER_NAME = "sql6";
var DB_DATABASE_NAME = "database_name";
var DB_DATABASE_USER_NAME = "username";
var DB_DATABASE_PASSWORD = "password";
var DateTimeNow = new Date();
var DATE_VALUE = DateTimeNow.getDate() + "/" + (DateTimeNow.getMonth() + 1) + "/" + DateTimeNow.getFullYear();
var EMAIL_SUBJECT_HEADER = "Email Extract Header - Extract Date - " + DATE_VALUE;
var EMAIL_SUBJECT = EMAIL_SUBJECT_HEADER + " Successful";
var EMAIL_SUBJECT_ERROR = "Unsuccessful - " + EMAIL_SUBJECT_HEADER;
var EmailCount = 0;
var DELIMITER = "|";
var STRING_SQL_FAILED = "SQL Failed";
var strScript = "";
var strMsg = "";
var ARG_REPORT_PATH = "";
var ARG_MAIL_SERVER = "";
var ARG_KEEP_BACKUP_COPIES = 0;
var ARG_EMAIL_FROM = "";
var ARG_EMAIL_TO = "";
function ParseCommandLineParams() {
var cmdArgs = WScript.Arguments;
for (i = 0; i < cmdArgs.length; i++) {
// Get next argument
arg = cmdArgs(i);
// Each argument is in the form a=b. Split the two components
j = arg.search(/=/);
if (j == -1) {
WScript.Echo("Invalid arguments arg = " + arg);
return false;
}
argName = arg.substring(0, j);
argValue = arg.substring(j + 1, arg.length);
switch (argName) {
case "/reportPath":
ARG_REPORT_PATH = argValue;
break;
case "/mailServer":
ARG_MAIL_SERVER = argValue;
break;
case "/keepBackupCopies":
ARG_KEEP_BACKUP_COPIES = parseInt(argValue);
break;
case "/from":
ARG_EMAIL_FROM = argValue;
break;
case "/to":
ARG_EMAIL_TO = argValue;
break;
default:
WScript.Echo("Invalid arguments. Name = '" + argName + "' Value = '" + argValue + "'");
return false;
}
}
return true;
}
function DisplayUsage() {
WScript.Echo("Usage: cscript DataExtract.js");
WScript.Echo(" [/m=<Report path directory where it creates file>]");
WScript.Echo(" [/from=<from-line in email>] [/to=<to-line in email>]");
WScript.Echo(" [/machinename=<Email Server name>] [/databaseConfig=<Database Config Setting file>]");
WScript.Echo(" ");
WScript.Echo(" /m - unc path to mail directory (default: ..\\reports\\)");
WScript.Echo(" /from - from-line in email (default: Tejas Magia <>");
WScript.Echo(" /to - to-line in email (default: Tejas Magia <>");
WScript.Echo(" /machinename - email server name (default: EXCHANGE2 ");
WScript.Echo(" /databaseConfig - DataBase Setup Config (default: ..\\..\\..\\..\\config\\config.txt ");
return;
}
function DisplayParameters() {
WScript.Echo("Parameters:");
WScript.Echo(" ARG_REPORT_PATH=[" + ARG_REPORT_PATH + "]");
WScript.Echo(" ARG_EMAIL_FROM=[" + ARG_EMAIL_FROM + "]");
WScript.Echo(" ARG_EMAIL_TO=[" + ARG_EMAIL_TO + "]");
WScript.Echo(" ARG_MAIL_SERVER=[" + ARG_MAIL_SERVER + "]");
WScript.Echo(" DB_SERVER_NAME=[" + DB_SERVER_NAME + "]");
WScript.Echo(" DB_DATABASE_NAME=[" + DB_DATABASE_NAME + "]");
return;
}
function GetConfigCDO() {
var objConf = WScript.CreateObject("CDO.Configuration");
objFields = objConf.Fields;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/sendusing") = 2;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ARG_MAIL_SERVER;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/smtpserverport") = 25;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30;
objFields.Update();
return objConf;
}
function GetDatabaseConnection() {
// SQLOLEDB is very sensitive to noise between client & server, and always mistaken certain
// noise as returned result set; ODBC is better way to go for now
var objCon = WScript.CreateObject("ADODB.Connection");
objCon.Open(
"Driver={Sql Server};APP=" + strScript +
";Server=" + DB_SERVER_NAME +
";Database=" + DB_DATABASE_NAME +
";user id=" + DB_DATABASE_USER_NAME +
";password=" + DB_DATABASE_PASSWORD +
";OLE DB Services=0"
);
return objCon;
}
//
// BEGIN MAIN PROGRAM
//
strMsg = "**************************************************";
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
strScript = String(WScript.ScriptName);
DateTimeNow = new Date();
strMsg = strScript + " started at " + DateTimeNow.toLocaleString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
if (!ParseCommandLineParams()) {
DisplayUsage();
WScript.Quit(0);
}
DisplayParameters();
try {
var objCon = GetDatabaseConnection();
var objCmd = WScript.CreateObject("ADODB.Command");
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_1;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
var objParam = objCmd.CreateParameter("Return", 3, 4); // adInteger, adParamReturnValue
objCmd.Parameters.Append(objParam);
WScript.Echo("Before execution");
if (ARG_KEEP_BACKUP_COPIES == 1) {
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt")) {
var File3 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
File3.Delete();
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt")) {
var File2 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
File2.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt")) {
var File1 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
File1.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt")) {
var File0 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt");
File0.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
}
}
var objRs = null;
try {
objRs = objCmd.Execute();
} catch (er) {
try {
objRs = objCmd.Execute();
} catch (er2) {
try {
objRs.Close();
} catch (er3) {
WScript.Echo("Error found even after 2nd run attempt!");
}
var strError = "First run failed";
// Send error email
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
// objMsg.To = ARG_EMAIL_TO;
objMsg.To = "support#sumtotal.com.au";
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = strError;
objMsg.send();
// Log error in output file
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
objTStreamLE.WriteLine(strError);
objTStreamLE.Close();
}
}
if (objRs.EOF) {
objRs.Close();
// check return code after closing the record set
var rc = objCmd(0);
if (rc == 100010) {
WScript.Echo("Another instance is running. Bail out");
} else if (rc == 0) {
WScript.Echo("Found no records for users");
} else {
var objError = new Error(rc, STRING_SQL_FAILED);
throw objError;
}
} else {
// Update Last Update Date
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_2;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objCmd.Execute();
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
while (true) {
if (objRs.EOF) {
// we have to send the last mail
strContentType = "";
} else {
var strApplicantID = new String(objRs("EmpID"));
var strCourse_Code = new String(objRs("Event_Code"));
var strCourse_Name = new String(objRs("Course_Name"));
var strCompletion_Date = new String(objRs("Completion_Date"));
var strGrade_Name = new String(objRs("Grade_Name"));
var strBodyInfo = strApplicantID + DELIMITER +
strCourse_Code + DELIMITER +
strCourse_Name + DELIMITER +
strCompletion_Date + DELIMITER +
strGrade_Name;
WScript.Echo(strBodyInfo);
objTStreamLE.WriteLine(strBodyInfo);
}
if (objRs.EOF) {
break;
}
objRs.MoveNext;
}
objTStreamLE.Close();
objRs.Close();
}
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT;
objMsg.TextBody = EMAIL_SUBJECT;
objMsg.send();
EmailCount++; // How many email sent. it will be always one email in this case.
objCon.Close();
} catch (e) {
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
var objTStreamErrorLog = objFSO.CreateTextFile(errorFile, true);
objTStreamErrorLog.WriteLine(strMsg);
if (e.description == STRING_SQL_FAILED) {
strMsg = "Failed to execute " + QUERY_NAME_1 + ".\nReturn code:" + String(e.number);
} else {
strMsg = "Script causes an exception.\nException number: 0x" + String(e.number & 0xFFFF) + "\nDescription:" + e.description;
}
objTStreamErrorLog.WriteLine(strMsg);
objTStreamErrorLog.close();
var errorFile = ARG_REPORT_PATH + LOG_FILE_NAME;
var theFile_1_Abs = objFSO.GetAbsolutePathName(errorFile);
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO + ";" + ARG_EMAIL_FROM;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = "The attached file contains a error log file of any issues that arose during the DataExtract process";
objMsg.AddAttachment(theFile_1_Abs);
objMsg.send();
WScript.StdErr.WriteLine(strMsg);
WScript.Echo(strMsg);
objFSO.DeleteFile(errorFile); // delete log file generated.
var objShell = WScript.CreateObject("WScript.Shell");
if (!objShell.LogEvent(1, "iX Learning Server Task(" + strScript + "): " + strMsg)) {
WScript.StdErr.WriteLine("Failed to log an event");
}
}
DateTimeNow = new Date();
strMsg = "Task ended at " + DateTimeNow.toLocaleString() + ". Total Messages Sent: " + EmailCount.toString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
//
// END MAIN PROGRAM
//
2nd run
var QUERY_NAME_1 = "Custom_Extract_Second_Run";
var QUERY_NAME_2 = "Custom_Extract_UpdateDate";
var QUERY_NAME_3 = "Custom_Extract_GetRunStatus";
var LOG_FILE_NAME = "DataExtract_error.log";
var EXTRACT_FILE_NAME = "Extract";
var DB_SERVER_NAME = "sql6";
var DB_DATABASE_NAME = "database_name";
var DB_DATABASE_USER_NAME = "username";
var DB_DATABASE_PASSWORD = "password";
var DateTimeNow = new Date();
var DATE_VALUE = DateTimeNow.getDate() + "/" + (DateTimeNow.getMonth()+1) + "/" + DateTimeNow.getFullYear();
var EMAIL_SUBJECT_HEADER = "Email Extract Header - Extract Date - " + DATE_VALUE;
var EMAIL_SUBJECT = EMAIL_SUBJECT_HEADER + " Successful";
var EMAIL_SUBJECT_ERROR = "Unsuccessful - " + EMAIL_SUBJECT_HEADER;
var EmailCount = 0;
var DELIMITER = "|";
var STRING_SQL_FAILED = "SQL Failed";
var strScript = "";
var strMsg = "";
var ARG_REPORT_PATH = "";
var ARG_MAIL_SERVER = "";
var ARG_KEEP_BACKUP_COPIES = 0;
var ARG_EMAIL_FROM = "";
var ARG_EMAIL_TO = "";
function ParseCommandLineParams()
{
var cmdArgs = WScript.Arguments;
for(i = 0; i < cmdArgs.length; i++)
{
// Get next argument
arg = cmdArgs(i);
// Each argument is in the form a=b. Split the two components
j = arg.search(/=/);
if(j == -1)
{
WScript.Echo("Invalid arguments arg = " + arg);
return false;
}
argName = arg.substring(0, j);
argValue = arg.substring(j+1, arg.length);
switch(argName)
{
case "/reportPath":
ARG_REPORT_PATH = argValue;
break;
case "/mailServer":
ARG_MAIL_SERVER = argValue;
break;
case "/keepBackupCopies":
ARG_KEEP_BACKUP_COPIES = parseInt(argValue);
break;
case "/from":
ARG_EMAIL_FROM = argValue;
break;
case "/to":
ARG_EMAIL_TO = argValue;
break;
default:
WScript.Echo("Invalid arguments. Name = '" + argName + "' Value = '" + argValue + "'");
return false;
}
}
return true;
}
function DisplayUsage()
{
WScript.Echo("Usage: cscript DataExtract.js");
WScript.Echo(" [/m=<Report path directory where it creates file>]");
WScript.Echo(" [/from=<from-line in email>] [/to=<to-line in email>]");
WScript.Echo(" [/machinename=<Email Server name>] [/databaseConfig=<Database Config Setting file>]");
WScript.Echo(" ");
WScript.Echo(" /m - unc path to mail directory (default: ..\\reports\\)");
WScript.Echo(" /from - from-line in email (default: Tejas Magia <>");
WScript.Echo(" /to - to-line in email (default: Tejas Magia <>");
WScript.Echo(" /machinename - email server name (default: EXCHANGE2 ");
WScript.Echo(" /databaseConfig - DataBase Setup Config (default: ..\\..\\..\\..\\config\\config.txt ");
return;
}
function DisplayParameters()
{
WScript.Echo("Parameters:");
WScript.Echo(" ARG_REPORT_PATH=[" + ARG_REPORT_PATH + "]");
WScript.Echo(" ARG_EMAIL_FROM=[" + ARG_EMAIL_FROM + "]");
WScript.Echo(" ARG_EMAIL_TO=[" + ARG_EMAIL_TO +"]");
WScript.Echo(" ARG_MAIL_SERVER=["+ ARG_MAIL_SERVER +"]");
WScript.Echo(" DB_SERVER_NAME=[" + DB_SERVER_NAME + "]");
WScript.Echo(" DB_DATABASE_NAME=[" + DB_DATABASE_NAME+ "]");
return;
}
function GetConfigCDO()
{
var objConf = WScript.CreateObject("CDO.Configuration");
objFields = objConf.Fields;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/sendusing") = 2;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ARG_MAIL_SERVER;
objFields.Item("http://schemas.Microsoft.com/cdo/configuration/smtpserverport") = 25;
objFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30;
objFields.Update();
return objConf;
}
function GetDatabaseConnection()
{
// SQLOLEDB is very sensitive to noise between client & server, and always mistaken certain
// noise as returned result set; ODBC is better way to go for now
var objCon = WScript.CreateObject("ADODB.Connection");
objCon.Open(
"Driver={Sql Server};APP=" + strScript +
";Server=" + DB_SERVER_NAME +
";Database=" + DB_DATABASE_NAME +
";user id=" + DB_DATABASE_USER_NAME +
";password=" + DB_DATABASE_PASSWORD +
";OLE DB Services=0"
);
return objCon;
}
//
// BEGIN MAIN PROGRAM
//
strMsg = "**************************************************";
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
strScript = String(WScript.ScriptName);
DateTimeNow = new Date();
strMsg = strScript + " started at " + DateTimeNow.toLocaleString();
WScript.Echo(strMsg);
WScript.StdErr.WriteLine(strMsg);
if (!ParseCommandLineParams())
{
DisplayUsage();
WScript.Quit(0);
}
DisplayParameters();
try
{
var objCon = GetDatabaseConnection();
var objCmd = WScript.CreateObject("ADODB.Command");
var objRs = null;
var runStatus = 0;
try
{
// Get Run Status
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_3;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objRs = objCmd.Execute();
runStatus = objRs("Status");
}
catch (er5)
{
}
if (runStatus < 1)
{
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_1;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
var objParam = objCmd.CreateParameter("Return", 3, 4); // adInteger, adParamReturnValue
objCmd.Parameters.Append(objParam);
WScript.Echo("Before execution");
if (ARG_KEEP_BACKUP_COPIES == 1)
{
var objFSO = WScript.CreateObject("Scripting.FileSystemObject");
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt"))
{
var File3 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
File3.Delete();
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt"))
{
var File2 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
File2.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "3.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt"))
{
var File1 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
File1.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "2.txt");
}
if (objFSO.FileExists(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt"))
{
var File0 = objFSO.GetFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt");
File0.Move(ARG_REPORT_PATH + EXTRACT_FILE_NAME + "1.txt");
}
}
try
{
objRs = objCmd.Execute();
}
catch (er)
{
try
{
objRs = objCmd.Execute();
}
catch (er2)
{
try
{
objRs.Close();
}
catch (er3)
{
WScript.Echo("Error found even after 2nd run attempt!");
}
var strError = "An unexpected error has occured during the run of this routine. " +
"and send through a manual extract. If you do not hear back from us by 1pm, please do not hesitate to call " +
".";
// Send error email
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO + ";email#email.com.au";
// objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT_ERROR;
objMsg.TextBody = strError;
objMsg.send();
// Log error in output file
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
objTStreamLE.WriteLine(strError);
objTStreamLE.Close();
}
}
if (objRs.EOF)
{
objRs.Close();
// check return code after closing the record set
var rc = objCmd(0);
if (rc == 100010)
{
WScript.Echo("Another instance is running. Bail out");
}
else if (rc == 0)
{
WScript.Echo("Found no records for users");
}
else
{
var objError = new Error(rc, STRING_SQL_FAILED);
throw objError;
}
}
else
{
// Update Last Update Date
objCmd.ActiveConnection = objCon;
objCmd.CommandText = QUERY_NAME_2;
objCmd.CommandType = 4; // adCmdStoredProc
objCmd.CommandTimeout = 1800;
objCmd.Execute();
var objTStreamLE = objFSO.CreateTextFile(ARG_REPORT_PATH + EXTRACT_FILE_NAME + ".txt", true);
while (true)
{
if (objRs.EOF)
{
// we have to send the last mail
strContentType = "";
}
else
{
var strApplicantID = new String(objRs("EmpID"));
var strCourse_Code = new String(objRs("Event_Code"));
var strCourse_Name = new String(objRs("Course_Name"));
var strCompletion_Date = new String(objRs("Completion_Date"));
var strGrade_Name = new String(objRs("Grade_Name"));
var strBodyInfo = strApplicantID + DELIMITER +
strCourse_Code + DELIMITER +
strCourse_Name + DELIMITER +
strCompletion_Date + DELIMITER +
strGrade_Name;
WScript.Echo(strBodyInfo);
objTStreamLE.WriteLine(strBodyInfo);
}
if (objRs.EOF)
{
break;
}
objRs.MoveNext;
}
objTStreamLE.Close();
objRs.Close();
}
var objConf = GetConfigCDO();
var objMsg = WScript.CreateObject("CDO.Message");
objMsg.Configuration = objConf;
objMsg.To = ARG_EMAIL_TO;
objMsg.From = ARG_EMAIL_FROM;
objMsg.Subject = EMAIL_SUBJECT;
objMsg.TextBody = EMAIL_SUBJECT;
objMsg.send();
EmailCount++; // How many email sent. it will be always one email in this case.
}
objCon.Close();
}
catch (e)
Following a great deal of testing and analysis, we're unable to pinpoint the root cause as both jobs seem to fail intermittently, some nights the extract produces data and other nights no data at all.
Below are our test results based on manual run via cmd and via the task both producing intermittent failed and success results, with no particular pattern.
Test results:
Execute 1st run via command prompt
1st run - Fail
2nd run - Fail
3rd run - Successful
4th run - Successful
5th run - Successful
Execute Scheduled task - 'First run'
1st run - Successful
2nd run - Successful
3rd run - Successful
4th run - Fail - Scheduler failed
5th run - Fail - Scheduler failed
Execute Scheduled task - Sequential First followed by Second run
1st run - First run successful, second run successful but no file generated
2nd run - First run unsuccessful, ran second job - successful
3rd run - First run successful, second run successful but no file generated
4th run - First run unsuccessful, ran second job - successful
A possible cause may be due to timeout; however, each run takes the same time to complete (approx. 3 minutes) whether failed or successful.
We recently upgraded the environment from Windows Server 2005 to Windows Server 2008 and noticed the issue starting occurring after implementation. No change was made to the logic or SQL procedures.
Would Server 2008 handle the logic in such a way to break the routine?

Firefox addon could not convert to bootstrap

Below I have the code for xul addon which works expectedly.
i.e. shows username and password input at the browser startup and whenever the topic is http-on-examine-response it validates and write in specified file.
But when I try to convert it to a bootstrapped extension it won't even show the username password input at the browser startup and nothing is showing in the console when I compile and run.
I couldn't figure out the wrong thing after I changed the components... to Cu/Cc/etc.
I referred below links
https://developer.mozilla.org/en/Add-ons/Bootstrapped_extensions
What does paragraph about figuring out XUL elements mean in MDN document: "How to convert an overlay extension to restartless"
https://github.com/Noitidart/l10n/tree/xhtml-xul
Below is my code:
const {Cc, Ci, Cu, components} = require("chrome");
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
function Tdatacounter() {
var Tdatacounter = {
observe: function(subject, topic, data) {
if (topic == "http-on-examine-response") {
var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
if (httpChannel.responseStatusText == "OK")
{
var buffer = httpChannel.URI.spec;
var pw = buffer.search("pub.tdata.com/releasepic");
if (pw > 0)
{
var date = new Date();
var TimeStamp = date.toLocaleString();
var pfum = buffer.search("flag=unmap");
if (pfum > 0 )
{
var flagname = "unmap";
var flagnum = 4;
}
if (flagnum > 0)
{
buffer += "^" + ThisUserName + "^" + ThisComputerName + "^" + TimeStamp + "^" + flagname + "\r\n";
fos.write(buffer, buffer.length);
}
}
}
}
}
};
var nsIEnvironment = Cc["#mozilla.org/process/environment;1"]
.getService(Ci.nsIEnvironment);
var prompts = Cc["#mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
var usernamelist = ["user1","user2","user3"];
var passlist = ["e%cd1","kowgirl23","bhava1204"];
var ThisUserName = null;
var username = {value: "UserName"}; // default the username to user
var password = {value: "pass"}; // default the password to pass
var check = {value: false}; // default the checkbox to true
var GetUserName = prompts.promptUsernameAndPassword(null, "ProcName", "Enter username and password:", username, password, null, check);
while (GetUserName === true) {
var a = usernamelist.indexOf(username.value);
var b = passlist[a];
if (password.value == b) {
var ThisUserName = username.value;
break;
} else {
var GetUserName = prompts.promptUsernameAndPassword(null, "ProcName", "Enter username and password:", username, password, null, check);
}
}
if (ThisUserName !== null) {
var ThisComputerName = nsIEnvironment.get("ComputerName");
var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils;
var d = new Date();
var dd = d.getDate();
if (dd < 10)
dd = "0" + dd;
var mm = (d.getMonth()+1);
if (mm < 10)
mm = "0" + mm;
var yyyy = d.getFullYear();
var file = new FileUtils.File( "\\\\STJ\\Proc1\\-\\" + yyyy + "\\" + mm + "\\" + dd );
if (!file.exists()) {
file.create(file.DIRECTORY_TYPE, 0755);
}
var filename = "HM_" + ThisUserName + yyyy + "-" + mm + "-" + dd + ".txt";
file.append(filename);
var fos = Cc["#mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
// PR_WRONLY | PR_CREATE_FILE | PR_APPEND
fos.init(file, 0x02 | 0x08 | 0x10, -1, 0);
var date = new Date();
var TimeStamp = date.toLocaleString();
var StartTime = "LoginTime=" + TimeStamp + " at " + ThisComputerName + "\r\n";
fos.write(StartTime, StartTime.length);
var observerService = Cc["#mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(Tdatacounter, "http-on-examine-response", false);
}}
Tdatacounter.prototype.classID = components.ID('{d4a9bb50-b9b2-11e0-a4dd-0800200c9a66}');
Tdatacounter.prototype.classDescription = 'tdata Counter';
Tdatacounter.prototype.contractID = '#tdata/TdataCounter;1';
var NSGetFactory = XPCOMUtils.generateNSGetFactory([TdataCounter]);
I figured out what I've missed. As a beginner in firefox javascript, I missed to add 'register', 'unregister' functions. What a silly mistake I did? Phew. anyhow I hope this answer will be helpful for the novices like me.
Thank you.

FireFox Addon getElementById on http-on-examine-response didn't return any value

I am trying to get a value from an element in active page on http-on-examine-response. My below script works well as an addon without lines between
var tabs = require("sdk/tabs");
and
contentScript: contentScriptString
But when I add those lines it access the text file but doesn't write Anything. I can't see any error message in console because I don't use SDK because the new SDK uses complex things which I have to learn more on this. (For Now I just edit js file in components folder and update chrome.manifest and install.rdf if any and zip them then rename it as xpi and open it in firefox and it works well). I googled how to debug this script with which sdk and tried many scripts titled 'getelementbyid firefox addon' and had no luck. It would be helpful even if you say which sdk should use (a dumb question. But couldnt help myself).
Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
function GetCity() {
var GetCity = {
observe: function(subject, topic, data) {
if (topic == "http-on-examine-response") {
var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
if (httpChannel.responseStatusText == "OK")
{
var buffer = httpChannel.URI.spec;
var pw = buffer.search("mywebsite.com/release");
if (pw > 0)
{
var date = new Date();
var TimeStamp = date.toLocaleString();
var pfum = buffer.search("flag=release1");
if (pfum > 0 )
{
var flagname = "1";
var flagnum = 4;
}
var pfuk = buffer.search("flag=release2");
if (pfuk > 0 )
{
var flagname = "2";
var flagnum = "1";
}
var pfup = buffer.search("flag=release3");
if (pfup > 0 )
{
var flagname = "3";
var flagnum = 2;
}
var pfap = buffer.search("flag=release4");
if (pfap > 0 )
{
var flagname = "4";
var flagnum = 3;
}
if (flagnum > 0)
{
var tabs = require("sdk/tabs");
var contentScriptString = 'var cityname = document.getElementById("city").value ;'
tabs.activeTab.attach({
contentScript: contentScriptString
});
buffer += "^" + ThisUserName + "^" + ThisComputerName + "^" + TimeStamp + "^" + flagname + "\r\n";
fos.write(buffer, buffer.length);
}
}
}
}
}
};
var nsIEnvironment = Components.classes["#mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment);
var ThisUserName = nsIEnvironment.get("UserName");
var ThisComputerName = nsIEnvironment.get("ComputerName");
var FileUtils = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;
var d = new Date();
var dd = d.getDate();
if (dd < 10)
dd = "0" + dd;
var mm = (d.getMonth()+1);
if (mm < 10)
mm = "0" + mm;
var yyyy = d.getFullYear();
var file = new FileUtils.File( "T:\\-\\CityList\\" + yyyy + "\\" + mm + "\\" + dd );
if (!file.exists()) {
file.create(file.DIRECTORY_TYPE, 0755);
}
var filename = "HM_" + ThisUserName + yyyy + "-" + mm + "-" + dd + ".txt";
file.append(filename);
var fos = Components.classes["#mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
// PR_WRONLY | PR_CREATE_FILE | PR_APPEND
fos.init(file, 0x02 | 0x08 | 0x10, -1, 0);
var observerService = Components.classes["#mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(HotelMappingCounter, "http-on-examine-response", false);
}
var NSGetFactory = XPCOMUtils.generateNSGetFactory([CityList]);

Javascript Date constructor ignoring parameters

Supposedly, I should be able to create an arbitrary date using the Date constructor as demonstrated here and referenced here
Where am I going wrong? Please notice that on the last few lines of prettyDateToTimeStamp, I modify the month and day to verify that the Date constructor is doing something - but it is not noticing anything I pass in and just returns the current date.
Here is my code below: and a jsfiddle
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display the full year of todays date.</p>
<p id="demo2">todays date.</p>
<p id="demo3">some other date.</p>
<button onclick="showdates()">Try it</button>
<script>
function showdates() {
var d = Date.now();
var dv = document.getElementById('demo');
dv.innerHTML = d;
var pd = prettyDate(d);
dv = document.getElementById('demo2');
dv.innerHTML = pd;
var ts = prettyDateToTimeStamp(pd);
dv = document.getElementById('demo3');
dv.innerHTML = ts;
}
function prettyDate(javaScriptTimeStamp) {
var dt = new Date(javaScriptTimeStamp);
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var day = dt.getDate();
var hours = dt.getHours();
var minutes = dt.getMinutes();
var seconds = dt.getSeconds();
return month + "/" + day + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
}
function prettyDateToTimeStamp(prettyDate) {
var halves = prettyDate.split(' ');
console.log("halves: " + halves);
var calpart = halves[0];
console.log("calpart : " + calpart );
var clockpart = halves[1];
console.log("clockpart : " + clockpart );
var calbits = calpart.split('/');
console.log("calbits : " + calbits );
var timebits = clockpart.split(':');
console.log("timebits : " + timebits );
var year = parseInt(calbits[2],10);
console.log("year : " + year );
var month = parseInt(calbits[0],10);
console.log("month : " + month );
var day = parseInt(calbits[1],10);
console.log("day : " + day );
var hour = parseInt(timebits[0],10);
console.log("hour : " + hour );
var min = parseInt(timebits[1],10);
console.log("min : " + min );
var sec = parseInt(timebits[2],10);
console.log("sec : " + sec );
month += 3; // change month radically to demonstrate the problem
console.log("month is now: " + month );
day += 7; // change day too
console.log("day is now: " + day );
var ts = Date(year,month,day,hour,min,sec,0);
console.log("ts : " + ts ); // date ctor paramters completely ignored...?
return ts;
}
</script>
</body>
</html>
omg, I have to say "new" Date .... (I've been using Python too much lately)
corrected code now works.
function prettyDateToTimeStamp(prettyDate) {
var halves = prettyDate.split(' ');
var calpart = halves[0];
var clockpart = halves[1];
var calbits = calpart.split('/');
var timebits = clockpart.split(':');
var year = parseInt(calbits[2],10);
var month = parseInt(calbits[0],10);
var day = parseInt(calbits[1],10);
var hour = parseInt(timebits[0],10);
var min = parseInt(timebits[1],10);
var sec = parseInt(timebits[2],10);
month += 3; // change month radically to demonstrate the problem
day += 7; // change day too
var ts = new Date(year,month,day,hour,min,sec,0); // you have to use NEW here!
return ts;
}

Categories

Resources