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

jobqueue.cpp « libsync « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72b9f3db00a6300c3f51372a550c762a55f2e685 (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
/*
 * Copyright (C) by Hannah von Reth <hannah.vonreth@owncloud.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License
 * for more details.
 */

#include "jobqueue.h"

#include "abstractnetworkjob.h"
#include "account.h"

#include <QLoggingCategory>

namespace OCC {

Q_LOGGING_CATEGORY(lcJobQUeue, "sync.networkjob.jobqueue", QtDebugMsg)

JobQueue::JobQueue(Account *account)
    : _account(account)
{
}

void JobQueue::block()
{
    _blocked++;
    qCDebug(lcJobQUeue) << "block:" << _blocked << _account->displayName();
}

void JobQueue::unblock()
{
    if (!isBlocked()) {
        return;
    }
    _blocked--;
    qCDebug(lcJobQUeue) << "unblock:" << _blocked << _account->displayName();
    if (_blocked == 0) {
        auto tmp = std::move(_jobs);
        for (auto job : tmp) {
            if (job) {
                qCDebug(lcJobQUeue) << "Retry" << job;
                job->retry();
            }
        }
    }
}

bool JobQueue::isBlocked() const
{
    return _blocked != 0;
}

bool JobQueue::retry(AbstractNetworkJob *job)
{
    if (!job->needsRetry()) {
        return false;
    }
    if (_blocked) {
        qCDebug(lcJobQUeue) << "Retry queued" << job;
        _jobs.push_back(job);
    } else {
        qCDebug(lcJobQUeue) << "Direct retry" << job;
        job->retry();
    }
    return true;
}

bool JobQueue::enqueue(AbstractNetworkJob *job)
{
    if (!_blocked) {
        return false;
    }
    qCDebug(lcJobQUeue) << "Queue" << job;
    _jobs.push_back(job);
    return true;
}

void JobQueue::clear()
{
    _blocked = 0;
    auto tmp = std::move(_jobs);
    for (auto job : tmp) {
        if (job) {
            qCDebug(lcJobQUeue) << "Abort" << job;
            job->abort();
        }
    }
}

size_t JobQueue::size() const
{
    return _jobs.size();
}

JobQueueGuard::JobQueueGuard(JobQueue *queue)
    : _queue(queue)
{
}

JobQueueGuard::~JobQueueGuard()
{
    unblock();
}

bool JobQueueGuard::block()
{
    if (!_blocked) {
        _blocked = true;
        _queue->block();
        return true;
    }
    return false;
}

bool JobQueueGuard::unblock()
{
    if (_blocked) {
        _blocked = false;
        _queue->unblock();
        return true;
    }
    return false;
}

bool JobQueueGuard::clear()
{
    if (_blocked) {
        _blocked = false;
        _queue->clear();
        return true;
    }
    return false;
}

JobQueue *JobQueueGuard::queue() const
{
    return _queue;
}
}