def test_invalid_color_exception(webhook, default_options): """Test InvalidColorException.""" options = get_options(default_options, **{}) options["webhook"] = webhook options["slack_beat_init_color"] = "asdf" with pytest.raises(InvalidColorException): app = Celery("schedule") app.config_from_object("tests.celeryapp.config") slack_app = Slackify(app, **options) # Doesn"t raise with valid color. options["slack_beat_init_color"] = "#000000" slack_app = Slackify(app, **options) # noqa F481
def test_slackify_task_filtration_exception(webhook, include_tasks, exclude_tasks, default_options): """Test TaskFiltrationException.""" these_options = locals() these_options.pop("default_options") options = get_options(default_options, **these_options) options["webhook"] = webhook if include_tasks and exclude_tasks: with pytest.raises(TaskFiltrationException): app = Celery("schedule") app.config_from_object("tests.celeryapp.config") slack_app = Slackify(app, **options)
def test_slackify_webhook_exception(possible_webhook, default_options): """Test Slackify construction.""" these_options = locals() these_options.pop("default_options") options = get_options(default_options, **these_options) options["webhook"] = possible_webhook # Test webhook exception if options["webhook"] is None: with pytest.raises(MissingWebhookException): app = Celery("schedule") app.config_from_object("tests.celeryapp.config") slack_app = Slackify(app, **options) return
def test_show_beat_option(webhook, show_beat, default_options, mocker): """Test the show_startup option.""" these_options = locals() these_options.pop("default_options") options = get_options(default_options, **these_options) app = Celery("schedule") app.config_from_object("tests.celeryapp.config") mocked_slack_beat_init = \ mocker.patch("celery_slack.slackify.slack_beat_init") slack_app = Slackify(app, **options) if show_beat: assert mocked_slack_beat_init.called else: assert not mocked_slack_beat_init.called
def test_failure_only_patching(webhook, failures_only, default_options, mocker): """Test failures_only option omits patching on_success method.""" these_options = locals() these_options.pop("default_options") options = get_options(default_options, **these_options) app = Celery("schedule") app.config_from_object("tests.celeryapp.config") mocked_task_success = \ mocker.patch("celery_slack.slackify.slack_task_success") slack_app = Slackify(app, **options) # If patching skipped ensure the method id is the same. if failures_only: assert not mocked_task_success.called else: assert mocked_task_success.called
def test_show_broker_option(webhook, show_broker, default_options, mocker): """Test the show_broker option.""" these_options = locals() these_options.pop("default_options") these_options.pop("mocker") options = get_options(default_options, **these_options) app = Celery("schedule") app.config_from_object("tests.celeryapp.config") mocked_slack_broker_connect = mocker.patch( "celery_slack.slackify.slack_broker_connect") mocked_slack_broker_disconnect = mocker.patch( "celery_slack.slackify.slack_broker_disconnect") slack_app = Slackify(app, **options) if show_broker: assert mocked_slack_broker_connect.called assert mocked_slack_broker_disconnect.called else: assert not mocked_slack_broker_connect.called assert not mocked_slack_broker_disconnect.called
slack_webhook = os.environ["SLACK_WEBHOOK"] logging.basicConfig(level="INFO") schedule = get_schedule() app = Celery("schedule") app.config_from_object("tests.celeryapp.config") options = { "flower_base_url": "https://flower.example.com", "webhook": slack_webhook, "beat_schedule": schedule, "show_beat": True, # "show_task_prerun": True, # "failures_only": True, # "show_celery_hostname": True, # "show_startup": False, # "show_shutdown": False, "slack_request_timeout": 3, "show_broker": True, } # logging.info("Creating celery-slack object.") slack_app = Slackify(app, **options) # slack_app = Slackify(app, slack_webhook) # logging.info("Created celery-slack object.") if __name__ == "__main__": app.start()
import os from celery import Celery from celery_slack import Slackify # Set sensorsafrica application settings module for sensorsafrica Celery instance os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sensorsafrica.settings") # Create Celery instance and pass the project name # The instance is bound to the variable app app = Celery("sensorsafrica") # Pass config made of up values begging with the prefix of CELERY_ in settings.py app.config_from_object("django.conf:settings", namespace="CELERY") # Autodiscover tasks in tasks.py app.autodiscover_tasks() SLACK_WEBHOOK = os.environ.get("SENSORSAFRICA_CELERY_SLACK_WEBHOOK", "") SLACK_WEBHOOK_FAILURES_ONLY = os.environ.get( "SENSORSAFRICA_CELERY_SLACK_WEBHOOK_FAILURES_ONLY", "").strip().lower() in ('true', 't', '1') options = {'failures_only': SLACK_WEBHOOK_FAILURES_ONLY} slack_app = Slackify(app, SLACK_WEBHOOK, **options)