Skip to main content

Turn dumb cron jobs into state-aware, conditional pipelines

Project description

๐Ÿšฐ cron-pipe

Turn dumb cron jobs into state-aware, conditional pipelines.

A zero-dependency, ultra-lightweight Python tool that connects isolated cron jobs using State JSON files. It turns isolated scripts into a gated pipeline, saving computing resources and API costs.


๐Ÿค” The Problem

We all love cron for scheduling background scripts. But as your system grows, cron's lack of state awareness becomes a massive pain:

  • Wasted Resources: Your downstream scripts run at fixed intervals, even when there's no new data from the upstream script. (Wasting CPU, API limits, and LLM tokens).
  • The Communication Gap: If Script A (data fetcher) finishes, how does Script B (analyzer) know it's time to run? You usually end up writing messy inter-process communication or database flags.

๐Ÿ’ก The Solution

cron-pipe generates boilerplate code to connect your cron jobs via State JSON Files. It acts as a strict "Gatekeeper".

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Cron 14:25     โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  state.json  โ”‚โ—€โ”€โ”€โ”€โ”€โ”‚  Cron 14:49      โ”‚
โ”‚  Upstream       โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚  Downstream      โ”‚
โ”‚  (Writer)       โ”‚            โ”‚              โ”‚  (Reader/Gate)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚ Gate Open?   โ”‚
                         โ”‚  (PROCEED?)  โ”‚
                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”
                          โ”‚ Yes โ”‚  No โ”‚
                          โ”‚     โ”‚     โ”‚
                          โ–ผ     โ–ผ     โ–ผ
                     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                     โ”‚  Run   โ”‚ โ”‚ Exit (0) โ”‚
                     โ”‚ Logic  โ”‚ โ”‚ Silent   โ”‚
                     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Quick Start

Install via pip:

pip install cron-pipe

Generate a writer/reader pair instantly:

cron-pipe init --writer scan.py --reader decider.py

This generates two Python scripts with the cron-pipe pattern already implemented.

๐Ÿ› ๏ธ Usage

1. The Upstream (Writer)

Evaluates the environment and sets a state.

from cron_pipe import StateWriter

state = StateWriter("/tmp/daily_pipeline_state.json")
score = 0.85  # Your upstream logic calculates this...

if score > 0.6:
    state.set("PROCEED", score=score, metrics={"volatility": "high"})
else:
    state.set("HALT", reason="Market is dead water")

2. The Downstream (Reader / Gatekeeper)

Checks the state before running expensive logic.

from cron_pipe import StateGate

gate = StateGate("/tmp/daily_pipeline_state.json")

# Exits silently (sys.exit(0)) if action is "HALT"
# Also checks if the state file is older than 3600 seconds (stale prevention)
gate.require_proceed(max_age_seconds=3600)

# You can also pass a threshold if your writer provided a score
# gate.require_proceed(threshold=0.8)

# --- Your expensive/LLM logic runs ONLY if the gate is open ---
print("Gate is open! Running heavy analysis...")

Fail-Open Safety

cron-pipe is designed with a Fail-Open philosophy. If the state file is missing or corrupted, it prints a warning to stderr but allows the downstream script to proceed. We prefer false positives over a completely blocked pipeline.

Stale State Prevention

State files include an ISO 8601 timestamp. The reader can reject state older than max_age_seconds, preventing downstream scripts from acting on stale data after the upstream has stopped writing.

๐Ÿ“– Case Study: Why I Built This

I developed cron-pipe while building an Autonomous AI Quantitative Trading System for the A-Share market.

I had a pipeline where a heavy LLM agent (DeepSeek V4) analyzed market sentiment every few minutes near market close. However, 80% of the time, the market was "dead water" (low volatility), and calling the LLM API was a massive waste of tokens.

By using cron-pipe:

  • A lightweight Python script (runs via cron at 14:25) calculates market volatility locally (0 tokens).
  • It writes the status to a daily_market_state.json.
  • The heavy LLM trading agent (runs via cron at 14:49) uses StateGate. If the market is dead, it exits instantly.

This simple pattern saved me ~100K tokens daily while keeping the architecture decoupled.

๐Ÿค Let's Connect

I regularly share insights on Python engineering, Multi-Agent architectures, and Quantitative Trading.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cron_pipe-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cron_pipe-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file cron_pipe-0.1.0.tar.gz.

File metadata

  • Download URL: cron_pipe-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for cron_pipe-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4670102872aa9d17d7b405dd0a47bea37c566391e3a58fe069a8a6a955060c94
MD5 65b48993b7e0206f6324a4e267565dd1
BLAKE2b-256 be95d8de2c254722ed8d753e96bcc488b4898841832639b75a6bf9b8cb551b3f

See more details on using hashes here.

File details

Details for the file cron_pipe-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cron_pipe-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for cron_pipe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48b5d1c8a482c2d7c9749ddfcca46227b1cf6db00be9d2c414d82153f7e754b3
MD5 ce023ae22437cdf488fbbc0894d9e79f
BLAKE2b-256 381ed48ee1a545989b0a521370ad56ecf95babbd255aab41a6eb77cb0b7c10fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page