Add, update or delete a repeated task with Windows Task Scheduler.
コード例 #1
0
ファイル: Program.cs プロジェクト: daptiv/Malevich
        /// <summary>
        /// Removes the notifier app.
        /// </summary>
        /// <param name="installParams"> Installation parameters.  </param>
        private static void RemoveMailer(InstallParameters installParams)
        {
            Log("Uninstalling notifier.");

            string notifier = Path.Combine(installParams.NotifierDirectory, "ReviewNotifier.exe");
            if (File.Exists(notifier))
            {
                Log("  Removing scheduled task.");
                TaskScheduler taskScheduler = new TaskScheduler();
                taskScheduler.Interval = installParams.NotifierInterval;
                taskScheduler.TaskName = "MalevichReviewNotifier";
                taskScheduler.TaskPath = notifier;

                taskScheduler.SetTask(null, null);

                using (Process client = new Process())
                {
                    Log("Removing notifier configuration.");
                    client.StartInfo.UseShellExecute = false;
                    client.StartInfo.RedirectStandardError = true;
                    client.StartInfo.RedirectStandardOutput = true;
                    client.StartInfo.CreateNoWindow = true;
                    client.StartInfo.FileName = notifier;
                    client.StartInfo.Arguments = "reset";

                    client.Start();
                    string stderr;
                    string stdout = Malevich.Util.CommonUtils.ReadProcessOutput(client, false, out stderr);
                    if (!String.IsNullOrEmpty(stderr))
                        Log(stderr);
                    if (!String.IsNullOrEmpty(stdout))
                        Log(stdout);
                }

            }

            Log("  Removing notifier files.");
            string[] files = { "ReviewNotifier.exe", "ReviewNotifier.exe.config", "CodeReviewDataModel.dll",
                "CommonUtils.dll" };

            foreach (string file in files)
            {
                string target = Path.Combine(installParams.NotifierDirectory, file);
                if (File.Exists(target))
                    File.Delete(target);
            }

            if (IsDirectoryEmpty(installParams.NotifierDirectory))
            {
                Log("  Removing the empty directory.");
                try
                {
                    Directory.Delete(installParams.NotifierDirectory);
                }
                catch (IOException ioe)
                {
                    Log("    Could not remove notifier directory: I/O error.");
                    Log(ioe.ToString());
                }
                catch (UnauthorizedAccessException uae)
                {
                    Log("    Could not remove notifier directory: access denied.");
                    Log(uae.ToString());
                }
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: daptiv/Malevich
        /// <summary>
        /// Add, update or delete notifier as repetition task in Windows Task Scheduler.
        /// </summary>
        /// <param name="args"> The argument array, presumed to be {"schedule", interval_in_minutes} </param>
        private static void SetScheduleTask(string[] args)
        {
            int interval;
            if (args.Length != 2 || !Int32.TryParse(args[1], out interval))
            {
                Console.Error.WriteLine("ReviewNotifier schedule repetition_interval_in_minutes");
                Console.Error.WriteLine(
                    "Set the repetition interval to a positive integar to add/update a scheduled task.");
                Console.Error.WriteLine("Set the repetition interval to 0 to delete the existing task.");
                return;
            }

            TaskScheduler taskScheduler = new TaskScheduler();
            taskScheduler.Interval = interval;
            taskScheduler.TaskName = SchedulTaskName;
            taskScheduler.TaskPath = Process.GetCurrentProcess().MainModule.FileName;

            if (!ReviewNotifierConfiguration.Load())
            {
                Console.Error.WriteLine("Please configure service parameters before echeduling the task!");
                return;
            }

            bool success = taskScheduler.SetTask(Config.User, Config.Password);
            if (success)
                Console.Error.WriteLine(
                    "Created/updated/deleted the scheduled task successfully. (repetition = {0} min)",
                    taskScheduler.Interval);
            else
                Console.Error.WriteLine("Failed to set schedule task, please create the task manually.");
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: daptiv/Malevich
        /// <summary>
        /// Configures the notifier app.
        /// </summary>
        /// <param name="installParams"> Installation parameters.  </param>
        private static void InstallAndConfigureMailer(InstallParameters installParams)
        {
            Log("Installing notifier.");

            string targetDir = installParams.NotifierDirectory == null ?
                Path.Combine(installParams.InstallTarget, "notifier") : installParams.NotifierDirectory;

            if (!Directory.Exists(targetDir))
            {
                Log("  Creating the directory.");
                Directory.CreateDirectory(targetDir);
            }

            Log("  Copying files.");
            string[] files = { "ReviewNotifier.exe", "ReviewNotifier.exe.config", "CodeReviewDataModel.dll",
                "CommonUtils.dll" };
            foreach (string file in files)
                File.Copy(Path.Combine(installParams.InstallSource, file), Path.Combine(targetDir, file), true);

            if (installParams.SmtpServer == null)
                return;

            Log("  Configuring notifier.");

            string notifier = Path.Combine(targetDir, "ReviewNotifier.exe");
            using (Process client = new Process())
            {
                Log("    Configuring smtp.");
                client.StartInfo.UseShellExecute = false;
                client.StartInfo.RedirectStandardError = true;
                client.StartInfo.RedirectStandardOutput = true;
                client.StartInfo.CreateNoWindow = true;
                client.StartInfo.FileName = notifier;
                string args = "smtp " + installParams.SmtpServer + " " + installParams.CompanyDomain;
                if (installParams.UseLdap)
                    args += " useldap";
                if (installParams.UseSsl)
                    args += " usessl";
                client.StartInfo.Arguments = args;

                client.Start();
                string stderr;
                string stdout = Malevich.Util.CommonUtils.ReadProcessOutput(client, false, out stderr);
                if (!(String.IsNullOrEmpty(stderr) && String.IsNullOrEmpty(stdout)))
                {
                    Log(stderr);
                    Log(stdout);
                    throw new InstallationException("Failed setting up smpt parameters.");
                }
            }

            using (Process client = new Process())
            {
                Log("    Configuring creds.");
                client.StartInfo.UseShellExecute = false;
                client.StartInfo.RedirectStandardError = true;
                client.StartInfo.RedirectStandardOutput = true;
                client.StartInfo.CreateNoWindow = true;
                client.StartInfo.FileName = notifier;
                client.StartInfo.Arguments = "credentials \"" + Environment.UserName + "\" " +
                    installParams.AliasToSendFrom;

                client.Start();
                string stderr;
                string stdout = Malevich.Util.CommonUtils.ReadProcessOutput(client, false, out stderr);
                if (!(String.IsNullOrEmpty(stderr) && String.IsNullOrEmpty(stdout)))
                {
                    Log(stderr);
                    Log(stdout);
                    throw new InstallationException("Failed setting up credentials.");
                }
            }

            using (Process client = new Process())
            {
                Log("    Configuring webserver.");
                client.StartInfo.UseShellExecute = false;
                client.StartInfo.RedirectStandardError = true;
                client.StartInfo.RedirectStandardOutput = true;
                client.StartInfo.CreateNoWindow = true;
                client.StartInfo.FileName = notifier;
                client.StartInfo.Arguments = "webserver " + Environment.MachineName.ToLower();

                client.Start();
                string stderr;
                string stdout = Malevich.Util.CommonUtils.ReadProcessOutput(client, false, out stderr);
                if (!(String.IsNullOrEmpty(stderr) && String.IsNullOrEmpty(stdout)))
                {
                    Log(stderr);
                    Log(stdout);
                    throw new InstallationException("Failed setting up server name.");
                }
            }

            using (Process client = new Process())
            {
                Log("    Configuring database.");
                client.StartInfo.UseShellExecute = false;
                client.StartInfo.RedirectStandardError = true;
                client.StartInfo.RedirectStandardOutput = true;
                client.StartInfo.CreateNoWindow = true;
                client.StartInfo.FileName = notifier;
                client.StartInfo.Arguments = "database " + installParams.Database;

                client.Start();
                string stderr;
                string stdout = Malevich.Util.CommonUtils.ReadProcessOutput(client, false, out stderr);
                if (!(String.IsNullOrEmpty(stderr) && String.IsNullOrEmpty(stdout)))
                {
                    Log(stderr);
                    Log(stdout);
                    throw new InstallationException("Failed setting up database.");
                }
            }

            Log("    Configuring scheduler.");
            TaskScheduler taskScheduler = new TaskScheduler();
            taskScheduler.Interval = installParams.NotifierInterval;
            taskScheduler.TaskName = "MalevichReviewNotifier";
            taskScheduler.TaskPath = notifier;

            bool success = taskScheduler.SetTask(null, null);
            if (!success)
            {
                Log("Failed to schedule the notifier, please create the task manually.");
                throw new InstallationException("Failed configuring the scheduler.");
            }
        }