4 Replies - 474 Views - Last Post: 12 October 2012 - 08:04 AM Rate Topic: -----

#1 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Method Call after A Successful Update

Posted 12 October 2012 - 06:59 AM

I'm trying to:

  • stop a windows service
  • call a method to replace the .exe file for the windows service with a newer version and then
  • start the service back up after the update is successful (my start and stop work great but the it may take longer for the service to start or stop).


Is there an event that watches the service to make sure it's completely stopped before the update starts, and another method that watches the .exe update until it's successfully updated before the call to starts the service?

I can watch the status of the service but I don't know if I should rely on that because the service may take longer to stop of start. You help is appreciated.

Thais is what I have so far:
string serviceStatus = GetServiceStatus();
                if (serviceStatus.Equals("Running"))
                {
                    StopService(SERVICE_NAME);

                    //TODO: event to watch the StopService before the UpgradeServiceExe call
                    if (serviceStatus.Equals("Stopped"))
                    {
                        UpgradeServiceDLL();
                    }
                    //TODO: event to watch the update process before the StartService call
                    StartService(SERVICE_NAME);
                }
                else {
                    _logger.Error("POS Service is not running!");
                }



Is This A Good Question/Topic? 0
  • +

Replies To: Method Call after A Successful Update

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Method Call after A Successful Update

Posted 12 October 2012 - 07:33 AM

Without knowing what those methods that you're calling actually do, we can't help you.
Was This Post Helpful? 0
  • +
  • -

#3 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Method Call after A Successful Update

Posted 12 October 2012 - 07:53 AM

GetServiceStatus(): get the status of the windows service.
StopService(SERVICE_NAME): stops the specifies service.
UpgradeServiceDLL(): replaces file(s) (e.g.. somefile.exe) with updated ones.
StartService(SERVICE_NAME): starts the service back up.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Method Call after A Successful Update

Posted 12 October 2012 - 07:59 AM

I mean, code-wise. If we don't know what your code is, we don't know what events or functions are available to you.
Was This Post Helpful? 0
  • +
  • -

#5 asdbabil  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 241
  • Joined: 26-October 08

Re: Method Call after A Successful Update

Posted 12 October 2012 - 08:04 AM

Here's all the code I have. (I still have to do more implementation)

