csctracker-queue-scheduler 24.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
File without changes
@@ -0,0 +1,25 @@
1
+ class BaseDTO:
2
+ def to_dict(self):
3
+ result = {}
4
+ for key, value in self.__dict__.items():
5
+ if value is None or (isinstance(value, list) and not value): # Ignore None and empty lists
6
+ continue
7
+ if callable(value): # Ignore functions/methods
8
+ continue
9
+ if hasattr(value, 'to_dict'): # If value has to_dict method, call it
10
+ result[key] = value.to_dict()
11
+ elif isinstance(value, list): # If value is a list, call to_dict on its items if possible
12
+ result[key] = [item.to_dict() if hasattr(item, 'to_dict') else item for item in value]
13
+ else: # Otherwise, just assign value
14
+ result[key] = value
15
+ return result
16
+
17
+ def update(self, param):
18
+ if isinstance(param, dict):
19
+ attr_dict = param
20
+ else:
21
+ self.data: list = [param]
22
+ return
23
+
24
+ for key, value in attr_dict.items():
25
+ setattr(self, key, value)
File without changes
@@ -0,0 +1,10 @@
1
+ from enum import Enum
2
+
3
+
4
+ class TimeUnit(Enum):
5
+ SECONDS = 'seconds'
6
+ MINUTES = 'minutes'
7
+ HOURS = 'hours'
8
+ DAYS = 'days'
9
+ WEEKS = 'weeks'
10
+ DAILY = 'daily'
@@ -0,0 +1,8 @@
1
+ from csctracker_queue_scheduler.models.base_dto import BaseDTO
2
+
3
+
4
+ class GenericDataDTO(BaseDTO):
5
+ msg: str
6
+
7
+ def __init__(self, msg: str = None):
8
+ self.msg = msg
@@ -0,0 +1,3 @@
1
+ from csctracker_queue_scheduler.services.queue_service import QueueService
2
+
3
+ queue_service = QueueService()
@@ -0,0 +1,82 @@
1
+ import logging
2
+ import queue
3
+ import threading
4
+ import time
5
+ from queue import Queue
6
+
7
+ from csctracker_queue_scheduler.models.generic_data_dto import GenericDataDTO
8
+ from csctracker_queue_scheduler.utils import Utils
9
+
10
+
11
+ class QueueService:
12
+ def __init__(self, num_threads: int = 10):
13
+ self.logger = logging.getLogger()
14
+ self.normal_queue = Queue()
15
+ self.priority_queue = Queue()
16
+ self.init_threads(num_threads)
17
+
18
+ def init_threads(self, num_threads):
19
+ for i in range(num_threads):
20
+ self.logger.debug(f"Starting thread {i}/{num_threads}")
21
+ t = threading.Thread(target=self.__worker)
22
+ t.daemon = True
23
+ t.start()
24
+
25
+ def __worker(self):
26
+ while True:
27
+ try:
28
+ self.__run_job(self.priority_queue, "priority")
29
+ except queue.Empty:
30
+ try:
31
+ self.__run_job(self.normal_queue, "normal")
32
+ except queue.Empty:
33
+ time.sleep(5)
34
+ except Exception as e:
35
+ self.logger.error(f"Error in normal queue: {e}")
36
+ except Exception as e:
37
+ self.logger.error(f"Error in priority queue: {e}")
38
+
39
+ def __run_job(self, queue, type):
40
+ func_, args_ = queue.get_nowait()
41
+ if func_ is not None:
42
+ if not args_:
43
+ args_ = None
44
+ self.logger.info(
45
+ f"Executing in {type} queue {Utils.get_friendly_method_name(func_)}({args_ if args_ else ''})")
46
+ if args_ is not None:
47
+ self.logger.info(
48
+ f"Return of {Utils.get_friendly_method_name(func_)}({args_ if args_ else ''}) -> {func_(**args_)}")
49
+ else:
50
+ self.logger.info(
51
+ f"Return of {Utils.get_friendly_method_name(func_)}({args_ if args_ else ''}) -> {func_()}")
52
+ queue.task_done()
53
+ self.logger.info(
54
+ f"Executed in {type} queue {Utils.get_friendly_method_name(func_)}({args_ if args_ else ''})")
55
+
56
+ def clean_queue(self) -> GenericDataDTO:
57
+ while not self.normal_queue.empty():
58
+ try:
59
+ self.normal_queue.get(block=False)
60
+ except queue.Empty:
61
+ continue
62
+ self.normal_queue.task_done()
63
+
64
+ while not self.priority_queue.empty():
65
+ try:
66
+ self.priority_queue.get(block=False)
67
+ except queue.Empty:
68
+ continue
69
+ self.priority_queue.task_done()
70
+
71
+ return GenericDataDTO(msg="Queues clean")
72
+
73
+ def get_queue_size(self) -> list[GenericDataDTO]:
74
+
75
+ return [GenericDataDTO(msg=f"Normal queues: {self.normal_queue.qsize()}"),
76
+ GenericDataDTO(msg=f"Priority queues: {self.priority_queue.qsize()}")]
77
+
78
+ def put(self, func, priority=False, **args):
79
+ if priority:
80
+ self.priority_queue.put((func, args))
81
+ else:
82
+ self.normal_queue.put((func, args))
@@ -0,0 +1,117 @@
1
+ import logging
2
+ import threading
3
+ import time
4
+
5
+ import schedule
6
+
7
+ from csctracker_queue_scheduler.models.enums.time_unit import TimeUnit
8
+ from csctracker_queue_scheduler.models.generic_data_dto import GenericDataDTO
9
+ from csctracker_queue_scheduler.services.queue_service import QueueService
10
+ from csctracker_queue_scheduler.utils.utils import Utils
11
+
12
+
13
+ class SchedulerService:
14
+ def __init__(self, queue_service: QueueService, services: list):
15
+ self.logger = logging.getLogger()
16
+ self.queue_service = queue_service
17
+ self.services = services
18
+ self.services.append(self.queue_service)
19
+ threading.Thread(target=self.worker).start()
20
+
21
+ def worker(self):
22
+ while True:
23
+ try:
24
+ schedule.run_pending()
25
+ time.sleep(1)
26
+ except Exception as e:
27
+ self.logger.error(e)
28
+ pass
29
+ pass
30
+
31
+ def start_scheduled_job(self,
32
+ function,
33
+ args=None,
34
+ period=5,
35
+ time_hh_mm="04:00",
36
+ time_unit: TimeUnit = TimeUnit.MINUTES):
37
+ thread = threading.Thread(target=self.start_scheduler,
38
+ args=(function, args, period, time_hh_mm, time_unit))
39
+ thread.start()
40
+
41
+ def start_scheduler(self, function, args=None, period=5, time_hh_mm=None,
42
+ time_unit: TimeUnit = TimeUnit.MINUTES):
43
+ if args is None:
44
+ args = {}
45
+ if time_unit == TimeUnit.SECONDS:
46
+ schedule.every(period).seconds.do(self.put_in_queue, function, args, True)
47
+ elif time_unit == TimeUnit.MINUTES:
48
+ schedule.every(period).minutes.do(self.put_in_queue, function, args, True)
49
+ elif time_unit == TimeUnit.HOURS:
50
+ schedule.every(period).hours.do(self.put_in_queue, function, args, True)
51
+ elif time_unit == TimeUnit.DAYS:
52
+ schedule.every(period).days.do(self.put_in_queue, function, args, True)
53
+ elif time_unit == TimeUnit.WEEKS:
54
+ schedule.every(period).weeks.do(self.put_in_queue, function, args, True)
55
+ elif time_unit == TimeUnit.DAILY:
56
+ schedule.every().day.at(time_hh_mm).do(self.put_in_queue, function, args, True)
57
+ else:
58
+ raise Exception(f"Inválid time unit -> {time_unit}")
59
+ if time_unit != 'daily':
60
+ self.logger.info(
61
+ f"Job {Utils.get_friendly_method_name(function)}({args if args else ''}) scheduled to run every {period} {time_unit.value}")
62
+ else:
63
+ self.logger.info(
64
+ f"Job {Utils.get_friendly_method_name(function)}({args if args else ''}) scheduled to run at {time_hh_mm}")
65
+
66
+ def init_job(self, function, args=None, priority_job=False, class_name=None, async_job=True) -> GenericDataDTO:
67
+ executed = False
68
+ if args is None:
69
+ args = {}
70
+ if 'priority_job' in args:
71
+ priority_job = args['priority_job'] == 'true'
72
+ del args['priority_job']
73
+ if 'async_job' in args:
74
+ async_job = args['async_job'] == 'true'
75
+ del args['async_job']
76
+ if '.' in function:
77
+ classe, method = function.split('.')
78
+ instance = self.get_instance_by_class_name(classe)
79
+ if instance is not None:
80
+ call_method = SchedulerService.call_method(instance, method)
81
+ if async_job:
82
+ self.queue_service.put(call_method, priority_job, **args)
83
+ else:
84
+ ret_ = call_method(**args)
85
+ return GenericDataDTO(msg=ret_)
86
+ executed = True
87
+ else:
88
+ if class_name is not None:
89
+ instance = self.get_instance_by_class_name(class_name)
90
+ if instance is not None:
91
+ service_call_method = SchedulerService.call_method(instance, function)
92
+ if async_job:
93
+ self.queue_service.put(service_call_method, priority_job, **args)
94
+ else:
95
+ ret_ = service_call_method(**args)
96
+ return GenericDataDTO(msg=ret_)
97
+ executed = True
98
+
99
+ if not executed:
100
+ return GenericDataDTO(msg=f'Job {function} not found')
101
+ schedule_type = 'priority' if priority_job else 'normal'
102
+ return GenericDataDTO(msg=f'Job {function} scheduled for execution {schedule_type}')
103
+
104
+ @staticmethod
105
+ def call_method(instance, method_name):
106
+ method = getattr(instance, method_name)
107
+ return method
108
+
109
+ def put_in_queue(self, function, args=None, priority=False):
110
+ self.queue_service.put(function, priority, **args)
111
+
112
+ def get_instance_by_class_name(self, class_name):
113
+ class_name = Utils.snake_to_camel(class_name)
114
+ for instance in self.services:
115
+ if type(instance).__name__ == class_name:
116
+ return instance
117
+ return None
@@ -0,0 +1,3 @@
1
+ from csctracker_queue_scheduler.utils.utils import Utils
2
+
3
+ utils = Utils()
@@ -0,0 +1,39 @@
1
+ import re
2
+
3
+
4
+ class Utils:
5
+ def __init__(self):
6
+ pass
7
+
8
+ @staticmethod
9
+ def snake_to_camel(word):
10
+ split_word = re.split('_', word)
11
+ return ''.join(x.capitalize() for x in split_word)
12
+
13
+ @staticmethod
14
+ def camel_to_snake(name):
15
+ s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
16
+ return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
17
+
18
+ @staticmethod
19
+ def to_snake_case(str_, scream):
20
+ if str_ is None:
21
+ return None
22
+ str_ = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', str_)
23
+ str_ = re.sub('([a-z0-9])([A-Z])', r'\1_\2', str_).lower()
24
+ str_ = str_.replace(" ", "_")
25
+ if scream:
26
+ str_ = str_.upper()
27
+ return str_
28
+
29
+ @staticmethod
30
+ def get_friendly_method_name(method_name):
31
+ try:
32
+ try:
33
+ function_class = method_name.__self__.__class__.__name__
34
+ except:
35
+ function_class = method_name.__class__.__name__
36
+ function_name = method_name.__name__
37
+ return f"{function_class}.{function_name}"
38
+ except:
39
+ return method_name
@@ -0,0 +1,20 @@
1
+ copyright (c) 2020 Coding Kitties
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: csctracker-queue-scheduler
3
+ Version: 24.1.1
4
+ Summary: A library for handle scheduled jobs.
5
+ Home-page: https://github.com/krlsedu/CscTrackerQueueScheduler.git
6
+ Author: Carlos Eduardo
7
+ License: MIT
8
+ Keywords: queue,scheduled,scheduler
9
+ Platform: UNKNOWN
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Topic :: Software Development
14
+ Requires-Python: >=3
15
+ Description-Content-Type: text/markdown
16
+ Requires-Dist: pytz ~=2023.3.post1
17
+ Requires-Dist: schedule ~=1.2.1
18
+
19
+ # CscTrackerQueueScheduler
20
+
21
+ This project is a Python job scheduling and queuing library. It allows jobs to be scheduled to run at specific periods
22
+ and queues these jobs for execution.
23
+
24
+ ## Key Features
25
+
26
+ - **Job Scheduling**: The Scheduler service (`SchedulerService`) allows jobs to be scheduled for execution at specified
27
+ intervals. It supports intervals in seconds, minutes, hours, days, and weeks, as well as daily scheduling at a
28
+ specific time.
29
+
30
+ - **Queue Service**: Scheduled tasks are put in a queue (`QueueService`) for execution. Tasks can be queued with or
31
+ without priority, which determines their order of execution.
32
+
33
+ ## Installing
34
+
35
+ Install and update using pip:
36
+
37
+ ```
38
+ pip install csctracker-queue-scheduler
39
+ ```
40
+
41
+ ## Documentation and Examples
42
+
43
+ COMING SOON
44
+
45
+ ## Simple Example
46
+
47
+ ```python
48
+ from csctracker_queue_scheduler.models.enums.time_unit import TimeUnit
49
+ from csctracker_queue_scheduler.services.queue_service import QueueService
50
+ from csctracker_queue_scheduler.services.scheduler_service import SchedulerService
51
+
52
+
53
+ def my_function(): print("Hello, world!")
54
+
55
+
56
+ scheduler = SchedulerService(queue_service=QueueService(), services=[])
57
+ scheduler.start_scheduled_job(
58
+ function=my_function, period=5, time_unit=TimeUnit.MINUTES)
59
+ ```
60
+
61
+ In the above example, `my_function` would be scheduled to run every 5 minutes.
62
+
63
+ ## Testing
64
+
65
+ Currently, this project does not have automated tests. They may be added in the future as needed.
66
+
67
+ ## Contributing
68
+
69
+ If you would like to contribute to this project, feel free to fork the repository, make your changes, and propose a pull
70
+ request.
71
+
72
+ ## License
73
+
74
+ This project is under the MIT license. Please refer to the `LICENSE` file for more details.
75
+
@@ -0,0 +1,16 @@
1
+ csctracker_queue_scheduler/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ csctracker_queue_scheduler/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ csctracker_queue_scheduler/models/base_dto.py,sha256=5jXZgV5nslwQbUC9HVesz6bc0uqtP528y_oM2hEGIIQ,1014
4
+ csctracker_queue_scheduler/models/generic_data_dto.py,sha256=Cu6iFdFYGBL8NHCdXI56pFyuayd9bSpPvf7daKimA6Y,174
5
+ csctracker_queue_scheduler/models/enums/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ csctracker_queue_scheduler/models/enums/time_unit.py,sha256=u1BerX-mxWdERQZCyiVT635Zr45Ep_g6i32FVgJUb9s,172
7
+ csctracker_queue_scheduler/services/__init__.py,sha256=j615fFoh5Z2YLL1EDcIIrWNXbejB0PytPGWQ9_C6-cA,106
8
+ csctracker_queue_scheduler/services/queue_service.py,sha256=GDTBUglVRZyMAjTrbHUw_AuMED5ABOTlxjMJ6Y06EAI,2979
9
+ csctracker_queue_scheduler/services/scheduler_service.py,sha256=h2YWfnhLMuFeMykvUAs7V1bZmllASqiUniHgU3xun54,5022
10
+ csctracker_queue_scheduler/utils/__init__.py,sha256=qb9SmpBbVojhx1jAMJnXpkP1KCbTlPEfrZyYV-vMO-s,73
11
+ csctracker_queue_scheduler/utils/utils.py,sha256=dyFKRvIeOi-ezCvkxzMthU10TI2fjwcerSgrtVV6LUE,1124
12
+ csctracker_queue_scheduler-24.1.1.dist-info/LICENSE,sha256=lwEypG02cgtE7cxzGAjChgdE2HR-t2EOYiIH-Q71X9w,1058
13
+ csctracker_queue_scheduler-24.1.1.dist-info/METADATA,sha256=RVwghSwN_yjJCWyqGzVCpRuQy5QreFOGpl775EbZGaY,2279
14
+ csctracker_queue_scheduler-24.1.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
15
+ csctracker_queue_scheduler-24.1.1.dist-info/top_level.txt,sha256=o1c0yYT7-NME4dtql1Fok3ZS_eP-BMSpY9MUVpByZLE,27
16
+ csctracker_queue_scheduler-24.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.42.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ csctracker_queue_scheduler