~aleteoryx/muditaos

ref: 34ee674f371c0588f1c47bb0ce6fdda517e2ace0 muditaos/module-services/service-appmgr/model/ActionsRegistry.cpp -rw-r--r-- 4.0 KiB
34ee674f — Mateusz Piesta [MOS-939] Apply target update scripts 2 years ago
                                                                                
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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ActionsRegistry.hpp"

#include <log/log.hpp>
#include <magic_enum.hpp>

#include <cassert>
#include <stdexcept>

namespace app::manager
{
    namespace
    {
        constexpr auto ExpirationTimeout = std::chrono::seconds{30};
    } // namespace

    ActionEntry::ActionEntry(actions::ActionId action, actions::ActionParamsPtr &&params, Timestamp creationTime)
        : actionId{action}, params{std::move(params)}, createdAt{creationTime}
    {}

    void ActionEntry::setTargetApplication(std::string targetApplication) noexcept
    {
        target = std::move(targetApplication);
    }

    auto ActionEntry::isExpired() const noexcept -> bool
    {
        const auto diff = Clock::now() - createdAt;
        return diff > ExpirationTimeout;
    }

    bool operator==(const ActionEntry &lhs, const ActionEntry &rhs) noexcept
    {
        return lhs.actionId == rhs.actionId && lhs.createdAt == rhs.createdAt && lhs.params == rhs.params;
    }

    bool operator!=(const ActionEntry &lhs, const ActionEntry &rhs) noexcept
    {
        return !operator==(lhs, rhs);
    }

    ActionsRegistry::ActionsRegistry(OnNextActionReadyCallback &&callback) : nextActionReady{std::move(callback)}
    {
        if (!nextActionReady) {
            throw std::invalid_argument{"No OnNextActionReadyCallback passed."};
        }
    }

    void ActionsRegistry::enqueue(ActionEntry &&action)
    {
        LOG_INFO("Enqueue action %s", magic_enum::enum_name(action.actionId).data());

        addAction(std::move(action));
        process();
    }

    void ActionsRegistry::process()
    {
        if (!isCurrentlyProcessing()) {
            notifyAboutNextAction();
        }
    }

    void ActionsRegistry::addAction(ActionEntry &&action)
    {
        actions.push_back(std::move(action));
    }

    auto ActionsRegistry::isCurrentlyProcessing() const noexcept -> bool
    {
        return hasPendingAction();
    }

    void ActionsRegistry::notifyAboutNextAction()
    {
        for (auto it = actions.begin(); it != actions.end();) {
            auto &action = *it;
            switch (const auto status = nextActionReady(action); status) {
            case ActionProcessStatus::Accepted: {
                LOG_INFO(
                    "Pending action %s to %s", magic_enum::enum_name(action.actionId).data(), action.target.c_str());
                actionInProgress = &action;
                return;
            }
            case ActionProcessStatus::Dropped: {
                // Drop the action, so it'll not be processed again.
                it = actions.erase(it);
                break;
            }
            case ActionProcessStatus::Skipped:
                // Skip the action and check the next one.
                ++it;
                break;
            }
        }
    }

    void ActionsRegistry::finished()
    {
        assert(isCurrentlyProcessing());

        LOG_INFO("Finished action %s on %s",
                 magic_enum::enum_name(actionInProgress->actionId).data(),
                 actionInProgress->target.c_str());
        if (const auto it = std::find(actions.begin(), actions.end(), *actionInProgress); it != actions.end()) {
            actions.erase(it);
        }

        actionInProgress = nullptr;
        onFinished();
    }

    void ActionsRegistry::onFinished()
    {
        removeExpiredActions();
        if (!actions.empty()) {
            notifyAboutNextAction();
        }
    }

    void ActionsRegistry::removeExpiredActions()
    {
        actions.erase(
            std::remove_if(actions.begin(), actions.end(), [](const auto &action) { return action.isExpired(); }),
            actions.end());
    }

    auto ActionsRegistry::hasPendingAction() const noexcept -> bool
    {
        return actionInProgress != nullptr;
    }

    auto ActionsRegistry::getPendingAction() noexcept -> ActionEntry *
    {
        return actionInProgress;
    }
} // namespace app::manager