Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Scheduler.cs « GUI « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 08f4ffed2a3bf3081909d055b01c7e8ec94b47ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#region Disclaimer / License
// Copyright (C) 2009, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Duplicati.Datamodel;
using System.Data.LightDatamodel;
using Duplicati.Library.Core;

namespace Duplicati.GUI
{
    public class Scheduler
    {
        private Thread m_thread;
        private IDataFetcherCached m_connection;
        private volatile bool m_terminate;
        private WorkerThread<IDuplicityTask> m_worker;
        private AutoResetEvent m_event;
        private object m_lock = new object();
        private object m_datalock;

        public event EventHandler NewSchedule;

        private Schedule[] m_schedule;

        public Scheduler(IDataFetcherCached connection, WorkerThread<IDuplicityTask> worker, object datalock)
        {
            m_datalock = datalock;
            m_connection = connection;
            m_thread = new Thread(new ThreadStart(Runner));
            m_worker = worker;
            m_schedule = new Schedule[0];
            m_terminate = false;
            m_event = new AutoResetEvent(false);
            m_thread.IsBackground = true;
            m_thread.Start();
        }

        public void Reschedule()
        {
            m_event.Set();
        }

        public List<Schedule> Schedule 
        { 
            get 
            {
                lock (m_lock)
                    return new List<Schedule>(m_schedule);
            } 
        }

        /// <summary>
        /// Terminates the thread. Any items still in queue will be removed
        /// </summary>
        /// <param name="wait">True if the call should block until the thread has exited, false otherwise</param>
        public void Terminate(bool wait)
        {
            m_terminate = true;
            m_event.Set();

            if (wait)
                m_thread.Join();
        }

        private void Runner()
        {
            while (!m_terminate)
            {
                List<Schedule> reps = new List<Schedule>();
                List<Schedule> tmp;
                lock (m_datalock)
                    tmp = new List<Schedule>(m_connection.GetObjects<Schedule>());

                foreach (Schedule sc in tmp)
                {
                    if (!string.IsNullOrEmpty(sc.Repeat))
                    {
                        DateTime start = sc.When;

                        if (start <= DateTime.Now)
                        {
                            m_worker.AddTask(new IncrementalBackupTask(sc));

                            int i = 0;
                            while (start <= DateTime.Now && i++ < 50000)
                                try
                                {
                                    start = Timeparser.ParseTimeInterval(sc.Repeat, start);
                                }
                                catch
                                {
                                    continue;
                                }

                            if (start < DateTime.Now)
                                continue;
                        }

                        reps.Add(sc);
                        sc.When = start;
                    }
                }

                System.Data.LightDatamodel.QueryModel.Operation op = System.Data.LightDatamodel.QueryModel.Parser.ParseQuery("ORDER BY When ASC");

                lock(m_lock)
                    m_schedule = op.EvaluateList<Schedule>(reps).ToArray();

                if (NewSchedule != null)
                    NewSchedule(this, null);

                int waittime = 0;

                if (m_schedule.Length > 0)
                {
                    TimeSpan nextrun = m_schedule[0].When - DateTime.Now;
                    if (nextrun.TotalMilliseconds < 0)
                        continue;

                    /*lock (m_datalock)
                        m_connection.CommitAll();*/

                    waittime = (int)Math.Min(nextrun.TotalMilliseconds, 60 * 1000 * 5);
                }
                else
                {
                    //No tasks, check back later
                    waittime = 60 * 1000;
                }

                //Waiting on the event, enables a wakeup call from termination
                // never use waittime = 0
                m_event.WaitOne(Math.Max(100, waittime), true);
            }
        }
    }
}