Exemple #1
0
        public static int Main(string[] args)
        {
            ITraceManager traceManager = new Log4netTraceManager(new Log4netWrapper());

            var rc = HostFactory.New(x =>
            {
                x.Service <ApiService>(sc =>
                {
                    sc.ConstructUsing(s => new ApiService());
                    sc.WhenStarted((s, hostControl) => s.Start(hostControl));
                    sc.WhenStopped((s, hostControl) => s.Stop(hostControl));
                });

                x.RunAsLocalSystem();
                x.SetDescription(ApiService.Description);
                x.SetDisplayName(ApiService.DisplayName);
                x.SetServiceName(ApiService.ServiceName);

                x.EnableServiceRecovery(recoveryOption => recoveryOption.RestartService(0));
                x.StartAutomaticallyDelayed();
            });

            TopshelfExitCode exitCode = TopshelfExitCode.AbnormalExit;

            try
            {
                exitCode = rc.Run();
            }
            catch (Exception e)
            {
                traceManager.TraceError(e, "ERROR on service starting.");
            }
            return((int)exitCode);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                var traceManager = new Log4netTraceManager(new Log4netWrapper());

                // If in docker container, edit the config file
                if (IsInContainer())
                {
                    traceManager.TraceDebug("Edit dynamically config file from docker container");
                    var dataSource               = Environment.GetEnvironmentVariable("DATASOURCE");
                    var database                 = Environment.GetEnvironmentVariable("DATABASE");
                    var fileServerScheme         = Environment.GetEnvironmentVariable("FILESERVERSCHEME");
                    var fileServer               = Environment.GetEnvironmentVariable("FILESERVER");
                    var fileServerPort           = Environment.GetEnvironmentVariable("FILESERVERPORT");
                    var sendNotificationInterval = Environment.GetEnvironmentVariable("SENDNOTIFICATIONINTERVAL");

                    traceManager.TraceDebug($"DATASOURCE : {dataSource}");
                    traceManager.TraceDebug($"DATABASE : {database}");
                    traceManager.TraceDebug($"FILESERVERSCHEME : {fileServerScheme}");
                    traceManager.TraceDebug($"FILESERVER : {fileServer}");
                    traceManager.TraceDebug($"FILESERVERPORT : {fileServerPort}");
                    traceManager.TraceDebug($"SENDNOTIFICATIONINTERVAL : {sendNotificationInterval}");

                    using (var process = new Process())
                    {
                        process.StartInfo.FileName  = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "kl2suitenotificationconfig.exe");
                        process.StartInfo.Arguments = $"-ConfigFile KProcess.KL2.Notification.exe.config -DataSource {dataSource} -Database {database} -FileServerScheme {fileServerScheme} -FileServer {fileServer} -FileServerPort {fileServerPort} -SendNotificationInterval {sendNotificationInterval}";
                        try
                        {
                            process.Start();
                            process.WaitForExit();
                            traceManager.TraceError("Config file correctly updated.");
                        }
                        catch
                        {
                            traceManager.TraceError("An issue has occured during config edition.");
                        }
                    }
                }

                Console.WriteLine("Registering dependencies ...");
                traceManager.TraceDebug("Registering dependencies ...");
                var container     = UnityResolver.RegisterServices(IoC.Container);
                int emailInterval = Convert.ToInt16(ConfigurationManager.AppSettings["SendNotificationInterval"]);

                x.UseUnityContainer(container);
                x.UsingQuartzJobFactory(() => new UnityJobFactory(container));
                x.Service <NotificationJobService>(s =>
                {
                    s.ConstructUsingUnityContainer();
                    s.WhenStarted(service => service.OnStart());
                    s.WhenStopped(service => service.OnStop());

                    s.ScheduleQuartzJob(q =>
                                        q.WithJob(() =>
                                                  JobBuilder.Create <SendEmailsJob>().Build())
                                        .AddTrigger(() =>
                                                    TriggerBuilder.Create()
                                                    .WithSimpleSchedule(b => b.WithIntervalInMinutes(emailInterval).RepeatForever()).Build())
                                        );

                    s.ScheduleQuartzJob(q =>
                                        q.WithJob(() =>
                                                  JobBuilder.Create <CollectReportsJob>().Build())
                                        .AddTrigger(() =>
                                                    TriggerBuilder.Create()
                                                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(07, 00).InTimeZone(TimeZoneInfo.Local)).Build())
                                        //.WithSimpleSchedule(b => b.WithIntervalInMinutes(30).RepeatForever()).Build())
                                        );

                    //s.ScheduleQuartzJob(q =>
                    //    q.WithJob(() =>
                    //        JobBuilder.Create<CreateEmailJob>().Build())
                    //    .AddTrigger(() =>
                    //        TriggerBuilder.Create()
                    //            //.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(15, 37).InTimeZone(TimeZoneInfo.Local)).Build())
                    //            .WithSimpleSchedule(b => b.WithIntervalInMinutes(3).RepeatForever()).Build())
                    //);
                });

                x.RunAsLocalSystem()
                .DependsOnEventLog()
                .StartAutomatically()
                .EnableServiceRecovery(rc => rc.RestartService(0));

                x.SetServiceName(NotificationJobService.ServiceName);
                x.SetDisplayName(NotificationJobService.DisplayName);
                x.SetDescription(NotificationJobService.Description);

                // TEST daily inspections reporting

                /*var job = container.Resolve<CollectReportsJob>();
                 * job.Execute(null);*/
            });
        }