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);
        }
        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);
            }
        }
        private static async Task CreateDbTarget(AzureSqlJobClient elasticJobs, CustomCollectionTargetInfo rootTarget
                                                 , BackgroundTaskService.BackgroundTaskLog log, DatabaseTarget dbTarget)
        {
            try
            {
                var dbTargetInfo = await elasticJobs.Targets.GetDatabaseTargetAsync(dbTarget.Server, dbTarget.Name)
                                   ?? await elasticJobs.Targets.CreateDatabaseTargetAsync(dbTarget.Server, dbTarget.Name);

                var result = await elasticJobs.Targets.AddChildTargetAsync(rootTarget.TargetId, dbTargetInfo.TargetId);

                if (!result)
                {
                    throw new Exception("child target adding returns false");
                }
                log.LogInfo("Targeting db " + dbTarget.Name + "@" + dbTarget.Server + " complete as " + dbTargetInfo.TargetId);
            }
            catch (Exception e)
            {
                log.LogException(e);
                throw new Exception("Targeting db " + dbTarget.Name + "@" + dbTarget.Server + " failed.", e);
            }
        }