Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Commit

Permalink
RedisWatcher project added
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryRawas committed Jun 20, 2012
1 parent 70b6575 commit 4931e55
Show file tree
Hide file tree
Showing 19 changed files with 2,382 additions and 0 deletions.
19 changes: 19 additions & 0 deletions msvs/RedisWatcher/.gitignore
@@ -0,0 +1,19 @@
*.log
*.user
*.exe
*.sdf
*.suo
*.bin
*.tlog
*.filters
*.opensdf
Install/obj/*
Install/bin/*
Install/Debug/*
Install/Release/*
RedisWatcher/Debug/*
RedisWatcher/Release/*
Debug/*
Release/*
RedisWatcher/RedisWatcher.h
RedisWatcher/RedisWatcher.rc
51 changes: 51 additions & 0 deletions msvs/RedisWatcher/Install/Install.wixproj
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>3.5</ProductVersion>
<ProjectGuid>{f152845b-db83-4862-909c-0476930233c1}</ProjectGuid>
<SchemaVersion>2.0</SchemaVersion>
<OutputName>InstallWatcher</OutputName>
<OutputType>Package</OutputType>
<WixTargetsPath Condition=" '$(WixTargetsPath)' == '' AND '$(MSBuildExtensionsPath32)' != '' ">$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
<WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>Debug</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Include="Product.wxs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RedisWatcher\RedisWatcher.vcxproj">
<Name>RedisWatcher</Name>
<Project>{a249484f-526d-4725-9434-430f74837ffe}</Project>
<Private>True</Private>
<DoNotHarvest>True</DoNotHarvest>
<RefProjectOutputGroups>Binaries;Content;Satellites</RefProjectOutputGroups>
<RefTargetDir>INSTALLLOCATION</RefTargetDir>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<WixExtension Include="WixUtilExtension">
<HintPath>$(WixExtDir)\WixUtilExtension.dll</HintPath>
<Name>WixUtilExtension</Name>
</WixExtension>
</ItemGroup>
<Import Project="$(WixTargetsPath)" />
<!--
To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Wix.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
37 changes: 37 additions & 0 deletions msvs/RedisWatcher/Install/Product.wxs
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util='http://schemas.microsoft.com/wix/UtilExtension'>
<Product Id="3404a743-6802-41dd-a0bd-9229827fd2af" Name="RedisWatcher" Language="1033" Version="1.0.0.0" Manufacturer="MsOpenTech"
UpgradeCode="dcf0348c-eb55-4f12-b639-15310d3a9b5d">
<Package InstallerVersion="200" Compressed="yes" />

<Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />

<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLLOCATION" Name="RedisWatcher">
<Component Id="ProductComponent" Guid="1b743227-f33f-426c-a0ea-20bd159b21d6">
<File Id="RedisWatcher.exe" Name="$(var.RedisWatcher.TargetFileName)" Source="$(var.RedisWatcher.TargetPath)"
DiskId="1" KeyPath="yes" />
<File Id="RedisWatcher.man" Name="RedisWatcher.man" Source="$(var.RedisWatcher.ProjectDir)" DiskId="1" >
<util:EventManifest MessageFile="[INSTALLLOCATION]RedisWatcher.exe" ResourceFile="[INSTALLLOCATION]RedisWatcher.exe"/>
</File>
<File Id="watcher.conf" Name="watcher.conf" Source="$(var.RedisWatcher.ProjectDir)" DiskId="1" >
<Permission GenericAll="yes" User=".\Users" />
<Permission GenericAll="yes" User=".\Administrators" />
</File>
<ServiceInstall Id="RedisWatcherSvc" Name="RedisWatcherSvc" DisplayName ="Redis watcher" ErrorControl="normal"
Start="auto" Type="ownProcess" Description="Redis process starter and watchdog">
</ServiceInstall>
<ServiceControl Id="SvcControl" Name="RedisWatcherSvc" Remove="uninstall" Stop="both" Wait="no" />
</Component>
</Directory>
</Directory>
</Directory>

<Feature Id="ProductFeature" Title="RedisWatcher" Level="1">
<ComponentRef Id="ProductComponent" />

<ComponentGroupRef Id="Product.Generated" />
</Feature>
</Product>
</Wix>
58 changes: 58 additions & 0 deletions msvs/RedisWatcher/README.md
@@ -0,0 +1,58 @@
redis-watcher
=============
RedisWatcher is an application that will run one or more instances of redis-server.
If the redis-server process terminates, then RedisWatcher will restart it.

RedisWatcher is installed as a Windows service.
It can also run as an application by passing 'console' as a command line argument.

Building
========
Visual Studio 2010 was used to create the solution and project files.
The solution includes an installer project that uses WiX Toolset v3.5.
You can download WiX from http://wix.codeplex.com
The RedisWatcher project uses mc.exe to process the RedisWatcher.man to create .h and .rc files


Configuring the watcher
=======================
The watcher.conf file configures RedisWatcher.
The service will load the watcher.conf from the same folder as RedisWatcher.exe.
A sample watcher.conf is provided by the installer.

Specify the location of the redis-server executable to run, as well as the executable file name.
Configure each instance of redis-server to be run.
- the working directory must be unique per instance
- the cmdparms is used to specify the configuration file to load if any.
If running multiple instances, use a Redis configuration file to specify the listening port per instance

If watcher.conf is modified while the service is running, it is reloaded.
- Any running redis-server processes are not terminated by RedisWatcher.
- If new redis-server instances are configured, new ones will be started.


Tracing
=======
ETW event messages are defined in RedisWatcher.man.
This is compiled during the build to create .h and .rc files.
The installer modifies the 'messageFileName' and 'resourceFileName' attributes.
The event provider is registered with Windows by the installer.

To manually register the event provider
wevtutil im RedisWatcher.man

To manually uninstall the event provider
wevtutil um RedisWatcher.man

To start tracing
logman start mytrace -p MsOpenTech-RedisWatcher -o mytrace.etl -ets

To stop tracing
logman stop mytrace -ets

To convert trace output to xml file
tracerpt mytrace.etl

Other tools are available for formatting the trace.


42 changes: 42 additions & 0 deletions msvs/RedisWatcher/RedisWatcher.sln
@@ -0,0 +1,42 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "RedisWatcher", "RedisWatcher\RedisWatcher.vcxproj", "{A249484F-526D-4725-9434-430F74837FFE}"
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Install", "Install\Install.wixproj", "{F152845B-DB83-4862-909C-0476930233C1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|Win32 = Debug|Win32
Debug|x86 = Debug|x86
Release|Mixed Platforms = Release|Mixed Platforms
Release|Win32 = Release|Win32
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A249484F-526D-4725-9434-430F74837FFE}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Debug|Win32.ActiveCfg = Debug|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Debug|Win32.Build.0 = Debug|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Debug|x86.ActiveCfg = Debug|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Release|Mixed Platforms.Build.0 = Release|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Release|Win32.ActiveCfg = Release|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Release|Win32.Build.0 = Release|Win32
{A249484F-526D-4725-9434-430F74837FFE}.Release|x86.ActiveCfg = Release|Win32
{F152845B-DB83-4862-909C-0476930233C1}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{F152845B-DB83-4862-909C-0476930233C1}.Debug|Mixed Platforms.Build.0 = Debug|x86
{F152845B-DB83-4862-909C-0476930233C1}.Debug|Win32.ActiveCfg = Debug|x86
{F152845B-DB83-4862-909C-0476930233C1}.Debug|x86.ActiveCfg = Debug|x86
{F152845B-DB83-4862-909C-0476930233C1}.Debug|x86.Build.0 = Debug|x86
{F152845B-DB83-4862-909C-0476930233C1}.Release|Mixed Platforms.ActiveCfg = Release|x86
{F152845B-DB83-4862-909C-0476930233C1}.Release|Mixed Platforms.Build.0 = Release|x86
{F152845B-DB83-4862-909C-0476930233C1}.Release|Win32.ActiveCfg = Release|x86
{F152845B-DB83-4862-909C-0476930233C1}.Release|x86.ActiveCfg = Release|x86
{F152845B-DB83-4862-909C-0476930233C1}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
173 changes: 173 additions & 0 deletions msvs/RedisWatcher/RedisWatcher/RedisWatcher.c
@@ -0,0 +1,173 @@
/***********************************************************************
* Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR
* A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache License, Version 2.0 for specific language governing
* permissions and limitations under the License.
*
**********************************************************************/

