private static void OnTaskCompleted(BundlingTask tsk, bool success, string message, IOutputWriter output = null)
        {
            using (var sc = Shell.GetScope())
            {
                if (success)
                {
                    var _acc   = sc.ServiceProvider.GetService <EnvironmentAccessor>();
                    var _paths = sc.ServiceProvider.GetService <IPathsService>();
                    _acc.CurrentEnvironment = _paths.GetEnvironments().Where(d => d.Name == tsk.Environment).FirstOrDefault();


                    var _bund = sc.ServiceProvider.GetService <IBundlingService>();
                    var _pub  = sc.ServiceProvider.GetService <IPublisherService>();
                    if (output != null)
                    {
                        _bund.OutputWriter = output;
                        _pub.OutputWriter  = output;
                    }
                    string version = _bund.GetAppVersion(tsk.TenantCode, true);
                    _pub.UploadTenantBundle(tsk.TenantCode, "v" + version);
                    _pub.SetTenantInfo(tsk.TenantCode, version);
                }
                BundlingTask.ClearCompleted();
                if (tsk.Status != "NULL")
                {
                    var pusher = sc.ServiceProvider.GetService <IMessagePusher <IBundlingTasksNotifications> >();
                    pusher.Publish(d => d.TaskChanged(tsk));
                }
            }
        }
        public IActionResult PublishTenant([FromBody] DbCreationRequest req)
        {
            SubmitResult = new SubmitResult();
            var outwriter = GetService <IOutputWriter>();
            var oth       = data.GetAppCodes().Where(d => d != req.TenantCode);

            var ver = bundl.GetAppVersion(req.TenantCode, true);

            if (req.Force ?? false)
            {
                ver = increment(ver);
            }
            BundlingTask.ClearCompleted();

            var tsk = BundlingTask.GetTask(req.TenantCode, ver);

            if (tsk == null)
            {
                if (bundl.StartProductionPackIfNeeded(req.TenantCode, out tsk, ver))
                {
                    tsk.Environment = req.Environment;

                    tsk.OnComplete       = (t, res) => OnTaskCompleted(tsk, res.IsSuccess, res.Message, outwriter);
                    SubmitResult.Message = "started_new_task";
                    BundlingTask.Add(tsk);
                }
                else
                {
                    OnTaskCompleted(new BundlingTask
                    {
                        Status      = "NULL",
                        TenantCode  = req.TenantCode,
                        Version     = ver,
                        Environment = req.Environment
                    }, true, "already_rendered");
                    SubmitResult.Message = "already_rendered";
                }
            }
            else
            {
                SubmitResult.Code    = 1;
                SubmitResult.Message = "task_is_already_running";
            }
            SubmitResult.Data["Task"] = tsk;
            return(Respond());
        }