Skip to content

Commit

Permalink
optimisations + enforce telegram max message length
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Hassan committed Jan 19, 2024
1 parent b724d9d commit 51a0805
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Release

permissions:
contents: write

on:
push:
branches:
- "main"
paths:
- "**/Cargo.toml"

env:
CARGO_TERM_COLOR: always

jobs:
release:
name: "Create New Release"
runs-on: "ubuntu-latest"

outputs:
version: ${{ steps.pkg.outputs.VERSION }}
exists: ${{ steps.check_tag.outputs.EXISTS }}

steps:
- name: "Check out the repo"
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: "Get the version number from this push"
id: pkg
shell: "bash"
run: |
VERSION=$(awk -F ' = ' '$1 ~ /version/ { gsub(/["]/, "", $2); printf("%s",$2) }' Cargo.toml)
echo VERSION=$VERSION >> $GITHUB_OUTPUT
- name: Check if this is a new version
id: check_tag
shell: bash
run: |
output=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/git/ref/tags/${{ steps.pkg.outputs.VERSION }})
message=$(echo "$output" | awk -F': ' '/"message":/ { gsub(/"/, "", $2); print $2; }' | tr -d ',')
echo EXISTS=$([ "$message" = "Not Found" ] && echo "false" || echo "true") >> $GITHUB_OUTPUT
echo $GITHUB_OUTPUT
- name: Create a release
if: steps.check_tag.outputs.EXISTS == 'false'
uses: ncipollo/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag: v${{ steps.pkg.outputs.VERSION }}
name: v${{ steps.pkg.outputs.VERSION }}
draft: false
prerelease: false
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "telelog"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

[dependencies]
Expand Down
18 changes: 12 additions & 6 deletions src/telegram.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lazy_static::lazy_static;
use std::borrow::BorrowMut;
use tokio;
use tokio::sync::Mutex as AsyncMutex;
use reqwest;
Expand Down Expand Up @@ -80,12 +79,19 @@ async fn flush_log_buffer() {
let mut buffer = LOG_BUFFER.lock().await;
// parse the buffer to form the telegram message
let mut message = String::from("<code>\n");
for entry in buffer.borrow_mut().iter() {
let colour_symbol = colour_translate(entry.priority);
message.push_str(&format!("{}[{}] {}: {}\n", colour_symbol, entry.timestamp.format("%b %d %H:%M:%S"), entry.identifier, entry.message));
}

buffer.retain(|entry: &LogEntry| {
let new_entry_string = format!("{}[{}] {}: {}\n", colour_translate(entry.priority), entry.timestamp.format("%b %d %H:%M:%S"), entry.identifier, entry.message);

if message.len() + new_entry_string.len() < 4088 {
message.push_str(&new_entry_string);
return false
}

return true
});

message.push_str("</code>");
buffer.clear();
drop(buffer); // release the lock

// send the message to telegram
Expand Down

0 comments on commit 51a0805

Please sign in to comment.