Giter Site home page Giter Site logo

cs-schtick's Introduction

C# Schtick

NuGet version Build status

A scheduled task runner built on Schyntax (a domain-specific language for defining event schedules in a terse, but readable, format).

Usage

Best practice is to make a singleton instance of the Schtick class.

using Schyntax;

var schtick = new Schtick();

// setup an exception handler so we know when tasks blow up
schtick.OnTaskException += (task, exception) => LogException(ex);

// add a task which will call DoSomeTask every hour at 15 minutes past the hour
schtick.AddTask("unique-task-name", "min(15)", (task, timeIntendedToRun) => DoSomeTask());

For complete documentation of schedule format language itself, see the Schyntax project.

AddTask() has several optional arguments which are documented in Schtick.cs and will show up in intellisense.

Async Task Callbacks

For async callbacks, use Schtick.AddAsyncTask().

schtick.AddAsyncTask("task-name", "hour(*)", async (task, time) => await DoSomethingAsync());

ScheduledTask Objects

AddTask() and AddAsyncTask() return an instance of ScheduledTask. This object has properties like Name, IsScheduleRunning, and IsCallbackExecuting. It can be used to start and stop the schedule via StartSchedule() and StopSchedule(). There is also an UpdateSchedule() method which allows you keep the same task, but run it on a different schedule.

The ScheduledTask object is also the first argument to all task callbacks.

Application Shutdown

To ensure the application doesn't terminate in the middle of an executing callback, you should call the Schtick.Shutdown() method before the app terminates. Shutdown is an async method which completes when all scheduled tasks have been stopped and all running callbacks have completed.

A good way to do this in an ASP.NET app is to use a registered object.

public class RegisteredSchtick : IRegisteredObject
{
	public Schtick Schtick { get; }
	
	public RegisteredSchtick(Schtick schtick)
	{
		Schtick = schtick;
	}

	public void Stop(bool immediate)
	{
		if (!Schtick.IsShuttingDown)
		{
			Schtick.Shutdown().ContinueWith(task => HostingEnvironment.UnregisterObject(this));
		}
	}
}

On app start:

_schtick = new Schtick();
HostingEnvironment.RegisterObject(new RegisteredSchtick(_schtick));

cs-schtick's People

Contributors

bretcope avatar rossipedia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

cs-schtick's Issues

Unresolved dependency to Schyntax 1.0.1.32

When I am using the latest version (1.0.4) I am getting an error saying that it requires Schyntax version 1.0.1.32.

Now, the dependencies in the Schtick.nuspec says that <dependency id="Schyntax" version="1.0.1"/> and in NuGet that seems to be translated to >= 1.0.1. When installing this it fetches the latest Schyntax (1.0.3), and building is fine. When running the application things fail with an exception as it tries to find Schyntax-1.0.1.32

This seems to me like the assemblyinfo.cs file contains to "specific" version specs. For our products we on purpose populate the AssemblyVersion tag with only the Major version-number. This is because internally the .Net applications binds to the specific AssemblyVersion of the dependencies used during compilation.

So, in the case with Schtick, when it was built and when the package was created it seems that it was bound to the 1.0.1.32 assembly-version. If you use semantic versioning (as we do) I suggest you change this to contain only the majorversion. I think this is a far more flexible solution than having to add binding-configuration (which is also an option of course, but very annoying...).

Here is a sample from one of my assemblyinfo.cs files:

[assembly: System.Reflection.AssemblyVersion("1")]
[assembly: System.Reflection.AssemblyFileVersion("1.0.1.0")]
[assembly: System.Reflection.AssemblyInformationalVersion("1.0.1-Alpha-151111145421ro")]

Calculate the scheduled expression from localtime instead of UTC?

First of all - I just love this project. It is just about perfect for my needs.

I have found one short-coming though:
Is there a way to specify the base-date to use when the schedules are created? The users of my app write their own schedules using Schyntax domain language, which I then feed into Schtick. However, if the user writes hours(12), then they expect 12 to be according to the localtime that their computer is running. It seems Schyntax is hardwired to do UTC, unless I am mistaken. Is this something that could be extended here or in Schyntax? I am not suggesting adding anything to the Schyntax domain language for the users to tell whether the input is localtime or UTC. If I could just instantiate Schtick with the needed offset then that would be all I needed.

If this is a Schyntax change though, this might have to be changed back in CS-Schyntax. Whether it also affects the base Schyntax project I don't know though.

I can help, if someone can help me get a pointer on where and what to change.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.