Skip to content

Commit 0ec8128

Browse files
Copilotsfreeman422
andauthored
Fix bash job scripts: require_command checks, curl temp file leak, batched MySQL inserts (#187)
* Initial plan * Fix code review feedback: require_command checks, temp file cleanup, batch MySQL inserts Co-authored-by: sfreeman422 <16405652+sfreeman422@users.noreply.github.com> Agent-Logs-Url: https://github.com/dev-chat/mocker/sessions/1c571688-b133-4945-9edc-a6d11e23df1d --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sfreeman422 <16405652+sfreeman422@users.noreply.github.com>
1 parent b5a6bd5 commit 0ec8128

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

packages/jobs/fun-fact-job/script.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,13 @@ send_slack_message() {
337337
--header "Authorization: Bearer ${MUZZLE_BOT_TOKEN}" \
338338
--header 'Content-Type: application/json; charset=utf-8' \
339339
--data "${payload}" \
340-
https://slack.com/api/chat.postMessage)
340+
https://slack.com/api/chat.postMessage || true)
341+
342+
if [[ -z "${response_code:-}" ]]; then
343+
echo "Slack API request failed: curl did not complete successfully" >&2
344+
rm -f "${response_file}"
345+
return 1
346+
fi
341347

342348
if [[ "${response_code}" != '200' ]] || ! jq -e '.ok == true' "${response_file}" >/dev/null 2>&1; then
343349
cat "${response_file}" >&2

packages/jobs/health-job/script.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ fi
1010

1111
PATH=/usr/local/bin:/usr/bin:/bin:${PATH:-}
1212

13+
require_command() {
14+
local command_name="$1"
15+
16+
if ! command -v "${command_name}" >/dev/null 2>&1; then
17+
echo "Missing required command: ${command_name}" >&2
18+
exit 1
19+
fi
20+
}
21+
1322
HEALTH_URL="${HEALTH_URL:-http://127.0.0.1:3000/health}"
1423
SLACK_CHANNEL="${SLACK_CHANNEL:-#muzzlefeedback}"
1524
SLACK_MESSAGE=':this-is-fine: `Moonbeam is experiencing some technical difficulties at the moment.` :this-is-fine:'
@@ -89,6 +98,10 @@ check_health() {
8998
}
9099

91100
main() {
101+
require_command curl
102+
require_command grep
103+
require_command mktemp
104+
92105
if check_health; then
93106
exit 0
94107
fi

packages/jobs/pricing-job/script.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ main() {
9696
local price_pct
9797
local price
9898
local median_rep
99+
local sql_batch
99100
local -a teams
100101
local -a items
101102
local row
@@ -125,18 +126,25 @@ main() {
125126

126127
median_rep=$(calculate_median_rep)
127128

129+
sql_batch=""
128130
for team_id in "${teams[@]}"; do
129131
[[ -n "${team_id}" ]] || continue
130132

131133
for item_row in "${items[@]}"; do
132134
IFS=$'\t' read -r item_id price_pct <<<"${item_row}"
133135
price=$(awk -v median="${median_rep}" -v pct="${price_pct}" 'BEGIN { printf "%.15f", median * pct }')
134-
mysql_query "INSERT INTO price(itemId, teamId, price, itemIdId) VALUES(${item_id}, '$(sql_escape "${team_id}")', ${price}, ${item_id});" >/dev/null
136+
sql_batch+="INSERT INTO price(itemId, teamId, price, itemIdId) VALUES(${item_id}, '$(sql_escape "${team_id}")', ${price}, ${item_id});"$'\n'
135137
done
136138

137-
echo "Completed update for ${team_id}"
139+
echo "Queued update for ${team_id}"
138140
done
139141

142+
if [[ -n "${sql_batch}" ]]; then
143+
echo 'Executing batch price inserts...'
144+
mysql_query "START TRANSACTION;
145+
${sql_batch}COMMIT;" || { echo 'Batch insert failed; transaction has been rolled back' >&2; return 1; }
146+
fi
147+
140148
echo "Completed job in $(( $(date +%s) - start_time )) seconds!"
141149
}
142150

0 commit comments

Comments
 (0)