Click here to Skip to main content
15,881,173 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Is it possible to add fore ground and back ground to any app that is down? Pin
Richard Deeming31-Aug-21 22:01
mveRichard Deeming31-Aug-21 22:01 
GeneralRe: Is it possible to add fore ground and back ground to any app that is down? Pin
samflex1-Sep-21 5:50
samflex1-Sep-21 5:50 
GeneralRe: Is it possible to add fore ground and back ground to any app that is down? Pin
Richard Deeming1-Sep-21 21:47
mveRichard Deeming1-Sep-21 21:47 
GeneralRe: Is it possible to add fore ground and back ground to any app that is down? Pin
samflex2-Sep-21 3:02
samflex2-Sep-21 3:02 
Questionproject code in .net for online banking transaction Pin
Jk 0724-Aug-21 5:12
Jk 0724-Aug-21 5:12 
AnswerRe: project code in .net for online banking transaction Pin
Richard MacCutchan24-Aug-21 5:40
mveRichard MacCutchan24-Aug-21 5:40 
GeneralRe: project code in .net for online banking transaction Pin
Member 1534301831-Aug-21 22:04
Member 1534301831-Aug-21 22:04 
QuestionI am having problem creating a script to monitor Rest/API services. Pin
samflex23-Aug-21 8:11
samflex23-Aug-21 8:11 
Hello experts,

I have five (5) public facing REST servers and I have been tasked with coming up with a script that will monitor the status of the servers (whether up or down) and send email notifications three times a day, at &30am, 12:30pm, and 3:30pm.

The email notification should either indicate whether the servers are up or down.

I have had issues thinking up a solution.

However, while googling, I found this article:

Repeat Task every N interval using Windows Service in C# and VB.Net[^]

I am trying to follow it and I have made some minor changes.

For instance, in appSettings, I changed the configuration to this:
<appSettings>
    <add key ="Mode" value ="Daily"/>
    <add key ="ScheduledTime" value ="24:00"/> //I am trying to set default scheduledTime for 24 hours. Not sure if it is right.
</appSettings>


I am not entirely sure that it is completely correct.

Then, I made this change:
if (mode == "DAILY")
{
    //Get the Scheduled Time from AppSettings.
    scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
    if(DateTime.Now().ToString("HH:mm") = "07:30" || DateTime.Now().ToString("HH:mm") = "12:30" || DateTime.Now().ToString("HH:mm") = "3:30"))
    {
       //I want to do the email notification here.

        //Then if Scheduled Time is passed set Schedule for the next day.
        scheduledTime = scheduledTime.AddDays(1);
    }
}


Do I still need the WriteToFile(...) which saves the server status to a text file?

Here is the complete script from that link:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }
 
    protected override void OnStart(string[] args)
    {
        this.WriteToFile("Simple Service started {0}");
        this.ScheduleService();
    }
 
    protected override void OnStop()
    {
        this.WriteToFile("Simple Service stopped {0}");
        this.Schedular.Dispose();
    }
 
    private Timer Schedular;
 
    public void ScheduleService()
    {
        try
        {
            Schedular = new Timer(new TimerCallback(SchedularCallback));
            string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
            this.WriteToFile("Simple Service Mode: " + mode + " {0}");
 
            //Set the Default Time.
            DateTime scheduledTime = DateTime.MinValue;
 
            if (mode == "DAILY")
            {
                //Get the Scheduled Time from AppSettings.
                scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
                if(DateTime.Now().ToString("HH:mm") = "07:30" || DateTime.Now().ToString("HH:mm") = "12:30" || DateTime.Now().ToString("HH:mm") = "3:30"))
                {
                   // Do the email task.
                   
                    //Then if Scheduled Time is passed set Schedule for the next day.
                    scheduledTime = scheduledTime.AddDays(1);
                }
            }
 
            if (mode.ToUpper() == "INTERVAL")
            {
                //Get the Interval in Minutes from AppSettings.
                int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);
 
                //Set the Scheduled Time by adding the Interval to Current Time.
                scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
                if (DateTime.Now > scheduledTime)
                {
                    //If Scheduled Time is passed set Schedule for the next Interval.
                    scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
                }
            }
 
            TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);
            string schedule = string.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
 
            this.WriteToFile("Simple Service scheduled to run after: " + schedule + " {0}");
 
            //Get the difference in Minutes between the Scheduled and Current Time.
            int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);
 
            //Change the Timer's Due Time.
            Schedular.Change(dueTime, Timeout.Infinite);
        }
        catch(Exception ex)
        {
            WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
 
            //Stop the Windows Service.
            using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
            {
                serviceController.Stop();
            }
        }
    }
 
    private void SchedularCallback(object e)
    {
        this.WriteToFile("Simple Service Log: {0}");
        this.ScheduleService();
    }
 
    private void WriteToFile(string text)
    {
        string path = "C:\\ServiceLog.txt";
        using (StreamWriter writer = new StreamWriter(path, true))
        {
            writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
            writer.Close();
        }
    }
}


Assistance, as always, is greatly appreciated.
AnswerRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming23-Aug-21 21:15
mveRichard Deeming23-Aug-21 21:15 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex24-Aug-21 5:08
samflex24-Aug-21 5:08 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
David Mujica24-Aug-21 6:04
David Mujica24-Aug-21 6:04 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex24-Aug-21 6:09
samflex24-Aug-21 6:09 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex25-Aug-21 4:18
samflex25-Aug-21 4:18 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
David Mujica25-Aug-21 5:24
David Mujica25-Aug-21 5:24 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex25-Aug-21 6:09
samflex25-Aug-21 6:09 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming25-Aug-21 6:18
mveRichard Deeming25-Aug-21 6:18 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex25-Aug-21 8:23
samflex25-Aug-21 8:23 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming25-Aug-21 22:12
mveRichard Deeming25-Aug-21 22:12 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 3:04
samflex26-Aug-21 3:04 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 3:52
mveRichard Deeming26-Aug-21 3:52 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 4:04
samflex26-Aug-21 4:04 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 5:08
samflex26-Aug-21 5:08 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 5:19
mveRichard Deeming26-Aug-21 5:19 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
samflex26-Aug-21 6:41
samflex26-Aug-21 6:41 
GeneralRe: I am having problem creating a script to monitor Rest/API services. Pin
Richard Deeming26-Aug-21 21:28
mveRichard Deeming26-Aug-21 21:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.