Convert your console app to Azure webjobs
I want to use WebJob to replace my existing console application. The console application accepts 1 of 5 parameters using the style / argument syntax that I am currently running with Windows Scheduler on my VM. Each schedule launches the application with a specific argument. Some tasks run every 5 minutes, others every 6 hours.
When I download a ZIP archive containing my console application and its dependencies, I expected to be able to provide a command line argument for the application. This is not true.
If I create a batch file (like Send-Emails.bat) and then create a new WebJob called Send-Emails, it launches the batch file. This is fine, but it would mean that I need to create 5 different WebJob (again, no problem) and download the ZIP 5 times, each with a separate batch file that invokes the console with an argument.
This last step seems very inefficient and quickly becomes a maintenance nightmare. How to deploy such a system would also create problems.
This looks like a basic scenario, and I am assuming the Azure team has developed WebJobs. Has anyone had any success with this? I'd rather not change my code if at all possible, and calling WebJobs from my web app using the API doesn't seem to be ideal either.
source to share
A few suggestions:
- You can try sharing the implementation files of the job instances: Can multiple Azure websites share the same assemblies?
- Why don't you have just one job, with a batch file calling your console application five times with different arguments?
- Deployment Automation: Enable WebJobs Deployment for Existing Console Application Project
- Or if you don't want to mess with WebSites in Visual Studio, write a script to update the FTPS jobs. For example. with WinSCP: Automating WebJob Updates
(I'm the author of WinSCP)
source to share
While this is not an ideal solution as it is still associated with the scheduler, the solution below has several advantages. Let me first describe how it works:
You have one WebJob that uses the WebJobs SDK. Within this job, you have 5 different functions, one for each assignment you set. Each of these functions listens on a different storage queue. Then you have 5 schedulers (one for each function) that will put the message on the queue corresponding to the function to be called.
The advantage of this solution is that you can change the function schedule without touching the job code, and you have all your jobs / functions in one place.
Here is some sample code for a webjob:
public static void Main()
{
using (JobHost host = new JobHost())
{
host.RunAndBlock();
}
}
public static void SendEmailFunction(
[QueueTrigger("send-email-function-trigger")] string message,
TextWriter log,
{
log.WriteLine("sending an email");
// Send email
}
public static void DoSomethingElse(
[QueueTrigger("do-something-else-trigger")] string message
TextWriter log)
{
log.WriteLine("Doing something else");
}
// ... more functions ...
Then, if you have a SendEmail scheduler that puts a message in "trigger-function-trigger-trigger-trigger", the SendEmailFunction will run.
Another option is to use some environment variables instead of command line arguments. Based on the job name, you look in the environment variable for the value of the argument.
source to share