using HHEntRpt.Core.Interfaces;
using HHHEntRpt.PosCollector;
using Quartz;
using StructureMap;
using HHEntRpt.PosCollectorServiceUpdater.Interfaces;
using System.ServiceProcess;
using System;
using System.Reflection;
using System.Management;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace HHEntRpt.PosCollectorServiceUpdater
{
    public class HHJob : IJob
    {

        private ILogger _logger = ObjectFactory.GetInstance<ILogger>();
        private IConfiguration configuration = ObjectFactory.GetInstance<Configuration>();
        private string SERVICE_NAME = "POSDataCollector";
        private Assembly assembly;

        public HHJob() { }

        public void Execute(IJobExecutionContext context)
        {
            _logger.Info("HELLO FROM Execute(): " + System.DateTime.Now);

            _logger.Info("Calling GetOSName()");
            string osVersion = GetOSName();
            _logger.Info("Calling GetCurrentServiceDLLVersion()");
            string currentServiceDLLVersion = GetCurrentServiceDLLVersion();
            _logger.Info("Calling GetNewServiceDLLVersion()");
            string newServiceDLLVersion = GetNewServiceDLLVersion();
            string serviceStatus = GetServiceStatus();
            
            if (!currentServiceDLLVersion.Equals(newServiceDLLVersion))
            {
                bool downloeded = DownloadNewDLL();
                if (downloeded == true)
                {
                    _logger.Info("Calling GetServiceStatus()");
                    string serviceStatus = GetServiceStatus();
                    if (serviceStatus.Equals("Running"))
                    {
                        StopService(SERVICE_NAME);

                        if (serviceStatus.Equals("Stopped"))
                        {
                            UpgradeServiceDLL();
                        }
                        //TODO: event to watch the update process before starting service
                        StartService(SERVICE_NAME);
                    }
                    else {
                        _logger.Error("POS Service is not running!");
                    }
                }

                string updatedDLLVersion = GetCurrentServiceDLLVersion();
                if (updatedDLLVersion.Equals(newServiceDLLVersion))
                {
                    _logger.Info("DLL update succeeded!");
                    //TODO: Update HHstore.CurrentServiceDLLVersion in db
                }
                else
                {
                    _logger.Error("DLL update failed");
                    //TODO: send an email or something
                }
            }
        }

        private void UpgradeServiceDLL()
        {
            string newFileName = "newFileName.exe";
            string currentFileName = "currentFileName.exe";
            string sourcePath = @"c:\tempFolder";
            string targetPath = @"C:\Program Files (x86)\HH\POSCollector";

            string sourceFile = System.IO.Path.Combine(sourcePath, newFileName);
            string destFile = System.IO.Path.Combine(targetPath, currentFileName);

            try
            {
                if (!System.IO.Directory.Exists(sourcePath))
                {
                    System.IO.Directory.CreateDirectory(sourcePath);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("ERROR: Could not create folder: {0}", ex.ToString());
            }

            try
            {
                if (System.IO.Directory.Exists(sourcePath))
                {
                    _logger.Info("Updater service is being updated to a newer version: " + System.DateTime.Now);
                    string[] files = System.IO.Directory.GetFiles(sourcePath);

                    // Copy the files and overwrite destination files if they already exist. 
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        newFileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetPath, newFileName);
                        System.IO.File.Copy(s, destFile, true);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error("ERROR: Source path does not exist!: {0}", ex.ToString());
            }
        }

        private bool DownloadNewDLL()
        {
            bool downloeded = false;
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFile("ftp://mysite.com/myfile.exe", @"c:\tempFolder\myNewFile.exe");
                    downloeded = true;
                    _logger.Info("File was downloaded successfully.");
                }
            }
            catch (Exception ex)
            {
                _logger.Error("ERROR: File Download Failed: {0}", ex.ToString());
            }
            return downloeded;
        }

        private string GetNewServiceDLLVersion()
        {
            // TODO: call webapi to get the new service dll version
            string newVersion = "";
            return newVersion;
        }

        private string GetOSName()
        {
            _logger.Info("GetOSName() was called.");
            string version = "";
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
                foreach (ManagementObject os in searcher.Get())
                {
                    version = os["Caption"].ToString();
                    break;
                }
                _logger.Info("OPERATING SYSTEM NAME IS: " + version);
            }
            catch (Exception ex)
            {
                _logger.Error("ERROR: {0}", ex.ToString());
            }

            return version;
        }

        private string GetServiceStatus()
        {
            string status = "";
            try
            {
                ServiceController sc = new ServiceController(SERVICE_NAME);
                status = sc.Status.ToString();
                _logger.Info("HHPOS service is: " + status);
               
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("ERROR: {0}", ex.ToString()));
            }

            return status;
        }

        private void StopService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            try
            {
                _logger.Info("Updater service is stopping: " + System.DateTime.Now);
                service.Stop();

                //_logger.Info("Calling UpgradeService()");
                //UpgradeService();

                //_logger.Info("Calling StartService()");
                //StartService(SERVICE_NAME);
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("ERROR: {0}", ex.ToString()));
            }
        }

        private void StartService(string serviceName)
        {
            ServiceController service = new ServiceController(serviceName);
            try
            {
                _logger.Info("Updater service is starting: " + System.DateTime.Now);
                service.Start();
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("ERROR: {0}", ex.ToString()));
            }
        }

        private string GetCurrentServiceDLLVersion()
        {
            _logger.Info("GetCurrentServiceDLLVersion() was called");
            string version = "";

            try
            {
                assembly = Assembly.LoadFile(@"C:\Program Files (x86)\POSCollector\HHEntRpt.PosCollector.exe");
                _logger.Info("service exe is : " + assembly.GetName());
                version = assembly.GetName().Version.ToString();
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("ERROR: {0}", ex.ToString()));
            }
            return version;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1