Example #1
0
        public async Task LoadService(ServiceInfo model)
        {
            try
            {
                if (ServiceInfo?.Id == model.Id)
                {
                    return;
                }

                var service = AvaliableServices.FirstOrDefault(s => s.Id == model.Id);
                if (service != null)
                {
                    ServiceInfo = service;
                }
                else
                {
                    var path = Path.Combine(serviceDir,
                                            $"{model.Name}-v{model.Version}{Path.GetExtension(model.ArchiveUrl)}");
                    using (HttpClient client = new HttpClient())
                    {
                        await DownloadFile(client, model.ArchiveUrl, path);
                    }

                    var targetDir = Path.Combine(serviceDir, Path.GetFileNameWithoutExtension(path));
                    Unzip(path, targetDir);
                    File.Delete(path);

                    model.Guid     = Guid.NewGuid();
                    model.LoadedAt = DateTime.Now;
                    model.Location = targetDir;
                    ServiceInfoHelper.Save(Path.Combine(targetDir, "serviceInfo.json"), model);

                    ServiceInfo = model;
                }

                Restart();
            }
            catch (Exception ex)
            {
                throw new Exception("Load service failed.", ex);
            }
        }
Example #2
0
        public void Boot(IConsulClient consulClient, ILoggerFactory logerFactory)
        {
            foreach (var dir in Directory.GetDirectories(serviceDir))
            {
                var serviceFilePath = Path.Combine(dir, "serviceInfo.json");
                if (!File.Exists(serviceFilePath))
                {
                    continue;
                }

                AvaliableServices.Add(ServiceInfoHelper.Load(serviceFilePath));
            }

            if (File.Exists(serviceInfoFilePath))
            {
                serviceInfo = ServiceInfoHelper.Load(serviceInfoFilePath);
            }
            else if (AvaliableServices.Count > 0)
            {
                ServiceInfo = AvaliableServices.OrderByDescending(s => s.LoadedAt).First();
            }

            Start(consulClient, logerFactory);
        }