Home > Software design >  console service that should run 24/7 not looping when deployed
console service that should run 24/7 not looping when deployed

Time:10-19

I have a service that downloads and uploads an img in s3, it is working and looping 24.7 when in local in debug mode, but whenever I deploy it in QA or PROD, it exits right after 1 loop. I thought it had something to do with the memory or s3 not allowing continuous save, but when I commented all codes except for the 1st and last line, I confirmed that it only ran once.

here is the program.cs that calls the loop service

static class Program
    {
        private static ILogger Logger = new ServiceLogHelper();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {


#if (DEBUG)
         
            Library.Code.Utility.EnvironemntUrl = "http://localhost";
#elif (QA)
            Library.Code.Utility.EnvironemntUrl = "http://qa.somesite.com";
  
#elif (PROD)
            Library.Code.Utility.EnvironemntUrl = "http://prod.somesite.com";
 
#endif

            // Set Environment
            if (args.Length > 0)
            {
                var enviromentArgument = args[0];
                if (enviromentArgument.StartsWith("-"))
                {
                    enviromentArgument = enviromentArgument.Substring(1);
                }
                Logger.WriteLine("enviromentArgument "   enviromentArgument   " "   DateTime.Now);
                Library.Code.Utility.EnvironemntUrl = new UriBuilder(enviromentArgument).Uri.ToString();
            }



#if (!DEBUG)
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new ImageSomeService()
                };
                ServiceBase.Run(ServicesToRun);
#else
         
            var service = new ImageSomeService();
                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            #endif
        }
    }

here is the loop service

public partial class ImageSomeService : ServiceBase
    {
        public ImageSomeService()
        {
            InitializeComponent();

            OnStart(new string[] { });
        }

        private bool Started { get; set; }

        protected override void OnStart(string[] args)
        {
            Started = true;

            do
            {
                ImageSvcHelper.RunBatch(); 
                System.Threading.Thread.Sleep(10000);
            }
            while (Started);
        }

        protected override void OnStop()
        {
            Started = false; //i put logger here but it was never printed so it means it didn't stop
        }
    }

below is the helper, which I found out run only once because it only printed in logger once. But if I am in debug mode in local (run thru Visual studio), the line "Image service running in .... " is infinitely printed until I stop it.

public static class ImageSvcHelper
    {
       
  
        public static bool RunBatch()
        {

           
            Logger.WriteLine("Image caching running in "   Library.Code.Utility.Environment   " "   DateTime.Now);
            var minID = AppHelper.GetValue<int>(group: "LastImgID", key: "ImgID");

            

            Logger.WriteLine("minid "   minID   " "   DateTime.Now);
           //  some codes here
            System.Threading.Thread.Sleep(30000);

            return true;

        }

   
    }

CodePudding user response:

The Service Control Manager start the service but wait for 30sec the OnStart() to start the service. So you have to run your loop in a new thread to let the SCM exit and manage services.

protected override void OnStart(string[] args)
{
  Started = true;
  Task.Run(() => {
    do
    {
      ImageSvcHelper.RunBatch(); 
      System.Threading.Thread.Sleep(10000);
    }
    while (Started);
  });
}
  • Related