コード例 #1
0
        public bool Run()
        {
            string translatedCommand = "";

            if (commandPath == "")
            {
                _jobLog.WriteEntry(this, "No custom commands found", Log.LogEntryType.Information);
                return(true); // not a critical failure
            }

            // Get the path if it is an absolute path, if it's relative we start in the MCEBuddy directory
            if (!Path.IsPathRooted(commandPath))
            {
                commandPath = Path.Combine(GlobalDefs.AppPath, commandPath); // Relative path starts with MCEBuddy path
            }
            if ((hangPeriod < 0) || (!File.Exists(commandPath)))
            {
                if (hangPeriod < 0)
                {
                    _jobLog.WriteEntry(this, _prefix + "HangPeriod NOT specified!", Log.LogEntryType.Error);
                }
                else
                {
                    _jobLog.WriteEntry(this, _prefix + "Path does NOT exist!", Log.LogEntryType.Error);
                }

                _jobLog.WriteEntry(this, "Invalid custom command parameters" + " \n" + _prefix + "Path = " + commandPath + " \n" + _prefix + "Parameters = " + commandParameters + " \n" + _prefix + "HangPeriod = " + hangPeriod.ToString(System.Globalization.CultureInfo.InvariantCulture) + " \n" + _prefix + "Critical = " + customCommandCritical.ToString() + " \n" + _prefix + "UISession = " + customCommandUISession.ToString() + " \n" + _prefix + "ShowWindow = " + customCommandShowWindow.ToString() + " \n" + _prefix + "ExitCodeCheck = " + customCommandExitCodeCheck.ToString(), Log.LogEntryType.Error);

                if (customCommandCritical)
                {
                    _jobStatus.ErrorMsg = "Invalid custom command parameters"; // Set an error message on if we are failing the conversion
                }
                return(!customCommandCritical);                                // return the opposite of the critical (if it's true then return false)
            }

            // Translate the user commands
            if (!String.IsNullOrWhiteSpace(commandParameters)) // Check if there was a custom command parameter
            {
                translatedCommand = UserCustomParams.CustomParamsReplace(commandParameters, _workingPath, _destinationPath, _convertedFile, _sourceFile, _remuxFile, _edlFile, _srtFile, _profile, _taskName, _metaData, _jobLog);
                if (String.IsNullOrWhiteSpace(translatedCommand))
                {
                    _jobLog.WriteEntry(this, "Invalid custom command. Error", (customCommandCritical ? Log.LogEntryType.Error : Log.LogEntryType.Warning));
                    if (customCommandCritical)
                    {
                        _jobStatus.ErrorMsg = Localise.GetPhrase("Invalid custom command"); // Set an error message on if we are failing the conversion
                    }
                    return(!customCommandCritical);                                         // return the opposite of the critical (if it's true then return false)
                }
            }

            try
            {
                baseCommand = new Base(customCommandShowWindow, translatedCommand, commandPath, customCommandUISession, _jobStatus, _jobLog); // send the absolute command path and by default success is true until process is terminated and show Window
            }
            catch (FileNotFoundException)
            {
                _jobLog.WriteEntry(this, "Invalid custom command path" + " " + _prefix + "Path = " + commandPath, (customCommandCritical ? Log.LogEntryType.Error : Log.LogEntryType.Warning));
                if (customCommandCritical)
                {
                    _jobStatus.ErrorMsg = "Invalid custom command path"; // Set an error message on if we are failing the conversion
                }
                return(!customCommandCritical);                          // return the opposite of the critical (if it's true then return false)
            }

            // Set the hang detection period
            baseCommand.HangPeriod = hangPeriod;

            _jobLog.WriteEntry(this, "About to run custom command with parameters:" + " \n" + _prefix + "Path = " + commandPath + " \n" + _prefix + "Parameters = " + commandParameters + " \n" + _prefix + "HangPeriod = " + hangPeriod.ToString(System.Globalization.CultureInfo.InvariantCulture) + " \n" + _prefix + "Critical = " + customCommandCritical.ToString() + " \n" + _prefix + "UISession = " + customCommandUISession.ToString() + " \n" + _prefix + "ShowWindow = " + customCommandShowWindow.ToString() + " \n" + _prefix + "ExitCodeCheck = " + customCommandExitCodeCheck.ToString(), Log.LogEntryType.Debug);

            // Run the custom command
            baseCommand.Run();

            // Check for hang/termination
            if (!baseCommand.Success)
            {
                _jobLog.WriteEntry(this, "Custom command hung, process was terminated", (customCommandCritical ? Log.LogEntryType.Error : Log.LogEntryType.Warning));

                if (customCommandCritical)
                {
                    _jobStatus.ErrorMsg = "Custom command hung, process was terminated";
                }
                else
                {
                    _jobStatus.ErrorMsg = "";   // Clear any errors
                }
                return(!customCommandCritical); // return the opposite, see above
            }

            // Check if exit code not equal to0 indicating failure, if required
            if (customCommandExitCodeCheck && (baseCommand.ExitCode != 0))
            {
                _jobStatus.ErrorMsg = "Custom command failed with exit code " + baseCommand.ExitCode.ToString();
                _jobLog.WriteEntry(this, "Custom command failed with Exit Code " + baseCommand.ExitCode.ToString(), (customCommandCritical ? Log.LogEntryType.Error : Log.LogEntryType.Warning));
                return(false); // failed
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Adds a conversion job to the queue
        /// This function does NOT take a lock on the queue before modifying it. This function is not thread safe
        /// </summary>
        /// <param name="filePath">File to add</param>
        /// <param name="monitorTask">Monitor task name which found the file</param>
        /// <param name="manual">True if this is a manuall added entry, false if it's added by a monitor task</param>
        private void AddJobs(string filePath, MonitorJobOptions monitorTask, bool manual)
        {
            bool filterMatchSuccessful = false;

            // Check if the file has already been processed in the past if so skip
            if (_conversionTaskFilterMismatchFiles.Contains(filePath))
            {
                return; //
            }
            foreach (ConversionJobOptions conversionTask in MCEBuddyConf.GlobalMCEConfig.AllConversionTasks)
            {
                // Check if the task is disabled, which case skip
                if (!conversionTask.enabled)
                {
                    Log.AppLog.WriteEntry(this, Localise.GetPhrase("Conversion Task") + " " + conversionTask.taskName + " " + Localise.GetPhrase("disabled, skipping file") + " " + filePath, Log.LogEntryType.Debug, true);
                    continue;
                }

                conversionTask.sourceVideo = filePath;

                // Monitor Task name matching if not empty
                if ((monitorTask != null) && !String.IsNullOrWhiteSpace(monitorTask.taskName) && (conversionTask.monitorTaskNames != null))
                {
                    bool foundMatch = false;
                    foreach (string matchMonitorTaskName in conversionTask.monitorTaskNames)
                    {
                        if (monitorTask.taskName.ToLower().Trim() != matchMonitorTaskName.ToLower().Trim()) // match the list of a name
                        {
                            continue;                                                                       // move onto next monitor task name
                        }
                        else
                        {
                            foundMatch = true;
                            break;
                        }
                    }

                    if (!foundMatch)
                    {
                        Log.AppLog.WriteEntry(this, "Skipping Conversion task " + conversionTask.taskName + " for file " + filePath + " since Monitor task " + monitorTask.taskName + " does not match the list of monitor tasks in the conversion task.", Log.LogEntryType.Debug, true);
                        continue; // move into next conversion task
                    }
                }

                // Metadata extract and pattern match from conversion task
                VideoMetaData metaData = MetadataMatchFilters(conversionTask);
                if (metaData != null) // We got a match - process the file
                {
                    // Calculate where to add the job in the queue
                    int idx;
                    if (manual || conversionTask.insertQueueTop) // Manual jobs to the head of the queue, just after the current active job, or check if the conversion task is asking to add the job at the head of the queue
                    {
                        for (idx = 0; idx < _jobQueue.Count; idx++)
                        {
                            if (!_jobQueue[idx].Active)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        idx = _jobQueue.Count; // Add the job to the end of the active queue
                    }
                    // If it's a manual entry then we need to convert, reset the skip reconverting flag if it's set
                    if (manual && conversionTask.skipReprocessing)
                    {
                        conversionTask.skipReprocessing = conversionTask.checkReprocessingHistory = false; // We can make a direct change since this is a copy object
                        Log.AppLog.WriteEntry(this, "Manually added file, resetting the skip reprocessing option, the file will converted even if it has been processed before", Log.LogEntryType.Debug, true);
                    }

                    Log.AppLog.WriteEntry(this, Localise.GetPhrase("Added new job to queue for") + " " + filePath, Log.LogEntryType.Information, true);
                    ConversionJob job = new ConversionJob(conversionTask, monitorTask, metaData);
                    _jobQueue.Insert(idx, job);
                    filterMatchSuccessful = true; // we cleared filters and processed the file

                    // Send an eMail if required
                    GeneralOptions go               = MCEBuddyConf.GlobalMCEConfig.GeneralOptions;
                    bool           sendEMail        = go.sendEmail; // do we need to send an eMail after adding a job to the queue
                    bool           sendQueue        = go.eMailSettings.queueEvent;
                    string         sendQueueSubject = go.eMailSettings.queueSubject;
                    bool           skipBody         = go.eMailSettings.skipBody;
                    if (sendEMail && sendQueue)
                    {
                        string subject = Localise.GetPhrase("MCEBuddy added a video conversion to the queue");
                        string message = Localise.GetPhrase("Source Video") + " -> " + job.OriginalFileName + "\r\n";
                        message += Localise.GetPhrase("Profile") + " -> " + job.Profile + "\r\n";
                        message += Localise.GetPhrase("Conversion Task") + " -> " + job.TaskName + "\r\n";

                        // Check for custom subject and process
                        if (!String.IsNullOrWhiteSpace(sendQueueSubject))
                        {
                            subject = UserCustomParams.CustomParamsReplace(sendQueueSubject, job.WorkingPath, "", "", job.OriginalFileName, "", "", "", job.Profile, job.TaskName, conversionTask.relativeSourcePath, metaData.MetaData, Log.AppLog);
                        }

                        eMailSendEngine.AddEmailToSendQueue(subject, (skipBody ? "" : message)); // Send the eMail through the eMail engine
                    }
                }
                else if (manual) // Delete manual file entry if we didn't create a conversion job, otherwise the engine will clear it on completion
                {
                    // Manual files may be added multiple times and the filter may already have it from the last time, so be safe and delete it
                    Ini iniManualQueue = new Ini(GlobalDefs.ManualQueueFile); // Delete the entry from the manual queue
                    iniManualQueue.DeleteKey("ManualQueue", filePath);
                }
            }

            // If we have been through all the conversion tasks and not a single task has processed this file, hence we add it to the filter mismatch list so it won't be processed in future (MetadataExtract is very intensive and also prevent overburdening the log file)
            if (!filterMatchSuccessful)
            {
                _conversionTaskFilterMismatchFiles.Add(filePath);
            }
        }