Timers not running in Windows Service Application - windows-services

I had an application which reads some data from a web database and updates the local database - on a periodic basis. Say every 5 mins.
This was in the form of an interactive windows application. But since this application has to run continuously, we decided to make a Windows Service Application.
We were using timers in the application, which were set to an interval of 5 mins, and in every tick of the timer, we were checking for the web updates.
We have carried the same thing in the service. But it seems the timers are not running now. Following are the few methods from the service:
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
'connect to sap business one
If Connect("rlap1", "sa", "dracula", "UnimaxNew", "manager", "manager") = False Then
End
SyncEvents.WriteEntry("Unable to connect to SAP Business One, " & oCompany.GetLastErrorDescription, EventLogEntryType.Error)
Else
SyncEvents.WriteEntry("Connected to SAP Business One.", EventLogEntryType.Information)
End If
'start the timers
WebTimer.Start()
SapTimer.Start()
DataTimer.Start()
SyncEvents.WriteEntry("Synchronization process started.")
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
oCompany.Disconnect()
SyncEvents.WriteEntry("Disconnected from SAP Business One.", EventLogEntryType.Information)
'stop the timers
WebTimer.Stop()
SapTimer.Stop()
DataTimer.Stop()
SyncEvents.WriteEntry("Synchronization process stopped.")
End Sub
Private Sub WebTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebTimer.Tick
SyncEvents.WriteEntry("Checking for new orders.")
CheckOrder()
End Sub
Private Sub SapTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SapTimer.Tick
SyncEvents.WriteEntry("Checking for new deliveries.")
CheckDelivery()
End Sub
Private Sub DataTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataTimer.Tick
SyncEvents.WriteEntry("Checking for stock updates.")
CheckStock()
End Sub
The messages are log correctly in the event logs - when we start or stop the service. But there are no messages from timers function. Also I've tried debugging the service - by using Attach To Process.. in Visual Studio. But even than the code never breaked on any of the tick functions.
Can't we use the timers in the service? If no, than what is the other way. If yes, what could be wrong here?
Thanks
Rahul Jain

There's nothing that prevents timers from working in a Windows service. It sounds to me like the timer is not configured for auto-reset, but without knowing what timer you're using (System.Timers.Timer? System.Threading.Timer? Other?) and how it's configured, it's impossible to know for sure.
Regarding debugging the service, put the following code "programmatic breakpoint" in your OnStart() method:
System.Diagnostics.Debugger.Break();
When you start the service, you be prompted to enter a debug session, and the execution will break at this line. You can debug normally from there.

Related

anylogic agent communication and message sending

