Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / JobQueue.cs @ b9f5b594

History | View | Annotate | Download (3.5 kB)

1 d15e99b4 Panagiotis Kanavos
// -----------------------------------------------------------------------
2 b9f5b594 Panagiotis Kanavos
// <copyright file="JobQueue.cs" company="GRNet">
3 b9f5b594 Panagiotis Kanavos
// Copyright 2011 GRNET S.A. All rights reserved.
4 b9f5b594 Panagiotis Kanavos
// 
5 b9f5b594 Panagiotis Kanavos
// Redistribution and use in source and binary forms, with or
6 b9f5b594 Panagiotis Kanavos
// without modification, are permitted provided that the following
7 b9f5b594 Panagiotis Kanavos
// conditions are met:
8 b9f5b594 Panagiotis Kanavos
// 
9 b9f5b594 Panagiotis Kanavos
//   1. Redistributions of source code must retain the above
10 b9f5b594 Panagiotis Kanavos
//      copyright notice, this list of conditions and the following
11 b9f5b594 Panagiotis Kanavos
//      disclaimer.
12 b9f5b594 Panagiotis Kanavos
// 
13 b9f5b594 Panagiotis Kanavos
//   2. Redistributions in binary form must reproduce the above
14 b9f5b594 Panagiotis Kanavos
//      copyright notice, this list of conditions and the following
15 b9f5b594 Panagiotis Kanavos
//      disclaimer in the documentation and/or other materials
16 b9f5b594 Panagiotis Kanavos
//      provided with the distribution.
17 b9f5b594 Panagiotis Kanavos
// 
18 b9f5b594 Panagiotis Kanavos
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 b9f5b594 Panagiotis Kanavos
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 b9f5b594 Panagiotis Kanavos
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 b9f5b594 Panagiotis Kanavos
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 b9f5b594 Panagiotis Kanavos
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 b9f5b594 Panagiotis Kanavos
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 b9f5b594 Panagiotis Kanavos
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 b9f5b594 Panagiotis Kanavos
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 b9f5b594 Panagiotis Kanavos
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 b9f5b594 Panagiotis Kanavos
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 b9f5b594 Panagiotis Kanavos
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 b9f5b594 Panagiotis Kanavos
// POSSIBILITY OF SUCH DAMAGE.
30 b9f5b594 Panagiotis Kanavos
// 
31 b9f5b594 Panagiotis Kanavos
// The views and conclusions contained in the software and
32 b9f5b594 Panagiotis Kanavos
// documentation are those of the authors and should not be
33 b9f5b594 Panagiotis Kanavos
// interpreted as representing official policies, either expressed
34 b9f5b594 Panagiotis Kanavos
// or implied, of GRNET S.A.
35 d15e99b4 Panagiotis Kanavos
// </copyright>
36 d15e99b4 Panagiotis Kanavos
// -----------------------------------------------------------------------
37 d15e99b4 Panagiotis Kanavos
38 d15e99b4 Panagiotis Kanavos
using System.Collections.Concurrent;
39 d15e99b4 Panagiotis Kanavos
using System.Threading;
40 d15e99b4 Panagiotis Kanavos
using System.Threading.Tasks;
41 d15e99b4 Panagiotis Kanavos
42 d15e99b4 Panagiotis Kanavos
namespace Pithos.Core
43 d15e99b4 Panagiotis Kanavos
{
44 d15e99b4 Panagiotis Kanavos
    using System;
45 d15e99b4 Panagiotis Kanavos
    using System.Collections.Generic;
46 d15e99b4 Panagiotis Kanavos
    using System.Linq;
47 d15e99b4 Panagiotis Kanavos
    using System.Text;
48 d15e99b4 Panagiotis Kanavos
49 d15e99b4 Panagiotis Kanavos
    /// <summary>
50 d15e99b4 Panagiotis Kanavos
    /// TODO: Update summary.
51 d15e99b4 Panagiotis Kanavos
    /// </summary>
52 d15e99b4 Panagiotis Kanavos
    public class JobQueue
53 d15e99b4 Panagiotis Kanavos
    {
54 d15e99b4 Panagiotis Kanavos
        private readonly BlockingCollection<Action> _statusUpdateQueue = new BlockingCollection<Action>();
55 d15e99b4 Panagiotis Kanavos
        private CancellationToken _cancellationToken;
56 d15e99b4 Panagiotis Kanavos
        
57 d15e99b4 Panagiotis Kanavos
58 d15e99b4 Panagiotis Kanavos
        public void Start(CancellationToken token)
59 d15e99b4 Panagiotis Kanavos
        {
60 d15e99b4 Panagiotis Kanavos
            _cancellationToken = token;
61 d15e99b4 Panagiotis Kanavos
            Task.Factory.StartNew(ProcessUpdates, _cancellationToken);
62 d15e99b4 Panagiotis Kanavos
        }
63 d15e99b4 Panagiotis Kanavos
64 d15e99b4 Panagiotis Kanavos
        private void ProcessUpdates()
65 d15e99b4 Panagiotis Kanavos
        {
66 d15e99b4 Panagiotis Kanavos
            foreach (var action in _statusUpdateQueue.GetConsumingEnumerable())
67 d15e99b4 Panagiotis Kanavos
            {
68 d15e99b4 Panagiotis Kanavos
                action();
69 d15e99b4 Panagiotis Kanavos
            }
70 d15e99b4 Panagiotis Kanavos
        }
71 d15e99b4 Panagiotis Kanavos
72 d15e99b4 Panagiotis Kanavos
        public void Add(Action action)
73 d15e99b4 Panagiotis Kanavos
        {
74 d15e99b4 Panagiotis Kanavos
            _statusUpdateQueue.Add(action);
75 d15e99b4 Panagiotis Kanavos
        }
76 d15e99b4 Panagiotis Kanavos
77 d15e99b4 Panagiotis Kanavos
        public void Stop()
78 d15e99b4 Panagiotis Kanavos
        {
79 d15e99b4 Panagiotis Kanavos
            _statusUpdateQueue.CompleteAdding();
80 d15e99b4 Panagiotis Kanavos
        }
81 d15e99b4 Panagiotis Kanavos
       
82 9c4346c9 Panagiotis Kanavos
    }   
83 9c4346c9 Panagiotis Kanavos
84 9c4346c9 Panagiotis Kanavos
    public class JobAgent:Agent<Action>
85 9c4346c9 Panagiotis Kanavos
    {
86 9c4346c9 Panagiotis Kanavos
        protected JobAgent(Action<Agent<Action>>  action)
87 9c4346c9 Panagiotis Kanavos
            :base(action)
88 9c4346c9 Panagiotis Kanavos
        {
89 9c4346c9 Panagiotis Kanavos
            
90 9c4346c9 Panagiotis Kanavos
        }
91 9c4346c9 Panagiotis Kanavos
92 9c4346c9 Panagiotis Kanavos
        public static JobAgent Create()
93 9c4346c9 Panagiotis Kanavos
        {
94 9c4346c9 Panagiotis Kanavos
            return (JobAgent)Start(queue =>
95 9c4346c9 Panagiotis Kanavos
            {
96 9c4346c9 Panagiotis Kanavos
                Action loop = null;
97 9c4346c9 Panagiotis Kanavos
                loop = () =>
98 9c4346c9 Panagiotis Kanavos
                {
99 9c4346c9 Panagiotis Kanavos
                    var job = queue.Receive();
100 9c4346c9 Panagiotis Kanavos
                    job.ContinueWith(t =>
101 9c4346c9 Panagiotis Kanavos
                    {
102 24477d85 Panagiotis Kanavos
                        t.IgnoreExceptions();
103 9c4346c9 Panagiotis Kanavos
                        var action = job.Result;
104 9c4346c9 Panagiotis Kanavos
                        action();
105 9c4346c9 Panagiotis Kanavos
                        queue.DoAsync(loop);
106 9c4346c9 Panagiotis Kanavos
                    });
107 9c4346c9 Panagiotis Kanavos
                };
108 9c4346c9 Panagiotis Kanavos
                loop();
109 9c4346c9 Panagiotis Kanavos
            });
110 9c4346c9 Panagiotis Kanavos
        }
111 9c4346c9 Panagiotis Kanavos
112 d15e99b4 Panagiotis Kanavos
    }
113 d15e99b4 Panagiotis Kanavos
}