celery-bittensor 0.1__tar.gz

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.
@@ -0,0 +1,107 @@
1
+ name: CI/CD
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ release:
8
+ types: [published]
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ build:
15
+ name: Build distributions
16
+ runs-on: ubuntu-latest
17
+ steps:
18
+ - uses: actions/checkout@v7
19
+
20
+ - uses: actions/setup-python@v7
21
+ with:
22
+ python-version: "3.12"
23
+
24
+ - name: Build sdist and wheel
25
+ run: |
26
+ python -m pip install --upgrade build twine
27
+ python -m build
28
+ python -m twine check --strict dist/*
29
+
30
+ - name: Check that the release tag matches the package version
31
+ if: github.event_name == 'release'
32
+ run: |
33
+ version=$(python -c "import re, pathlib; print(re.search(r'__version__ = \"(.+?)\"', pathlib.Path('src/celery_bittensor/version.py').read_text()).group(1))")
34
+ if [ "v${version}" != "${GITHUB_REF_NAME}" ] && [ "${version}" != "${GITHUB_REF_NAME}" ]; then
35
+ echo "Tag ${GITHUB_REF_NAME} does not match package version ${version}"
36
+ exit 1
37
+ fi
38
+
39
+ - uses: actions/upload-artifact@v7
40
+ with:
41
+ name: dist
42
+ path: dist/
43
+ if-no-files-found: error
44
+
45
+ unit-tests:
46
+ name: Unit tests (Python ${{ matrix.python-version }})
47
+ runs-on: ubuntu-latest
48
+ strategy:
49
+ fail-fast: false
50
+ matrix:
51
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14", "3.14"]
52
+ steps:
53
+ - uses: actions/checkout@v7
54
+
55
+ - uses: actions/setup-python@v7
56
+ with:
57
+ python-version: ${{ matrix.python-version }}
58
+
59
+ - name: Install
60
+ run: |
61
+ python -m pip install --upgrade pip
62
+ python -m pip install -e . --group dev
63
+
64
+ - name: Run unit tests
65
+ run: python -m pytest
66
+
67
+ smoke-test:
68
+ name: Install and import (Python ${{ matrix.python-version }})
69
+ needs: build
70
+ runs-on: ubuntu-latest
71
+ strategy:
72
+ fail-fast: false
73
+ matrix:
74
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
75
+ steps:
76
+ - uses: actions/setup-python@v7
77
+ with:
78
+ python-version: ${{ matrix.python-version }}
79
+
80
+ - uses: actions/download-artifact@v8
81
+ with:
82
+ name: dist
83
+ path: dist/
84
+
85
+ - name: Install the wheel and import it
86
+ run: |
87
+ python -m pip install dist/*.whl
88
+ python -c "import celery_bittensor; print(celery_bittensor.__version__)"
89
+
90
+ pypi-publish:
91
+ name: Upload release to PyPI
92
+ if: github.event_name == 'release'
93
+ needs: [build, unit-tests, smoke-test]
94
+ runs-on: ubuntu-latest
95
+ environment:
96
+ name: pypi
97
+ url: https://pypi.org/p/celery-bittensor
98
+ permissions:
99
+ id-token: write
100
+ steps:
101
+ - uses: actions/download-artifact@v8
102
+ with:
103
+ name: dist
104
+ path: dist/
105
+
106
+ - name: Publish package distributions to PyPI
107
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,220 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ *.lcov
51
+ .hypothesis/
52
+ .pytest_cache/
53
+ cover/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ .pybuilder/
77
+ target/
78
+
79
+ # Jupyter Notebook
80
+ .ipynb_checkpoints
81
+
82
+ # IPython
83
+ profile_default/
84
+ ipython_config.py
85
+
86
+ # pyenv
87
+ # For a library or package, you might want to ignore these files since the code is
88
+ # intended to run in multiple environments; otherwise, check them in:
89
+ # .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ # Pipfile.lock
97
+
98
+ # uv
99
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
100
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
101
+ # commonly ignored for libraries.
102
+ # uv.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ # poetry.lock
110
+ # poetry.toml
111
+
112
+ # pdm
113
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
115
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
116
+ # pdm.lock
117
+ # pdm.toml
118
+ .pdm-python
119
+ .pdm-build/
120
+
121
+ # pixi
122
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
123
+ # pixi.lock
124
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
125
+ # in the .venv directory. It is recommended not to include this directory in version control.
126
+ .pixi/*
127
+ !.pixi/config.toml
128
+
129
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
130
+ __pypackages__/
131
+
132
+ # Celery stuff
133
+ celerybeat-schedule*
134
+ celerybeat.pid
135
+
136
+ # Redis
137
+ *.rdb
138
+ *.aof
139
+ *.pid
140
+
141
+ # RabbitMQ
142
+ mnesia/
143
+ rabbitmq/
144
+ rabbitmq-data/
145
+
146
+ # ActiveMQ
147
+ activemq-data/
148
+
149
+ # SageMath parsed files
150
+ *.sage.py
151
+
152
+ # Environments
153
+ .env
154
+ .envrc
155
+ .venv
156
+ env/
157
+ venv/
158
+ ENV/
159
+ env.bak/
160
+ venv.bak/
161
+
162
+ # Spyder project settings
163
+ .spyderproject
164
+ .spyproject
165
+
166
+ # Rope project settings
167
+ .ropeproject
168
+
169
+ # mkdocs/Zensical documentation
170
+ /site
171
+
172
+ # mypy
173
+ .mypy_cache/
174
+ .dmypy.json
175
+ dmypy.json
176
+
177
+ # Pyre type checker
178
+ .pyre/
179
+
180
+ # pytype static type analyzer
181
+ .pytype/
182
+
183
+ # Cython debug symbols
184
+ cython_debug/
185
+
186
+ # PyCharm
187
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
188
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
189
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
190
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
191
+ # .idea/
192
+
193
+ # Abstra
194
+ # Abstra is an AI-powered process automation framework.
195
+ # Ignore directories containing user credentials, local state, and settings.
196
+ # Learn more at https://abstra.io/docs
197
+ .abstra/
198
+
199
+ # Visual Studio Code
200
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
201
+ # can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
202
+ # and can be added to the global gitignore or merged into this file. However, if you prefer, you
203
+ # could uncomment the following to ignore the entire vscode folder
204
+ # .vscode/
205
+ # Temporary file for partial code execution
206
+ tempCodeRunnerFile.py
207
+
208
+ # Ruff stuff:
209
+ .ruff_cache/
210
+
211
+ # PyPI configuration file
212
+ .pypirc
213
+
214
+ # Marimo
215
+ marimo/_static/
216
+ marimo/_lsp/
217
+ __marimo__/
218
+
219
+ # Streamlit
220
+ .streamlit/secrets.toml
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Tomasz Żyźniewski
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.5
2
+ Name: celery-bittensor
3
+ Version: 0.1
4
+ Summary: Celery's Bittensor Schedule
5
+ Project-URL: Homepage, https://github.com/zyzniewski/celery-bittensor
6
+ Project-URL: Issues, https://github.com/zyzniewski/celery-bittensor/issues
7
+ Author-email: Tomasz Żyźniewski <zyzniewski@gmail.com>
8
+ License-Expression: BSD-3-Clause
9
+ License-File: LICENSE
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.10
13
+ Requires-Dist: bittensor==11.*
14
+ Requires-Dist: celery==5.*
15
+ Description-Content-Type: text/markdown
16
+
17
+ # celery-bittensor
18
+ Bittensor's Schedule for Celery
19
+
20
+ ## Installation
21
+
22
+ ```
23
+ pip install celery-bittensor
24
+ ```
25
+
26
+ Requires Python 3.10+.
27
+
28
+
29
+ ## Example
30
+
31
+ ```python
32
+ import celery
33
+ import celery_bittensor
34
+
35
+ app = celery.Celery()
36
+
37
+
38
+ @app.on_after_configure.connect
39
+ def setup_periodic_tasks(sender: celery.Celery, **kwargs):
40
+ # Calls test('hello') every 10 blocks.
41
+ sender.add_periodic_task(
42
+ celery_bittensor.schedule(10), test.s("hello"), name="add every 10"
43
+ )
44
+
45
+
46
+ @app.task
47
+ def test(arg):
48
+ print(arg)
49
+ ```
50
+
51
+ ## License
52
+
53
+ BSD 3-Clause License, see [LICENSE](LICENSE).
@@ -0,0 +1,37 @@
1
+ # celery-bittensor
2
+ Bittensor's Schedule for Celery
3
+
4
+ ## Installation
5
+
6
+ ```
7
+ pip install celery-bittensor
8
+ ```
9
+
10
+ Requires Python 3.10+.
11
+
12
+
13
+ ## Example
14
+
15
+ ```python
16
+ import celery
17
+ import celery_bittensor
18
+
19
+ app = celery.Celery()
20
+
21
+
22
+ @app.on_after_configure.connect
23
+ def setup_periodic_tasks(sender: celery.Celery, **kwargs):
24
+ # Calls test('hello') every 10 blocks.
25
+ sender.add_periodic_task(
26
+ celery_bittensor.schedule(10), test.s("hello"), name="add every 10"
27
+ )
28
+
29
+
30
+ @app.task
31
+ def test(arg):
32
+ print(arg)
33
+ ```
34
+
35
+ ## License
36
+
37
+ BSD 3-Clause License, see [LICENSE](LICENSE).
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["hatchling >= 1.26"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "celery-bittensor"
7
+ authors = [
8
+ { name="Tomasz Żyźniewski", email="zyzniewski@gmail.com" },
9
+ ]
10
+ description = "Celery's Bittensor Schedule"
11
+ readme = "README.md"
12
+ requires-python = ">=3.10"
13
+ classifiers = [
14
+ "Programming Language :: Python :: 3",
15
+ "Operating System :: OS Independent",
16
+ ]
17
+ license = "BSD-3-Clause"
18
+ license-files = ["LICENSE"]
19
+ dependencies = [
20
+ "bittensor == 11.*",
21
+ "celery == 5.*",
22
+ ]
23
+ dynamic = ["version"]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/zyzniewski/celery-bittensor"
27
+ Issues = "https://github.com/zyzniewski/celery-bittensor/issues"
28
+
29
+ [tool.hatch.version]
30
+ path = "src/celery_bittensor/version.py"
31
+
32
+ [dependency-groups]
33
+ dev = ["pytest>=8.0"]
@@ -0,0 +1 @@
1
+ -e .
@@ -0,0 +1,9 @@
1
+ from .schedules import (
2
+ schedule,
3
+ )
4
+ from .version import __version__
5
+
6
+ __all__ = [
7
+ "__version__",
8
+ "schedule",
9
+ ]
@@ -0,0 +1,106 @@
1
+ import asyncio
2
+ import contextlib
3
+ import datetime
4
+ import logging
5
+ import threading
6
+ from typing import NamedTuple
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ class Block(NamedTuple):
12
+ number: int
13
+ timestamp: datetime.datetime
14
+
15
+
16
+ class BittensorSynced:
17
+ RETRY = datetime.timedelta(seconds=5)
18
+
19
+ def __init__(
20
+ self,
21
+ network: str,
22
+ finalized: bool,
23
+ ):
24
+ self.network = network
25
+ self.finalized = finalized
26
+
27
+ self.block: Block | None = None
28
+ self.block_time: float | None = None
29
+
30
+ self._thread = threading.Thread(
31
+ daemon=True,
32
+ name=f"BittensorSynced[{self.network}]",
33
+ target=self._run,
34
+ )
35
+ self._stop = threading.Event()
36
+
37
+ self._loop: asyncio.AbstractEventLoop | None = None
38
+ self._task: asyncio.Task | None = None
39
+
40
+ def start(self) -> None:
41
+ self._thread.start()
42
+
43
+ def stop(self) -> None:
44
+ self._stop.set()
45
+
46
+ loop, task = self._loop, self._task
47
+
48
+ if loop and task:
49
+ with contextlib.suppress(RuntimeError):
50
+ loop.call_soon_threadsafe(task.cancel)
51
+
52
+ def join(self) -> None:
53
+ self._thread.join()
54
+
55
+ def _run(self) -> None:
56
+ try:
57
+ asyncio.run(self._main())
58
+ except asyncio.CancelledError:
59
+ pass
60
+ finally:
61
+ self._loop = None
62
+ self._task = None
63
+
64
+ async def _main(self) -> None:
65
+ self._loop = asyncio.get_running_loop()
66
+ self._task = asyncio.current_task()
67
+
68
+ if self._stop.is_set():
69
+ return
70
+
71
+ while True:
72
+ try:
73
+ await self._blocks_generator()
74
+ except Exception:
75
+ self.block = None
76
+
77
+ logger.exception(
78
+ "Error occured during processing on %s. Reconnecting in %ds",
79
+ self.network,
80
+ self.RETRY.total_seconds(),
81
+ )
82
+
83
+ await asyncio.sleep(self.RETRY.total_seconds())
84
+
85
+ async def _blocks_generator(self) -> None:
86
+ import bittensor
87
+
88
+ logger.info("Connecting to %s", self.network)
89
+
90
+ async with bittensor.Subtensor(
91
+ network=self.network,
92
+ retry_forever=True,
93
+ ) as client:
94
+ logger.info("Connected to %s. Checking block time", self.network)
95
+
96
+ self.block_time = await client.block_time()
97
+
98
+ logger.info("Awaiting for blocks on %s", self.network)
99
+
100
+ async for block in client.blocks(finalized=self.finalized):
101
+ logger.info("New block received on %s: %d", self.network, block.number)
102
+
103
+ self.block = Block(
104
+ block.number,
105
+ datetime.datetime.now(datetime.timezone.utc),
106
+ )
@@ -0,0 +1,185 @@
1
+ import atexit
2
+ import datetime
3
+ import threading
4
+ from collections.abc import Callable
5
+ from typing import ClassVar
6
+
7
+ import celery.schedules
8
+
9
+ from .client import BittensorSynced, Block
10
+
11
+
12
+ class BaseBittensorSchedule(celery.schedules.BaseSchedule):
13
+ INIT_PULL = datetime.timedelta(microseconds=100)
14
+
15
+ _lock: ClassVar[threading.Lock] = threading.Lock()
16
+ _clients: ClassVar[dict[tuple[str, bool], BittensorSynced]] = {}
17
+
18
+ def __init__(
19
+ self,
20
+ network: str = "finney",
21
+ finalized: bool = True,
22
+ nowfun: Callable | None = None,
23
+ app: celery.Celery | None = None,
24
+ ):
25
+ super().__init__(nowfun, app)
26
+
27
+ self.network = network
28
+ self.finalized = finalized
29
+ self.last_run_at = Block(0, self.maybe_make_aware(datetime.datetime.min))
30
+
31
+ self._bittensor: BittensorSynced | None = None
32
+
33
+ def __eq__(self, other: object) -> bool:
34
+ if isinstance(other, BaseBittensorSchedule):
35
+ return (
36
+ self.network == other.network
37
+ and self.finalized == other.finalized
38
+ and super().__eq__(other)
39
+ )
40
+
41
+ return NotImplemented
42
+
43
+ def __getstate__(self) -> dict:
44
+ return {
45
+ "finalized": self.finalized,
46
+ "last_run_at": self.last_run_at,
47
+ "network": self.network,
48
+ "nowfun": self.nowfun,
49
+ }
50
+
51
+ def __setstate__(self, state: dict) -> None:
52
+ self.__dict__.update(state)
53
+ self._app = None
54
+ self._bittensor = None
55
+
56
+ @property
57
+ def bittensor(self) -> BittensorSynced:
58
+ if not self._bittensor:
59
+ with BaseBittensorSchedule._lock:
60
+ if (self.network, self.finalized) not in BaseBittensorSchedule._clients:
61
+ BaseBittensorSchedule._clients[(self.network, self.finalized)] = (
62
+ BittensorSynced(
63
+ network=self.network,
64
+ finalized=self.finalized,
65
+ )
66
+ )
67
+ BaseBittensorSchedule._clients[
68
+ (self.network, self.finalized)
69
+ ].start()
70
+
71
+ self._bittensor = BaseBittensorSchedule._clients[
72
+ (self.network, self.finalized)
73
+ ]
74
+
75
+ return self._bittensor
76
+
77
+ def is_due(self, last_run_at: datetime.datetime) -> celery.schedules.schedstate:
78
+ if not self.bittensor.block_time:
79
+ return celery.schedules.schedstate(
80
+ is_due=False, next=self.INIT_PULL.total_seconds()
81
+ )
82
+
83
+ block = self.bittensor.block
84
+
85
+ if not block:
86
+ return celery.schedules.schedstate(
87
+ is_due=False, next=self.INIT_PULL.total_seconds()
88
+ )
89
+
90
+ if self.last_run_at.number == 0:
91
+ self.last_run_at = block
92
+
93
+ is_due = self._prev_block(block.number) > self.last_run_at.number
94
+
95
+ if is_due:
96
+ self.last_run_at = block
97
+
98
+ return celery.schedules.schedstate(
99
+ is_due=is_due,
100
+ next=self._time_until_next(block).total_seconds(),
101
+ )
102
+
103
+ def _next_block(self, block: int) -> int:
104
+ raise NotImplementedError
105
+
106
+ def _prev_block(self, block: int) -> int:
107
+ raise NotImplementedError
108
+
109
+ def _time_until_next(self, block: Block) -> datetime.timedelta:
110
+ assert self.bittensor.block_time is not None
111
+
112
+ target = self._next_block(block.number)
113
+ elapsed = self.maybe_make_aware(self.now()) - block.timestamp
114
+ remaining = (target - block.number) * datetime.timedelta(
115
+ seconds=self.bittensor.block_time
116
+ ) - elapsed
117
+
118
+ return max(remaining, self.INIT_PULL)
119
+
120
+
121
+ class schedule(BaseBittensorSchedule):
122
+ def __init__(
123
+ self,
124
+ run_every: int,
125
+ relative: bool = False,
126
+ network: str = "finney",
127
+ finalized: bool = True,
128
+ nowfun: Callable | None = None,
129
+ app: celery.Celery | None = None,
130
+ ):
131
+ if not isinstance(run_every, int):
132
+ raise TypeError("run_every must be an integer")
133
+
134
+ if run_every <= 0:
135
+ raise ValueError("run_every must be a positive integer")
136
+
137
+ super().__init__(
138
+ network,
139
+ finalized,
140
+ nowfun,
141
+ app,
142
+ )
143
+
144
+ self.run_every = run_every
145
+ self.relative = relative
146
+
147
+ def __eq__(self, other: object) -> bool:
148
+ if isinstance(other, schedule):
149
+ return (
150
+ self.run_every == other.run_every
151
+ and self.relative == other.relative
152
+ and super().__eq__(other)
153
+ )
154
+
155
+ return self.run_every == other
156
+
157
+ def __getstate__(self) -> dict:
158
+ state = super().__getstate__()
159
+ state.update({
160
+ "run_every": self.run_every,
161
+ "relative": self.relative,
162
+ })
163
+
164
+ return state
165
+
166
+ def _next_block(self, block: int) -> int:
167
+ return self._prev_block(block) + self.run_every
168
+
169
+ def _prev_block(self, block: int) -> int:
170
+ if self.relative:
171
+ base = self.last_run_at.number
172
+ return base + (block - base) // self.run_every * self.run_every
173
+
174
+ return block - block % self.run_every
175
+
176
+
177
+ def stop_watchers() -> None:
178
+ for watcher in BaseBittensorSchedule._clients.values():
179
+ watcher.stop()
180
+
181
+ for watcher in BaseBittensorSchedule._clients.values():
182
+ watcher.join()
183
+
184
+
185
+ atexit.register(stop_watchers)
@@ -0,0 +1 @@
1
+ __version__ = "0.1"
@@ -0,0 +1,98 @@
1
+ import datetime
2
+ import unittest.mock
3
+
4
+ import pytest
5
+
6
+ from celery_bittensor import schedule
7
+ from celery_bittensor.client import Block
8
+
9
+ BLOCK_TIME = datetime.timedelta(seconds=12)
10
+
11
+
12
+ @pytest.mark.parametrize(
13
+ "scheduler, last_run_at, block_number, prev_block",
14
+ [
15
+ (schedule(5, False), 100, 100, 100),
16
+ (schedule(5, False), 100, 101, 100),
17
+ (schedule(5, False), 100, 102, 100),
18
+ (schedule(5, False), 100, 103, 100),
19
+ (schedule(5, False), 100, 104, 100),
20
+ (schedule(5, False), 100, 105, 105),
21
+ (schedule(5, False), 105, 106, 105),
22
+ (schedule(5, True), 101, 101, 101),
23
+ (schedule(5, True), 101, 102, 101),
24
+ (schedule(5, True), 101, 103, 101),
25
+ (schedule(5, True), 101, 104, 101),
26
+ (schedule(5, True), 101, 105, 101),
27
+ (schedule(5, True), 101, 106, 106),
28
+ (schedule(5, True), 106, 107, 106),
29
+ ],
30
+ )
31
+ def test_schedule_prev_block(scheduler, last_run_at, block_number, prev_block):
32
+ scheduler.last_run_at = Block(last_run_at, datetime.datetime.now())
33
+ scheduler._bittensor = unittest.mock.Mock(
34
+ block=Block(block_number, datetime.datetime.now()),
35
+ block_time=BLOCK_TIME.total_seconds(),
36
+ )
37
+
38
+ assert scheduler._prev_block(block_number) == prev_block
39
+
40
+
41
+ @pytest.mark.parametrize(
42
+ "scheduler, last_run_at, block_number, next_block",
43
+ [
44
+ (schedule(5, False), 100, 100, 105),
45
+ (schedule(5, False), 100, 101, 105),
46
+ (schedule(5, False), 100, 102, 105),
47
+ (schedule(5, False), 100, 103, 105),
48
+ (schedule(5, False), 100, 104, 105),
49
+ (schedule(5, False), 100, 105, 110),
50
+ (schedule(5, False), 105, 106, 110),
51
+ (schedule(5, True), 101, 101, 106),
52
+ (schedule(5, True), 101, 102, 106),
53
+ (schedule(5, True), 101, 103, 106),
54
+ (schedule(5, True), 101, 104, 106),
55
+ (schedule(5, True), 101, 105, 106),
56
+ (schedule(5, True), 101, 106, 111),
57
+ (schedule(5, True), 106, 107, 111),
58
+ ],
59
+ )
60
+ def test_schedule_next_block(scheduler, last_run_at, block_number, next_block):
61
+ scheduler.last_run_at = Block(last_run_at, datetime.datetime.now())
62
+ scheduler._bittensor = unittest.mock.Mock(
63
+ block=Block(block_number, datetime.datetime.now()),
64
+ block_time=BLOCK_TIME.total_seconds(),
65
+ )
66
+
67
+ assert scheduler._next_block(block_number) == next_block
68
+
69
+
70
+ @pytest.mark.parametrize(
71
+ "scheduler, last_run_at, block_number, is_due",
72
+ [
73
+ (schedule(5, False), 100, 100, False),
74
+ (schedule(5, False), 100, 101, False),
75
+ (schedule(5, False), 100, 102, False),
76
+ (schedule(5, False), 100, 103, False),
77
+ (schedule(5, False), 100, 104, False),
78
+ (schedule(5, False), 100, 105, True),
79
+ (schedule(5, False), 105, 106, False),
80
+ (schedule(5, True), 101, 101, False),
81
+ (schedule(5, True), 101, 102, False),
82
+ (schedule(5, True), 101, 103, False),
83
+ (schedule(5, True), 101, 104, False),
84
+ (schedule(5, True), 101, 105, False),
85
+ (schedule(5, True), 101, 106, True),
86
+ (schedule(5, True), 106, 107, False),
87
+ ],
88
+ )
89
+ def test_schedule_is_due(scheduler, last_run_at, block_number, is_due):
90
+ now = datetime.datetime.now(datetime.timezone.utc)
91
+
92
+ scheduler.last_run_at = Block(last_run_at, now)
93
+ scheduler._bittensor = unittest.mock.Mock(
94
+ block=Block(block_number, now),
95
+ block_time=BLOCK_TIME.total_seconds(),
96
+ )
97
+
98
+ assert scheduler.is_due(now).is_due is is_due