Not Just .NET: Run node.js Scripts In a Task – Part 1
Taskmatics Scheduler is known for being a powerful tool that .NET developers can use to simplify their task automation. By providing an easy to use API, it allows developers to leverage the power of the scheduling platform to run custom .NET tasks. What might not be well known is that it’s also super easy to run code outside the .NET framework within a custom task. In this three part series, we’re going to walk through how simple it is to do this by creating a task that runs node.js scripts. In the end, you’ll come away with a custom task that you can use as a basis for scheduling tasks in a bunch of different languages.
Why Run node.js Scripts From Taskmatics Scheduler?
Without Taskmatics Scheduler, managing task automation usually means overseeing a growing number of executables or scripts that are scheduled using Windows Task Scheduler or Cron. The end result is usually a cacophony of code where one task may fail and write some error to a database table or file somewhere on the system while another task just fails and doesn’t indicate the underlying reason. Also, trying to keep track of which jobs successfully ran or are in the process of running can be a nightmare. The beauty of Taskmatics Scheduler is that task scheduling, execution and reporting can be managed from one place: the administration website (‘admin’). Furthermore, logging and reporting for every task is done for you in a centralized and consistent manner.
Taskmatics Scheduler makes it possible to extend the benefits we just covered to other languages and frameworks as well. Since each task instance is spawned off as its own process, you can create your own child processes in every task without having to worry about affecting the overall ecosystem of the Scheduler. This means that any code that be can run from a command line can be run by Taskmatics Scheduler, and you get all the same features and benefits that standard .NET tasks receive.
There are, of course, some pre-requisites before you can run a node.js script (or any other code for that matter) from Taskmatics Scheduler. Node.js isn’t installed as part of the Taskmatics Scheduler installation process, so it’s important that node.js be installed on the computer that has Taskmatics Scheduler installed. This applies to all other languages. The runtimes must be available to execute the code or it simply won’t work. Once that’s out of the way, we can use the Taskmatics Scheduler API to write a task template that can be used to create not only our node.js task, but any other scripting tasks we want to create as well (think Python, Powershell and the like):
public abstract class ExecuteProcessTask : TaskBase { protected override void Execute() { var info = new ProcessStartInfo(GetProcessFileName()); info.Arguments = GetArguments(); info.RedirectStandardOutput = true; info.RedirectStandardError = true; info.UseShellExecute = false; info.CreateNoWindow = true; var process = new Process(); process.StartInfo = info; process.Start(); process.OutputDataReceived += (s, e) => Log("INFO", e.Data); process.ErrorDataReceived += (s, e) => Log("ERROR", e.Data); process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); HandleExitCode(process.ExitCode); } protected abstract string GetProcessFileName(); protected abstract string GetArguments(); protected virtual void Log(string type, string message) { if (message == null) return; Context.Logger.Log("{0}: {1}", type, message); } protected virtual void HandleExitCode(int exitCode) { if (exitCode == 0) return; throw new ApplicationException("The process failed with exit code " + exitCode + "."); } }
Analyzing the Code
Tasks in Taskmatics Scheduler inherit from TaskBase
and must override the Execute
method. This task handles that by using the Process
and ProcessStartInfo
classes of the .NET framework to create a child process and execute it, redirecting all standard and error output to Taskmatics Scheduler’s centralized logging infrastructure to store and stream all output to the user in the admin, just like any other task. Being an abstract class, it provides the GetProcessFileName
method for determining the file system path to the executable that will run our script. The GetArguments
method is where logic around creating the arguments to that script is handled.
The Log
method bridges the outputs of the child process to the Taskmatics Scheduler real time logger out of the gate, but the method is left virtual which allows for customization of how logging is handled if additional logging infrastructure is needed. Finally, the HandleExitCode
method shown above simply throws an exception if the exit code is nonzero, which the Taskmatics Scheduler system will consider a failed task status. Again since the method is virtual it is flexible for those who may need more complex behavior when a process completes.
Another key feature of Taskmatics Scheduler is its extensibility. It’s a snap to use custom parameter objects in these tasks, which makes extending ExecuteProcessTask
into a single task that can run any node.js script a piece of cake.
Next Up…
With a simple task, we’ve provided a basis for running code written in any language within Taskmatics Scheduler. This allows you to centralize all of your task automation under one roof, regardless of the language. You also get a consistent execution and logging pattern that can make maintenance of a large number of disparate tasks much easier than using Windows Task Scheduler or Cron. In the next article, we’ll create a simple node.js script and a wrap it in a task using the ExecuteProcessTask
class from this article, and we’ll finish up the series by demonstrating how easy it is to schedule our new node.js task in the admin and see it run.