Esempio n. 1
0
    def test_raises_error_when_subscription_is_duplicated(self):
        with pytest.raises(RuntimeError) as excinfo:
            load_subscriptions_from_paths(
                ["tests.test_config", "tests.more_subs.subs"])

        assert (
            str(excinfo.value) ==
            "Duplicate subscription name found: rele-another-cool-topic. Subs "
            "tests.more_subs.subs.another_sub_stub and tests.test_config.another_sub_stub collide."
        )
Esempio n. 2
0
def main():
    # modify path so we can import modules and packages
    sys.path.insert(0, os.getcwd())

    parser = argparse.ArgumentParser(
        prog="Relé",
        description="Harness the power of Relé from the command line")

    subparsers = parser.add_subparsers(help="Select a command", dest="command")

    run_parser = subparsers.add_parser(
        "run",
        help="Run a Relé worker with auto-discovery of subs modules in the "
        "current path. Auto-discovery will include all subs "
        "and settings modules. If no settings module is discovered, "
        "defaults will be used.",
    )
    run_parser.add_argument(
        "--settings",
        "-s",
        default=None,
        required=False,
        help="Settings file dot path. Ex. project.settings. "
        "If none is supplied, Relé will attempt to autodiscover in the root path.",
    )
    args = parser.parse_args()

    if args.command == "run":
        settings, module_paths = discover.sub_modules(args.settings)
        configuration = config.setup(settings.RELE if settings else None)
        subs = config.load_subscriptions_from_paths(module_paths,
                                                    configuration.sub_prefix,
                                                    configuration.filter_by)
        create_and_run(subs, configuration)
Esempio n. 3
0
    def handle(self, *args, **options):
        headers = ["Topic", "Subscriber(s)", "Sub"]

        subscription_paths = discover_subs_modules()
        subs = sorted(load_subscriptions_from_paths(subscription_paths),
                      key=lambda sub: sub.topic)
        sub_data = [(sub.topic, sub.name, sub._func.__name__) for sub in subs]

        self.stdout.write(tabulate(sub_data, headers=headers))
Esempio n. 4
0
    def test_loads_subscriptions_when_they_are_class_based(self):
        subscriptions = load_subscriptions_from_paths(
            ["tests.subs"],
            sub_prefix="test",
            filter_by=[lambda attrs: attrs.get("lang") == "en"],
        )

        assert len(subscriptions) == 2
        klass_sub = subscriptions[0]
        assert isinstance(klass_sub, Subscription)
        assert klass_sub.name == "test-alternative-cool-topic"
        assert klass_sub({"id": 4}, lang="en") == 4
Esempio n. 5
0
 def handle(self, *args, **options):
     if all(
             map(lambda x: x.get("CONN_MAX_AGE"),
                 settings.DATABASES.values())):
         self.stderr.write(
             self.style.WARNING(
                 "WARNING: settings.CONN_MAX_AGE is not set to 0. "
                 "This may result in slots for database connections to "
                 "be exhausted."))
     subs = config.load_subscriptions_from_paths(discover_subs_modules(),
                                                 self.config.sub_prefix,
                                                 self.config.filter_by)
     self.stdout.write(f"Configuring worker with {len(subs)} "
                       f"subscription(s)...")
     create_and_run(subs, self.config)
Esempio n. 6
0
 def subscriptions(self):
     return load_subscriptions_from_paths(
         ["tests.test_config"],
         sub_prefix="test",
         filter_by=lambda attrs: attrs.get("lang") == "en",
     )