Esempio n. 1
0
        public ActionResult Create(int id, string task, JobViewModel jobViewModel)
        {
            if (!_authorizer.Authorize(Permissions.ManageCloudMediaJobs, T("You are not authorized to manage cloud jobs.")))
            {
                return(new HttpUnauthorizedResult());
            }

            Logger.Debug("User requested to create job with task of type {0} on cloud video item with ID {1}.", task, id);

            var cloudVideoPart = _contentManager.Get <CloudVideoPart>(id, VersionOptions.Latest);

            if (cloudVideoPart == null)
            {
                Logger.Warning("User requested to create job on cloud video item with ID {0} but no such cloud video item exists.", id);
                return(HttpNotFound(String.Format("No cloud video item with ID {0} was found.", id)));
            }

            var taskProvider    = _taskProviders.Single(x => x.Name == task);
            var inputAsset      = cloudVideoPart.Assets.Single(x => x.Record.Id == jobViewModel.SelectedInputAssetId);
            var videoName       = _contentManager.GetItemMetadata(cloudVideoPart).DisplayText;
            var taskConfig      = (TaskConfiguration)taskProvider.Editor(New, this);
            var taskConnections = taskProvider.GetConnections(taskConfig);
            var taskDisplayText = taskProvider.GetDisplayText(taskConfig);
            var jobName         = !String.IsNullOrWhiteSpace(jobViewModel.Name) ? jobViewModel.Name.TrimSafe() : !String.IsNullOrWhiteSpace(taskDisplayText) ? taskDisplayText : String.Format("{0} ({1})", videoName, taskProvider.Name);
            var jobDescription  = jobViewModel.Description.TrimSafe();

            if (ModelState.IsValid)
            {
                try {
                    var wamsJob        = _wamsClient.CreateNewJob(jobName);
                    var wamsInputAsset = _wamsClient.GetAssetById(inputAsset.WamsAssetId);
                    var wamsTask       = taskProvider.CreateTask(taskConfig, wamsJob.Tasks, new[] { wamsInputAsset });
                    wamsJob.Submit(); // Needs to be done here for job and tasks to get their WAMS ID values.

                    var job = _jobManager.CreateJobFor(cloudVideoPart, j => {
                        j.WamsJobId              = wamsJob.Id;
                        j.Name                   = jobName;
                        j.Description            = jobDescription;
                        j.Status                 = JobStatus.Pending;
                        j.CreatedUtc             = _clock.UtcNow;
                        j.OutputAssetName        = jobViewModel.OutputAssetName.TrimSafe();
                        j.OutputAssetDescription = jobViewModel.OutputAssetDescription.TrimSafe();
                    });

                    _jobManager.CreateTaskFor(job, t => {
                        t.HarvestAssetType = taskConnections.Outputs.First().AssetType;
                        t.HarvestAssetName = taskConnections.Outputs.First().AssetName;
                        t.Settings         = taskProvider.Serialize(taskConfig.Settings);
                        t.Index            = 0;
                        t.TaskProviderName = taskProvider.Name;
                        t.WamsTaskId       = wamsTask.Id;
                    });

                    Logger.Information("Job was created with task of type {0} on cloud video item with ID {1}.", task, id);
                    _notifier.Success(T("The job '{0}' was successfully created.", job.Name));

                    return(Redirect(Url.ItemEditUrl(cloudVideoPart)));
                }
                catch (Exception ex) {
                    _transactionManager.Cancel();

                    Logger.Error(ex, "Error while creating job with task of type {0} on cloud video item with ID {1}.", task, id);
                    _notifier.Error(T("Ar error occurred while creating the job:\n{0}", ex.Message));
                }
            }

            return(View(jobViewModel));
        }