Skip to main content

Overview

The service module runs as a Kodi background service when the plugin starts. It performs initialization tasks including cache clearing and provider prefetching to improve user experience.

Service Execution

The service runs automatically when Kodi loads the Cricfy plugin. It executes the following operations sequentially:

1. Cache Clearing

cache.delete('%')
log_info("service", "All cache cleared")
Clears all cached data from the plugin’s cache storage using a wildcard pattern ('%'). This ensures fresh data is fetched on plugin startup.
Cache Entries Cleared:
  • "cricfy_providers" - Cached provider list
  • "channels_{hash}" - All cached channel lists (hashed by provider URL)
  • Any other cached entries
Cache clearing happens on every plugin startup. This may cause slightly slower initial loading but ensures users always see up-to-date content.

2. Provider Prefetching

providers = get_providers()
log_info("service", f"Fetched {len(providers)} providers")
Immediately fetches the provider list after clearing cache. This “warms up” the cache so providers are available instantly when the user opens the plugin UI.
Benefits:
  1. Faster UI Loading: Provider list is already cached when user navigates to plugin
  2. Early Error Detection: Network/decryption errors are caught during service initialization
  3. Background Operation: Happens without blocking Kodi’s UI

Code Structure

from lib.config import cache
from lib.logger import log_info
from lib.providers import get_providers

if __name__ == '__main__':
    # Clear all cache entries
    cache.delete('%')
    log_info("service", "All cache cleared")

    # Prefetch providers to warm up cache
    providers = get_providers()
    log_info("service", f"Fetched {len(providers)} providers")

Execution Context

The service is configured in addon.xml:
<extension point="xbmc.service" library="service.py" />
Kodi runs this script:
  • When: Plugin is enabled/Kodi starts
  • Thread: Background thread (non-blocking)
  • Lifecycle: Runs once per Kodi session

Dependencies

ModuleUsage
lib.config.cacheCache management (delete operation)
lib.logger.log_infoLogging service operations
lib.providers.get_providersProvider fetching and caching

Logging

The service logs two events: 1. Cache Cleared:
[service] All cache cleared
2. Providers Fetched:
[service] Fetched 5 providers
(Number varies based on available providers)

Use Cases

Fresh Start on Plugin Load

Every time the plugin loads, users get fresh data:
  • Updated provider list
  • Latest channel information (fetched on demand)
  • No stale cached content

Cache Warm-Up

Prefetching providers means:
  • Instant provider list display (no loading spinner)
  • Better user experience
  • Network requests happen in background

Example Scenario

User Action: Enable Cricfy plugin in Kodi Service Execution:
1. service.py starts

2. Cache cleared → log_info("All cache cleared")

3. get_providers() called

4. Providers fetched from remote

5. Providers decrypted and cached

6. log_info(f"Fetched {len(providers)} providers")

7. Service completes
User Experience: When user opens the Cricfy plugin UI:
  • Provider list loads instantly (from warm cache)
  • No network delay on first view
  • Fresh data guaranteed

Performance Considerations

Trade-offs:
  • Pro: Fast UI loading, fresh data on every startup
  • Con: Network request on every Kodi/plugin restart
The service prioritizes data freshness and user experience over minimizing network usage.

Error Handling

The service does not explicitly handle errors. If get_providers() fails:
  • Empty list is returned
  • Cache remains empty
  • Error is logged by get_providers() internally
  • Plugin UI will show empty provider list
Users may see “No providers available” if remote fetch fails during service initialization.

Customization

Developers can modify the service to: 1. Selective Cache Clearing:
# Only clear provider cache, keep channel caches
cache.delete(PROVIDERS_CACHE_KEY)
2. Conditional Prefetching:
# Only prefetch if cache is older than X hours
if should_refresh_cache():
    providers = get_providers()
3. Additional Initialization:
# Prefetch popular channels
if providers:
    popular_provider = providers[0]
    get_channels(popular_provider['catLink'])

See also:

Build docs developers (and LLMs) love