private static AzureSqlJobClient CreateAzureSqlJobClient(AzureSqlJobCredentials creds)
        {
            var securePassword = CreatePassword(creds.Password);
            var connection     = new AzureSqlJobConnection(creds.ServerName, creds.DatabaseName, creds.Username, securePassword);

            return(new AzureSqlJobClient(connection));
        }
        private async static Task <bool> Do(AzureSqlJobCredentials azureJobsCreds, BackgroundTaskService.BackgroundTaskLog log, DatabaseDacPacUpdateTaskData data)
        {
            var serviceLocator = ServiceLocatorFactory.CreateMasterSysAdmin();

            var masterTargets = new List <DatabaseTarget>
            {
                new DatabaseTarget(Settings.ChalkableSchoolDbServers.First(), Settings.MasterDbName)
            };

            log.LogInfo("Master DacPac deployment initated");

            if (!(await DeployDacPac(azureJobsCreds, data.DacPacName + "-master", data.MasterDacPacUri, masterTargets, log)))
            {
                return(false);
            }

            log.LogInfo("Preparing schools DacPac targets");

            var schoolTargets = new List <DatabaseTarget>(
                Settings.ChalkableSchoolDbServers.Select(x => new DatabaseTarget(x, Settings.SchoolTemplateDbName))
                .Concat(serviceLocator.DistrictService.GetDistricts().Select(x => new DatabaseTarget(x.ServerUrl, x.Id.ToString())))
                );

            log.LogInfo("Schools DacPac deployment initated");

            if (!(await DeployDacPac(azureJobsCreds, data.DacPacName + "-school", data.SchoolDacPacUri, schoolTargets, log)))
            {
                return(false);
            }

            log.LogInfo("Deploy success");

            return(true);
        }
        public bool Handle(BackgroundTask task, BackgroundTaskService.BackgroundTaskLog log)
        {
            bool res;

            try
            {
                var data = DatabaseDacPacUpdateTaskData.FromString(task.Data);
                log.LogInfo("DACPAC: " + data.DacPacName);
                var azureJobsCreds = new AzureSqlJobCredentials
                {
                    ServerName   = data.ServerName,
                    DatabaseName = data.DatabaseName,
                    Username     = data.Username,
                    Password     = data.Password
                };

                var work = Do(azureJobsCreds, log, data);
                work.Wait();
                res = work.Result;
            }
            catch (Exception ex)
            {
                log.LogError("Error during database update");
                log.LogException(ex);
                res = false;
            }
            return(res);
        }
Beispiel #4
0
 public JobStatHelper(AzureSqlJobCredentials creds)
 {
     dbName    = creds.DatabaseName;
     serverUrl = creds.ServerName;
     userName  = creds.Username;
     password  = creds.Password;
 }
        private static async Task <bool> DeployDacPac(AzureSqlJobCredentials azureJobsCreds, string dacPacName, string dacPacUri,
                                                      IList <DatabaseTarget> targets, BackgroundTaskService.BackgroundTaskLog log)
        {
            var elasticJobs = CreateAzureSqlJobClient(azureJobsCreds);

            var jobExecution = await StartJob(elasticJobs, targets, log, dacPacName, dacPacUri);

            JobStatHelper helper = new JobStatHelper(azureJobsCreds);

            DateTime?since     = null;
            var      lifecycle = jobExecution.Lifecycle;

            while (true)
            {
                try
                {
                    var status = await elasticJobs.JobExecutions.GetJobExecutionAsync(jobExecution.JobExecutionId);

                    if (status.Lifecycle != lifecycle)
                    {
                        log.LogInfo($"{dacPacName} job is {status.Lifecycle}");
                    }
                    lifecycle = status.Lifecycle;

                    var stats = helper.GetChilderJobExecutionStat(jobExecution.JobName);
                    log.LogInfo("Task stats:\n" + stats.Select(x => x.Lifecycle + ": " + x.Count).JoinString("\n"));

                    var taskExecutions = helper.GetJobTaskExecutions(jobExecution.JobName, since, null);
                    log.LogInfo(taskExecutions.Select(x => x.Message));
                    since = taskExecutions.Max(x => x.EndTime);

                    switch (status.Lifecycle)
                    {
                    case JobExecutionLifecycle.Failed:
                    case JobExecutionLifecycle.Canceled:
                    case JobExecutionLifecycle.Skipped:
                    case JobExecutionLifecycle.TimedOut:
                        log.LogError("Deploy " + status.Lifecycle);
                        return(false);

                    case JobExecutionLifecycle.Succeeded:
                        log.Flush();
                        return(true);
                    }
                }
                catch (SqlException e)
                {
                    log.LogInfo($"Failed to connect to {azureJobsCreds.DatabaseName}@{azureJobsCreds.DatabaseName}: {e.Message}");
                    log.LogException(e);
                }

                await Task.Delay(30000);
            }
        }
 public static Task <bool> Test(AzureSqlJobCredentials creds, BackgroundTaskService.BackgroundTaskLog log, DatabaseDacPacUpdateTaskData data)
 {
     return(Do(creds, log, data));
 }