Jump to content

Scheduled Task report progress.


Recommended Posts

I have a scheduled task in a plugin, after calculating progress inside my plugin I end up with a double I would like to send back to the server to update the task interval.

 

The 'Execute' command takes a '<IProgress> double' as an argument, but I'm not sure where to give back my number to update the task.

public class FileCompressionCopyScheduledTask : IScheduledTask, IConfigurableScheduledTask
    {
        private static ILogger Logger { get; set; }
        public FileCompressionCopyScheduledTask(ILogger logger)
        {
            Logger = logger;
        }
        public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
        {
            var config = new PluginConfiguration();
            foreach (DirectoryInfo di in from folder in Directory.GetDirectories(config.MonitoredFolder)
                                         where !folder.ToLower().Contains("sample")
                                         select new DirectoryInfo((folder))
                into di
                                         where !File.Exists(di.FullName + "\\####emby.extracted####")
                                         select di)
            {
                //Create the Folder Marker so as not to read it again.
                File.Create(di.FullName + "\\####emby.extracted####");


                foreach (FileInfo fi in di.GetFiles().Where(fi => fi.Extension == ".rar"))
                {
                    UnzipAndCopyFiles.BeginDecompressionAndCopy(fi.FullName, fi.Name, Logger);

                    Plugin.Instance.UpdateConfiguration(new PluginConfiguration()
                    {
                        ExtractionDetails = new ExtractionInfo()
                        {
                            Name = string.Empty,
                            Percentage = 0 //<- I could send back this double when its finsihed
                        }
                    });

                    config.CompletedItems.Add(Path.GetFileNameWithoutExtension(fi.Name));
                }


                foreach (FileInfo fi in di.GetFiles().Where(fi => fi.Extension == ".mkv" ||
                                                                  fi.Extension == ".mp4" ||
                                                                  fi.Extension == ".avi"))
                {
                    CopyFiles.BeginCopy(fi);
                    Plugin.Instance.UpdateConfiguration(new PluginConfiguration()
                    {
                        ExtractionDetails = new ExtractionInfo()
                        {
                            Name = string.Empty,
                            Percentage = 0.0; //<- I could send back this double when its finsihed
                        }
                    });

                    config.CompletedItems.Add(Path.GetFileNameWithoutExtension(fi.Name));
                }

                Plugin.Instance.UpdateConfiguration(new PluginConfiguration()
                {
                    ExtractionDetails = new ExtractionInfo()
                    {
                        Name = string.Empty,
                        Percentage = 0.0; //<- I could send back this double when its finsihed
                    }
                });



            }

Inside my compression function:

 

I could send updates to the server in several place:

        private static void Archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e)
        {
            var config = new PluginConfiguration();
            
            long b = e.CompressedBytesRead;
            var extractionInfo = new ExtractionInfo
            {
                Name = config.ExtractionDetails.Name,

                //Percentage can send updates to the server scheduled Task
                Percentage = Math.Round(((double) e.CompressedBytesRead / (double) totalSize) * 100)
            
            };

            Plugin.Instance.UpdateConfiguration(new PluginConfiguration()
            {
                ExtractionDetails = extractionInfo
            });

        }

For copying files this callback:

             CopyFileCallbackAction CopyFilesCallback(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
            {
                
                var extractionInfo = new ExtractionInfo
                {

                    //Again this percentage needs to return to the server to update the Scheduled Task
                    Percentage = Math.Round(((double) totalBytesTransferred / (double) totalFileSize) * 100.0),
                    
                    Name = config.ExtractionDetails.Name

                };

                Plugin.Instance.UpdateConfiguration(new PluginConfiguration
                {
                    ExtractionDetails = extractionInfo
                });
                return CopyFileCallbackAction.Continue;
            }

Is there an interface for that?

Edited by chef
  • Like 1
Link to post
Share on other sites

You just call progress.Report throughout the process. So as your compression and copy operations are ongoing and reporting their progress to you, you'll calculate some type of total progress value and report that back to the scheduled task.

  • Like 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...