In my model, I have some agents;
"Demand" agent,
"EnergyProducer1" agent
"EnergyProducer2" agent.
When my hourly energy demands are created in the Main agent with a function, the priority for satisfying this demand is belongs to "EnergyProducer1" agent. In this agent, I have a function that calculate energy production based on some situtations. The some part of the inside of this function is following;
**" if (statechartA.isStateActive(Operating.busy)) && ( main.heatLoadDemandPerHour >= heatPowerNominal) {
producedHeatPower = heatPowerNominal;
naturalGasConsumptionA = naturalGasConsumptionNominal;
send("boilerWorking",boiler);
} else ..... "**
Here my question is related to 4th line of the code. If my agent1 fails to satisfy the hourly demand, I have to say agent2 that " to satisfy rest of demand". If I send this message to agent2, its statechart will be active and the function of agent2 will be working. My question is that this all situations will be realized at the same hour ??? İf it is not, is accessing variables and parameters of other agent2 more appropiaote way???
I hope I could explain my problem.
thanks for your help in advance...
**Edited question...
As a general comment on your question, within AnyLogic environment sending messages is alway preferable to directly accessing variable and parameters of another agent.
Specifically in the example presented the send() function will schedule message delivery the next instance after the completion of the current function.
Update: A message in AnyLogic can be any Java class. Sending strings such as "boilerWorking" used in the example is good for general control, however if more information needs to be shared (such as a double value) then it is good practice to create a new Java class (let's call is ModelMessage and follow these instructions) with at least two properties msgStr and msgVal. With this new class sending a message changes from this:
...
send("boilerWorking", boiler);
...
to this:
...
send(new ModelMessage("boilerWorking",42.0), boiler);
...
and firing transitions in the statechart has to be changed to use if expression is true with expression being msg.msgString == "boilerWorking".
More information about Agent communication is available here.

How to test if msg was send to GenServer process

I'm running GenServer as a background job which is rescheduled each interval by Process.send_after(self(), :work, #interval).
This job is started by Supervisor when Application starts.
It's working perfectly, but now I want to test if my GenServer module is really spawning new process each interval.
How can I test it?
EDIT
I found that :sys.get_status(pid) can be use to fetch some data about process, but I would really like to use something like receive do ... end
EDIT 2
handle_info/2 function:
#impl true
def handle_info(:work, state) do
do_smt()
schedule_worker()
{:noreply, state}
end
schedule_worker/0 function:
defp schedule_worker do
Process.send_after(self(), :work, #interval)
end
There's something missing in your message. From what you have posted we can understand that every #interval milliseconds a :work message is sent. You are not telling us what the handle_info/2 is supposed to do when the message is dispatched.
Once this is defined, you can definitely write a test to assert that a message has been received by using the assert_received assertion.
I would test do_smt() by using Mock library and writing a test that makes as assertion like the following:
with_mock(MyModule, [do_stm_else: fn -> :ok]) do
do_smt()
assert_called MyModule.do_stm_else()
end
In this way, you have called the function that the task should execute, so you can assume that the task creation is being called.
If you want to let the do_stm_else function communicate with your test (in this scenario it looks a bit overengineered) you should:
get the pid of the test by calling self()
Pass the pid to the mock function to get it used
use assert_receive to verify that the communication has occurred
pid = self()
with_mock(MyModule, [do_stm_else: fn ->
Process.send(pid, :msg)
]) do
do_smt()
assert_called MyModule.do_stm_else()
end
assert_receive(:msg)
Please note that I had no time to check this, you should spend a bit to investigate.

What is wrong with my gen_statem fsm implementation?

I am implementing a gen_fsm using the gen_statem module and when i am trying to check its state as a handle_event_function i get the following error:
> ** exception error: {function_clause,
> {gen_statem,call,[{ok,<0.139.0>},state,0]}}
> in function gen:do_for_proc/2
> called as gen:do_for_proc({ok,<0.139.0>},#Fun<gen.0.9801092>)
> in call from gen_statem:'-call_clean/4-fun-0-'/5 (gen_statem.erl, line 637) 25> c("C:/Erlang/Genserv/fsm.erl").
Below is my code which is separated into :
-mandatory methods for the fsm to work
-api that the client can use (state change,get the state,start)
- generic handlers for when client demands something related to state
-state implementations
Module
-module(fsm).
-record(state,{
current="None",
intvCount=0,
jobCount=0
}).
-export([init/1,terminate/3,callback_mode/0,code_change/4]).
-export([state/1,start/0,hire/2,fire/2,interview/2]).
-export([sitting_home/3,interviewing/3,working/3]).
-behaviour(gen_statem).
%API
start()->
gen_statem:start_link(?MODULE,[],[]).
state(PID)->
gen_statem:call(PID,state,0).
hire(PID,Company)->
gen_statem:call(PID,{hire,Company},0).
fire(PID,Company)->
gen_statem:call(PID,{fire,Company},0).
interview(PID,Company)->
gen_state:call(PID,{intv,Company},0).
%mandatory
code_change(V,State,Data,Extra)->{ok,State,Data}.
callback_mode() ->
[state_functions,handle_event_function].
init([])->
{ok,sitting_home,#state{current="None",jobCount=0,intvCount=0}}.
terminate(Reasom,State,Data)->
void.
% Generic handlers
handle_event({call,From},state,State)->
{keep_state,State,[{reply,From,State}]};
handle_event(_,_,State)->
{keep_state,State}.
% State implementations
sitting_home({call,From},{intv,Company},State=#state{intvCount=C})->
{next_state,interviewing,State#state{intvCount=C+1},[{reply,From,"Interviewing by:"++Company}]};
sitting_home(EventType,Event,State)->
handle_event(EventType,Event,State).
interviewing({call,From},{rejected,Company},State)->
{next_state,sitting_home,State,[{reply,From,"rejected by:"++Company}]};
interviewing({call,From},{accepted,Company},State=#state{jobCount=J})->
{next_state,
working,
State#state{jobCount=J+1,current=Company},
[{reply,From,"accepted offer from:"++Company}]
};
interviewing(EventType,Event,State)->
handle_event(EventType,Event,State).
working({call,From},{fire,Company},State=#state{current=C})->
{next_state,working,State#state{current="None"},[{reply,From,"Got fired from"++Company}]};
working(EventType,Event,State)->
handle_event(EventType,Event,State).
Usage
FSM_PID=fsm:start().
fsm:state(PID). //crashes when demanding state
fsm:hire(PID,"Some Company").
fsm:start() returns the tuple {ok,Pid}. You cannot use directly the return of the function in your next call. Instead you must do something like:
{ok,P} = fsm:start().
fsm:state(P).
Then you will experience other troubles, the first one is that you specify a timeout of 0, so you will get a timeout error. You will have to change this, and continue debugging :o)
[edit]
You don't need to specify the Pid, it is done in background by the gen_statem code, the gen_statem:call function is executed in the caller context so it still has access to the caller Pid.
In fact it is a little more complex, depending if you specify a timeout or not, the gen_statem:call/3 will spawn a process to call the gen:call/4 function or will directly call it, thus the Pid sent to the state machine will be either the spawned function's one or the caller's one.
The gen:call/4 also add a Reference in the message to the state machine to "sign" the answer and guaranty that an incoming message from any other part of the application will not be interpreted as the answer.
This is a general pattern in erlang (and programming) to hide as much as possible the system mechanism in interface function. Just as you do in the function state/1

Windows service not executing after first run

I have create a windows service in VS 2010. I install it and also run it at the same time and set startup type to Automatic . I see it running fine through EventViewer and is successfully completed.
But after that i done see EventViewer showing anything, even if the work is doen it still should check DB and skip as all rows done.
So what is the issue ?
DO i need to make it an infinite loop in the service to keep it running?
Something like
While (ROWs in DB ! = null) ?
Because it does not seem it is working like task scheduler!
Yes, you need to do a loop with the possibility to break it again. Example service (VB.NET):
Public Class MyService
Protected Property IsRunning As Boolean = False
Protected Sub OnStart(args() As String)
IsRunning = True
' make the loop function run asynchronously
Dim t As New System.Threading.Thread(AddressOf MyLoopFunction)
t.Start()
End Sub
Protected Sub MyLoopFunction
While IsRunning
' here comes your code ...
' sleep for a second for better CPU freedom
System.Threading.Thread.Sleep(1000)
End While
End Sub
Protected Sub OnStop()
IsRunning = False
End Sub
End Class

how to run scheduler utility in web application

I have the web application in visual studio web express and having db in sql server express.
I want to perform insert 100 records on 5:00 PM daily.web application is developed in asp.net MVC and vb.net. and deployed on server machine with IIS 7.5. what logic i should follow?
For me i'm using this approach and it's good till now :)
I've enum with Tasks to do and the time for the task to restart and this time in seconds like this:
public enum ScheduledTasks
{
CleanGameRequests = 120,
AnotherTask = 30,
}
Then i start all my tasks in the Application_Start to ensure that the task will execute while my application is run
protected void Application_Start()
{
...............
// Add the tasks on Application starts
AddTask(ScheduledTasks.CleanGameRequests);
AddTask(ScheduledTasks.AnotherTask);
}
OK now here is the trick :)
in the AddTask method i just add new empty item to cache and set the AbsoluteExpiration for it according to the task time and the call the suitable method for this task.
Actually my i couldn't explain the idea very clear but here is the code:
private static CacheItemRemovedCallback _onCacheRemove;
private void AddTask(ScheduledTasks task)
{
// Add my `CacheItemRemoved` method to be called on cache removed
_onCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
// Add new key to the cache with the name of this task
// and Expiration time acccordin to the task
HttpRuntime.Cache.Insert(task.ToString(), (int)task, null,
DateTime.Now.AddSeconds((int)task), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, _onCacheRemove);
}
Then all I've to do is to select suitable method for each task in the CacheItemRemoved method :
public void CacheItemRemoved(string key, object time, CacheItemRemovedReason r)
{
//Get Task enum object
var task = (ScheduledTasks)Enum.Parse(typeof(ScheduledTasks), key);
// Select the suitable method to depending on the Task Enum object
switch (task)
{
case ScheduledTasks.CleanGameRequests:
GameRequest.CleanUp();
break;
case ScheduledTasks.AnotherTask:
Service.AnotherTask();
break;
}
// Don't forget to re-add the task to the cache to do it again and again
AddTask(task);
}
Last thing remain for your case is to check the time if it's 5:00 PM and i advice you to put this check in your Service class.
Hope this helped you :)
Since you are using Sql server express edition you can't create scheduled jobs in sql side. But you can try other options like.
Quartz.Net
Service Broker approach
Windows services (If your hosting provider allows)

Resources