-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH-46]Support persistent queue for storops
With the support of this commit, user can: * add storops operations to a queue, and they can be executed periodically till operation executes correctly. * unfinished items in the queue can be restored automatically after restart.
- Loading branch information
1 parent
51c7121
commit 4082f45
Showing
4 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# coding=utf-8 | ||
# Copyright (c) 2016 EMC Corporation. | ||
# All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import logging | ||
import pqueue | ||
import threading | ||
import time | ||
|
||
|
||
__author__ = 'Peter Wang' | ||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class PQueue(object): | ||
DEFAULT_INTERVAL = 300 | ||
|
||
def __init__(self, path, interval=None): | ||
self.path = path | ||
self._interval = interval if interval else self.DEFAULT_INTERVAL | ||
|
||
def init_queue(self, start=False): | ||
self._q = pqueue.Queue(self.path) | ||
self.start = start | ||
if start: | ||
self._run() | ||
return self | ||
|
||
def put(self, item): | ||
return self._q.put(item) | ||
|
||
def get(self): | ||
return self._q.get() | ||
|
||
def start(self): | ||
if not self.start: | ||
self._run() | ||
self.start = True | ||
else: | ||
LOG.info("PQueue[{}] had already started.".format(self.path)) | ||
|
||
def stop(self): | ||
self._interval = 0 | ||
|
||
def set_interval(self, interval): | ||
self._interval = interval | ||
|
||
def _run(self): | ||
self._t = threading.Thread(target=self._run_tasks) | ||
self._t.start() | ||
|
||
def _run_tasks(self): | ||
while True and self._interval > 0: | ||
LOG.debug("Running periodical check.") | ||
try: | ||
data = self._q.get_nowait() | ||
except pqueue.Empty: | ||
LOG.debug("Queue is empty now.") | ||
data = None | ||
if data and type(data) is dict: | ||
method = getattr(data['object'], data['method'], None) | ||
try: | ||
method(data['param']) | ||
except Exception as ex: | ||
LOG.debug("Failed to execute {}: {}, this message can be " | ||
"safely ignored.".format(method.__name__, | ||
ex.message)) | ||
# Re-enqueue since failed to execute | ||
self._q.put(data) | ||
self._q.task_done() | ||
time.sleep(self._interval) | ||
LOG.info("{} with path {} has been " | ||
"stopped.".format(self.__class__.__name__, self._q.path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
from __future__ import unicode_literals | ||
|
||
import logging | ||
from multiprocessing.pool import ThreadPool | ||
from time import sleep | ||
from unittest import TestCase | ||
|
||
from hamcrest import assert_that, equal_to, close_to, only_contains, raises, \ | ||
contains_string, has_items | ||
|
||
from storops.exception import EnumValueNotFoundError | ||
from storops.lib.common import Dict, Enum, WeightedAverage, synchronized, \ | ||
text_var, int_var, enum_var, yes_no_var, JsonPrinter, get_lock_file, \ | ||
EnumList, round_3 | ||
from storops.lib import queue_tasks | ||
from test.vnx.cli_mock import patch_cli, t_vnx, t_cli | ||
|
||
|
||
class TestPQueue(TestCase): | ||
|
||
def _get_queue(self, path): | ||
return queue_tasks.PQueue(path) | ||
|
||
def test_init_queue(self): | ||
q = self._get_queue('/tmp/TestPQueue') | ||
pq = q.init_queue() | ||
assert_that(pq.path, '/tmp/TestPQueue') | ||
|
||
def test_put(self): | ||
q = self._get_queue('/tmp/test_put') | ||
q.init_queue() | ||
fake_vnx = t_vnx() | ||
item = {'object': fake_vnx, 'method': 'delete', 'param': 'lun-1'} | ||
q.put(item) | ||
|
||
def test_get(self): | ||
q = self._get_queue('/tmp/test_get') | ||
q.init_queue() | ||
fake_vnx = t_vnx() | ||
item = {'object': fake_vnx, 'method': 'delete', 'param': 'lun-1'} | ||
q.put(item) | ||
|
||
pickled_item = q.get() | ||
assert_that(pickled_item['object']._ip, fake_vnx._ip) | ||
assert_that(pickled_item['method'], 'delete') | ||
assert_that(pickled_item['param'], 'lun-1') | ||
|
||
def test_run_tasks(self): | ||
q = self._get_queue('/tmp/test_run_tasks') | ||
q.set_interval(0.01) | ||
q.init_queue(start=True) | ||
q.stop() |