1

From my other question i learned that asp.net kills my threads because 'it feels like it'. These threads are my daemons that handle thumbnail generation, registration emails and other things.

I was told to use a 'Windows Service' but what i know of, its an app you install on your machine that runs on bootup. There are several problems with using that if thats what he meant. The last time i attempted a service i remember it prove difficult and i just put the app in the startup files. anyways I AM NOT ON WINDOWS. I am on linux.

The other problems having it in a separate app is i have much code in asp.net that would need to be copied over which i dont want to do. The other is i have asp.net poke the thread using ManualResetEvent whenever it needs it so i am not hammering the db and get immediate results. AFAIK i dont know of any way to poke another process.

How might i run this code? with asp.net? I dont think checking if the threads exist every request is a good idea. What can i do?

Community
  • 1
  • 1
  • Your site architecture sounds strange. I handle thumbnail generation by giving a `thumbnails.ashx` file as the img src, and then generating + returning thumbnails when needed. Registration emails are sent out when required by use action. – Oliver Oct 03 '11 at 11:40
  • Just add a windows service project to your project and then include the projects you need. If this is not possible there is something worng with your architecture – Kimtho6 Oct 03 '11 at 11:44
  • @Oliver: ok thats fine but i also process audio files. If a user makes 10 1min samples and uploads them at the same time theres no way i can fit 10ffmpeg process into ram at the same time. Now if another user does the same thing at the same time with 5 3minute samples.... I definitely cant be doing that. –  Oct 03 '11 at 11:57
  • @Kim: I just created a windows service project. From what i can see 1) I cant run it on the command line or debug it in visual studios (wtf, really, no debugging) 2) I have to install it with a window specific exe. My server is linux. –  Oct 03 '11 at 12:18
  • 1
    @acidzombie24 take a look at this post: http://stackoverflow.com/questions/637948/how-to-migrate-a-net-windows-service-application-to-linux-using-mono I hope it gets you in the right direction – Kimtho6 Oct 03 '11 at 12:23
  • @Kim: It seems like all answers say put it in a different process which means i need to refactor my code into a (site specific) library or copy/paste them. This... will be somewhat annoying. Also it means i can no longer poke the thread with ManualResetEvent as its on another process. Thats really lame :( -edit- btw if you put that as an answer i'll accept it –  Oct 03 '11 at 12:27
  • 1
    @acidzombie24 you want to structure your code such that you test and debug it using a command line application, and then you package it into a service installer for deployment. This basically just means put all of your code in DLLs and nothing but the invocation of it in the console app & service host. – Chris Marisic Oct 03 '11 at 12:30
  • Also I'm not sure how relevant the service installer part would be for running on linux as opposed to windows. You might just want to build a console app that runs on an infinite loop, that checks it's app.config for a value to exit as the loop terminator. Then you can setup a cron job that starts your console, and terminates it by updating the app.config as needed. (If you do this, make sure to sleep your app every cycle otherwise your console app will nearly spin lock your cpu or atleast 1 core) – Chris Marisic Oct 03 '11 at 12:39

2 Answers2

3

ASP.NET is designed to follow a request-response pattern. It has mechanisms specifically to prevent leaving threads running outside of a request, such as killing long-running threads (which it considers "broken").

What you are asking to do is not the intended function of ASP.NET. To do what you want in ASP.NET, you will be fighting the platform, not building on it. This is why you've been told to write another application which performs your long-running processes. If you're on Linux, that means writing a deamon that can host the threads you want to run continuously in the background, because that's specifically their purpose.

You must use the right tool for the job.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • 1
    "You must be the right tool for the job." - no need to insult the guy :) – starskythehutch Oct 03 '11 at 11:55
  • +1 while it's certainly possible to use ASP.NET to handle some tasks like this (StackOverflow used to for a long time) for mission critical tasks it's not reliable enough. – Chris Marisic Oct 03 '11 at 12:30
  • Thats kind of lame (see my comments to my question). Of course many sites built will need threads to manage queues and memory. Why wouldn't asp.net allow me to write code for that in my asp.net project :( –  Oct 03 '11 at 12:33
  • "many sites" do it the right way and don't use ASP.NET for things it's not suited for. – John Saunders Oct 03 '11 at 15:15
1

Take a look at this post: How to migrate a .NET Windows Service application to Linux using mono? I hope it gets you in the right direction :)

Community
  • 1
  • 1
Kimtho6
  • 6,154
  • 9
  • 40
  • 56