For the previous example I've showed how to create a console application that runs every X seconds using Quartz.NET. So when the job is ran you might want to trigger a SMS alert in cases like, some processes are not running, DB no responding, HDD space running out.
For the SMS providers the 'hot' one would be Twilio, and you can get their helper library for C# at https://github.com/twilio/twilio-csharp#readme
You need to register and get your account id and token that you'll need for your API calls. I think there's a limited number of free SMS using the free trial account. If you upgrade then the SMS are charged per message. E.g. For Singapore where I'm located it's 2cents for sending a message to my carrier.
Sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twilio;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//You should replace those in {} with your actual sid, token, twilio number and recipient phone
//number
TwilioRestClient client = new TwilioRestClient("{your account sid}", "{your account token}");
var message = client.SendSmsMessage("your twilio number", "recipient's phone number", "Your message");
Console.WriteLine(message.Sid);
}
}
}
Thursday, February 21, 2013
Friday, February 15, 2013
Schedule job to check process running
In my day job I had to have a scheduled job to check whether an application was running. If it wasn't then it needs to be restarted.
As any developer would do I got into the rabbit hole fast, worrying about the threads/schedules etc. Alas Quartz.Net came to my rescue. You can find it at Quartz.Net.
Quartz.Net is a job scheduler for .Net based on Quartz (Java). It is a very flexible and comprehensive library. Download it and just add the Quartz.dll to your project from the release directory of the zip file.
Below are some sample code that might help you get started
Program.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using Quartz.Core;
using Quartz.Job;
using Quartz.Simpl;
using Quartz.Util;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Create scheduler factory
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();
//Get interval you want to check
String interval = ConfigurationManager.AppSettings["CheckInterval"];
int checkInterval;
//No AppSetting set it to a default value
if (!int.TryParse(interval, out checkInterval))
checkInterval = 60;
// define the job and tie it to our Class1 class
IJobDetail job = JobBuilder.Create().WithIdentity("Checker", "Group1").Build();
// Trigger the job to run on the next round minute
ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger", "Group1").StartNow()
.WithDailyTimeIntervalSchedule(x => x.WithIntervalInSeconds(checkInterval))
.Build();
// Tell quartz to schedule the job using our trigger
sched.ScheduleJob(job, trigger);
// Start up the scheduler (nothing can actually run until the
// scheduler has been started)
sched.Start();
String quit = Console.ReadLine();
}
}
}
Class1.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using System.Configuration;
using System.Diagnostics;
namespace ConsoleApplication1
{
[PersistJobDataAfterExecution]
class Class1:IJob
{
public void Execute(IJobExecutionContext context)
{
//Example to check whether a application is running, use WebClient to check whether website is up.
//it's up to you.
String ProcessName = ConfigurationManager.AppSettings["ProcessName"];
if (!String.IsNullOrEmpty(ProcessName))
{
var running = Process.GetProcesses().Where(prc => prc.ProcessName.ToLower().Contains(ProcessName)).FirstOrDefault();
if (running == null)
{
//Process is not running!! Call Mama!!
//Send a email
//Try running the process again
//Twilio integration for SMS!!
Console.Out.WriteLine(ProcessName + " is NOT Running!");
}
}
}
}
}
Your app.config should have two keys with "CheckInterval" and "ProcessName".
As any developer would do I got into the rabbit hole fast, worrying about the threads/schedules etc. Alas Quartz.Net came to my rescue. You can find it at Quartz.Net.
Quartz.Net is a job scheduler for .Net based on Quartz (Java). It is a very flexible and comprehensive library. Download it and just add the Quartz.dll to your project from the release directory of the zip file.
Below are some sample code that might help you get started
Program.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using Quartz.Core;
using Quartz.Job;
using Quartz.Simpl;
using Quartz.Util;
using System.Configuration;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Create scheduler factory
ISchedulerFactory sf = new StdSchedulerFactory();
IScheduler sched = sf.GetScheduler();
//Get interval you want to check
String interval = ConfigurationManager.AppSettings["CheckInterval"];
int checkInterval;
//No AppSetting set it to a default value
if (!int.TryParse(interval, out checkInterval))
checkInterval = 60;
// define the job and tie it to our Class1 class
IJobDetail job = JobBuilder.Create
// Trigger the job to run on the next round minute
ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger", "Group1").StartNow()
.WithDailyTimeIntervalSchedule(x => x.WithIntervalInSeconds(checkInterval))
.Build();
// Tell quartz to schedule the job using our trigger
sched.ScheduleJob(job, trigger);
// Start up the scheduler (nothing can actually run until the
// scheduler has been started)
sched.Start();
String quit = Console.ReadLine();
}
}
}
Class1.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using System.Configuration;
using System.Diagnostics;
namespace ConsoleApplication1
{
[PersistJobDataAfterExecution]
class Class1:IJob
{
public void Execute(IJobExecutionContext context)
{
//Example to check whether a application is running, use WebClient to check whether website is up.
//it's up to you.
String ProcessName = ConfigurationManager.AppSettings["ProcessName"];
if (!String.IsNullOrEmpty(ProcessName))
{
var running = Process.GetProcesses().Where(prc => prc.ProcessName.ToLower().Contains(ProcessName)).FirstOrDefault();
if (running == null)
{
//Process is not running!! Call Mama!!
//Send a email
//Try running the process again
//Twilio integration for SMS!!
Console.Out.WriteLine(ProcessName + " is NOT Running!");
}
}
}
}
}
App.config
Your app.config should have two keys with "CheckInterval" and "ProcessName".
Thursday, February 14, 2013
My Favourite book on creativity
I'm very interested in the studies on Creativity and throughout the years I've read as much as I could on this topic. Throughout my years of reading, there were a lot of books that I've wasted money on. Most of these books have a tendency of talking too much about case studies that you and I can easily find on the web or writings that doesn't include a concrete way for you to embark on your quest for becoming a creative person.
A look into my Kindle library these include:
1. Imagine: How Creativity Works by John Lehrer
2. Cracking Creativity by Michael Michalko
3. Lateral Thinking by Edward De Bono
4. Your Creative Brain by Shelley Carson
5. Creativity by Mihaly Csikszentmihalyi
6. Disciplined Dreaming by Josh Linkner
Out of the 6 books, the only book that I would recommend is Disciplined Dreaming: A Proven System to Drive Breakthrough Creativity (Amazon affiliate link) His book helps you to be creative by giving you processes that improves you or your team's creativity. All of these processes was used by the author in his company ePrize to help him innovate keep his company on it's toes. These process includes:
1. Asking the three magic questions
2. Developing the five skills of master innovators
3. Looking through different lens
4. Putting patterns to use
There are more in the book but I'll just list these 4 as examples.
For a brief description, the "Looking through a different lens" process allows you to find new perspectives for a problem.This can be achieved by looking at a problem with different roles. i.e. Student, Musician, Architect or a Villain. There's more in the chapter so go have a look at the book.
I really highly recommend this book to whoever wants a step 1 - 10 guide on improving your creativity. If you have any other book to recommend please feel free to let me know in the comments.
A look into my Kindle library these include:
1. Imagine: How Creativity Works by John Lehrer
2. Cracking Creativity by Michael Michalko
3. Lateral Thinking by Edward De Bono
4. Your Creative Brain by Shelley Carson
5. Creativity by Mihaly Csikszentmihalyi
6. Disciplined Dreaming by Josh Linkner
Out of the 6 books, the only book that I would recommend is Disciplined Dreaming: A Proven System to Drive Breakthrough Creativity (Amazon affiliate link) His book helps you to be creative by giving you processes that improves you or your team's creativity. All of these processes was used by the author in his company ePrize to help him innovate keep his company on it's toes. These process includes:
1. Asking the three magic questions
2. Developing the five skills of master innovators
3. Looking through different lens
4. Putting patterns to use
There are more in the book but I'll just list these 4 as examples.
For a brief description, the "Looking through a different lens" process allows you to find new perspectives for a problem.This can be achieved by looking at a problem with different roles. i.e. Student, Musician, Architect or a Villain. There's more in the chapter so go have a look at the book.
I really highly recommend this book to whoever wants a step 1 - 10 guide on improving your creativity. If you have any other book to recommend please feel free to let me know in the comments.
Subscribe to:
Posts (Atom)