#include "stdafx.h"
#include "watcher.h"

// Globals

// Name of configuration file
wchar_t * ConfigFile = L"watcher.conf";
wchar_t * configPath = NULL;

// Lock used to prevent threads from corrupting data
CRITICAL_SECTION ConfigLock;

// Locking functions
void Lock()
{
EnterCriticalSection(&ConfigLock);
}

void Unlock()
{
LeaveCriticalSection(&ConfigLock);
}

void InitLock()
{
InitializeCriticalSection(&ConfigLock);
}

void TermLock()
{
DeleteCriticalSection(&ConfigLock);
}

//
// Purpose:
// Initialize the watcher
//
// Parameters:
// configuration path
//
// Return value:
// TRUE or FALSE
//
BOOL WatcherStart(wchar_t * path)
{
WatcherConfig * config;

EventRegisterMsOpenTech_RedisWatcher();
InitLock();

EventWriteWatcher_Start();
if (CombineFilePath(path, ConfigFile, &configPath))
{
config = parseConfig(configPath);

if (config != NULL)
{
initialize(config);
}

startMonitorConfigFile(configPath);

return TRUE;
}

return FALSE;
}

//
// Purpose:
// Terminate the watcher
//
// Parameters:
// none
//
// Return value:
// none
//
void WatcherStop()
{
stopMonitorConfigFile();
cleanup();
EventWriteWatcher_Stop();

EventUnregisterMsOpenTech_RedisWatcher();
TermLock();
}

//
// Purpose:
// Main entry point
// Start as either a service or a console application
//
// Parameters:
// argc, argv
// no arguments means start as service
// "console" means start as console.
//
// Return value:
// exit code
//
int wmain(int argc, wchar_t* argv[])
{
wchar_t * path = NULL;

// If command-line parameter is "console", run as console app.
// Otherwise, the service is probably being started by the SCM.

if (argc > 1)
{
if (_wcsicmp(argv[1], L"console") == 0)
{

if (GetCurrentDir(&path))
{
wchar_t buff[100];

// Start redis watcher
if (!WatcherStart(path))
{
printf("Failed to start watcher\n");
return 1;
}

// run until user enters x
while (1)
{
_getws_s(buff, 100);
if (buff[0] == L'x') break;
}

WatcherStop();

return 0;
}
else
{
return 1;
}
}
else
{
printf("Parameter not valid\n");
return 1;
}
}
else
{
SvcStart();
return 0;
}

return 1;
}

Binary file added msvs/RedisWatcher/RedisWatcher/RedisWatcher.man
Binary file not shown.

0 comments on commit 4931e55

Please sign in to comment.