Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple ManagerConnection Instances #5

Closed
diegort opened this issue Mar 10, 2015 · 11 comments
Closed

Multiple ManagerConnection Instances #5

diegort opened this issue Mar 10, 2015 · 11 comments
Labels
bug A bug in the project that needs to be fixed. question Most likely not a bug, but a user needing help understanding something.

Comments

@diegort
Copy link
Contributor

diegort commented Mar 10, 2015

Hi guys,
I'm trying make a service that is an interface to manage some autodialer with one thread per autodialer.

Each autodialer has a managerconnection to an Asterisk server, and some of then could be connected to the same Asterisk.

My problem is when I run more than one thread I always get TimeoutException sending QueueStatusAction to Asterisk via manager.

I've investigated for two weeks debugging the code but I've no idea what can happen

Thank you for your time and help

@skrusty
Copy link
Collaborator

skrusty commented Mar 10, 2015

Can you include a code snippet so I can see how you're calling SendAction please. Also, can you confirm you're running the latest version of AsterNET (nuget/release/src etc).

Also, just to confirm, this doesn't happen when you're running one manager connection?

Does it happen only when both connections are to the same asterisk box?

@diegort
Copy link
Contributor Author

diegort commented Mar 11, 2015

I've downloaded the code and I run from it. I have the lastest version there is in codeplex, but I compared with the version on GitHub and it seems the same.

I have an abstraction layer to hide useless details to top level software, but it only works with one instance of this.

This is an example of program logic:

    Dim lock as New Object 
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim threads As New List(Of Threading.Thread)
        Dim queues As New List(Of String)({"queue1", "queue2", "queue3", "queue4"})
        Dim tmp As Threading.Thread

        For Each c As String In colas
            tmp = New Threading.Thread(AddressOf runThread)
            threads.Add(tmp)
            tmp.Start(c)
        Next
    End Sub

    Private Sub runThread(ByVal q As String)
        Dim aux As Centralita
        aux = New Centralita()
        Dim queue As String = q
        Do
            SyncLock (lock)
                Console.WriteLine(String.Format("Queue: {0}; Agents: {1}", queue, aux.AgentsAvaibleSync(queue)))
            End SyncLock
            Threading.Thread.Sleep(5000)
        Loop
    End Sub

Centralita is the abstraction layer of AsterNET, and its job is connect to Asterisk, send apropiate actions and manage resultin events to provide just the data.

Here is fragment of code

    public int AgentsAvaibleSync(string cola)
        {
            int members = 0;
            lock (lockColas) { 
                string tmpAcc = DateTime.Now.Ticks.ToString();

                QueueStatusAction acc = new QueueStatusAction();
                acc.Queue = cola;
                acc.ActionId = tmpAcc;

                try
                {
                    //_manager.SendAction(acc);
                    ResponseEvents aux = _manager.SendEventGeneratingAction(acc);
                    ResponseEvent e;
                    for (int i = 0; i < aux.Events.Count; i++)
                    {
                        e = aux.Events[i];
                        if (typeof(QueueMemberEvent) == e.GetType())
                        {
                            if (((QueueMemberEvent)e).Status == 1)
                            members += 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    members = -1;
                }
            }
            return members;
        }

Also I have an asynchronous method to get the avaible agents, but I receive the same result.

It occurs always when I have more than an instance of Centralita, which is also more than an instance of ManagerConnection

What am I doing wrong?

@skrusty
Copy link
Collaborator

skrusty commented Mar 11, 2015

I tend to use SendAction() myself, so I may have to create a small test setup to see if I can replicate your problem.

Small points.

  1. You don't need to specify the ActionId
  2. The code on GitHub is more recent, but it's very minor changes (mostly in Asterisk version support)
  3. You can turn on tracing in AsterNET to get much more detailed information about exceptions (which i suspect is what's actually causing the Timeout to happen (hidden exception)). To turn on tracing, see: https://asternet.codeplex.com/discussions/572963

@diegort
Copy link
Contributor Author

diegort commented Mar 11, 2015

I usually use SenAction() to do the process async, but in this case, I wanted do it synchronoys to see the process more clearly.

If you want, I can send you my abstracion layer of AsterNET to simplify your work.

  1. I need to specify the ActionId to track which events are caused by my action, because I have other processes using AMI in the same Asterisk, though it isn't necessary in the code I showed you.
  2. When I compared both versions of the code, I saw those changes and I thought replace my version of AsterNET with the GitHub one.
  3. I'm going to try tracing in AsterNET as you told me. I hope it could give me a big help to solve my problem.

In others tests I done, I don't use these abstraction layer but I created the ManagerConnection object in the same class that I call AvaibleAgents() and using the correct EventHandlers. It works fine for all threads I want to run at the same time!
I don't undetstand anything

@skrusty
Copy link
Collaborator

skrusty commented Mar 11, 2015

OK, so it works fine without your abstraction layer? Turn on the tracing, and lets see what you get. It may flag up something we've not considered.

@skrusty skrusty added the question Most likely not a bug, but a user needing help understanding something. label Mar 11, 2015
@diegort
Copy link
Contributor Author

diegort commented Mar 11, 2015

Yes. If I merge all the code into a unique class, it works. I've turned on the tracing, but I don't see anything. Maybe I don't know how read this output

@skrusty
Copy link
Collaborator

skrusty commented Mar 11, 2015

It's quite verbose I know.

Are you seeing anything around the same time as the time-out? Feel free to put some in a gist and link here for me to look at if you want.

@diegort
Copy link
Contributor Author

diegort commented Mar 11, 2015

Here is my abstractionLayer https://gist.github.com/5841c7360aa8ba9dede7.git. I programmed it into the same project as AsterNET. I'm Spanish, so in the code may be some variable and function names that are in Spanish. If you don't understand something, feel free to ask me

@diegort
Copy link
Contributor Author

diegort commented Mar 13, 2015

I've taken a small step:
I've isolated my abstraction layer in a new proyect, and I'm using the nuget dll of AsterNET instead the code I had downloaded. And Voila! It works perfectly.
The disadvantage of this solution is that the DetermineVersion() method can't find the version of my Asterisk PBX. If I execute the 'core show version' command, I get this result: "Asterisk SVN-branch-11-r429270 built..."

To fix this I modified the Regular Expression that looks for the version, and now it looks like this:

public static Regex ASTERISK_VERSION = new Regex("^Asterisk\\s+\\D*([0-9]+.[0-9]+.[0-9]+|[1-9][0-9]-r[0-9]+).*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

If you could do this modification to the NuGet version of AsterNET, I would use it and all my problems will disappear.

Thanks for your work and your help

@skrusty
Copy link
Collaborator

skrusty commented Mar 13, 2015

Fancy forking, making the change and submitting a pull request?

Keeps contributions all nice and attributed.

Thanks,
Ben

@diegort
Copy link
Contributor Author

diegort commented Mar 13, 2015

I've followed your advice. Now, you must have a pull request on Common.cs

@skrusty skrusty mentioned this issue Mar 13, 2015
skrusty added a commit that referenced this issue Mar 13, 2015
@skrusty skrusty closed this as completed Mar 13, 2015
@skrusty skrusty added the bug A bug in the project that needs to be fixed. label Mar 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug in the project that needs to be fixed. question Most likely not a bug, but a user needing help understanding something.
Projects
None yet
Development

No branches or pull requests

2 participants