An employee opened fire at a California railyard on Wednesday, killing eight people before taking his own life as law enforcement rushed in, authorities said.

The shooting took place around 6:30am in two buildings at a light rail facility for the Valley Transportation Authority, which provides bus, light rail and other transit services throughout Santa Clara County, the most populated county in the San Francisco Bay Area.

Santa Clara County sheriff Laurie Smith told reporters: “When our deputies went through the door, initially he was still firing rounds.

“When our deputy saw him, he took his life. (Deputies) were going through hallways saying, ‘Sheriff’s office!’ He knew at that time that his time for firing shots was over.”

The victims, many of them longtime employees of the transit agency, were identified by the Santa Clara County coroner’s office as Paul Delacruz Megia, 42; Taptejdeep Singh, 36; Adrian Balleza, 29; Jose Dejesus Hernandez, 35; Timothy Michael Romo, 49; Michael Joseph Rudometkin, 40; Abdolvahab Alaghmandan, 63, and Lars Kepler Lane, 63.

Their jobs included bus and light rail operators, mechanics, linemen and assistant superintendent.

The attacker was identified as 57-year-old Sam Cassidy, according to two law enforcement officials. Investigators offered no immediate word on a possible motive.

His ex-wife, Cecilia Nelms, told The Associated Press that Cassidy had a bad temper and would tell her that he wanted to kill people at work, “but I never believed him, and it never happened, until now”.

Ms Nelms, teary-eyed and shaken by the news, said her ex-husband would come home wound up and angry about things that happened at work.

As he talked about it, “he would get more mad”, she said, adding: “He could dwell on things.”

Ms Nelms also said she was scared when Cassidy lost his temper, and he was someone who could physically hurt others.

She added they were married for 10 years — Cassidy filed for divorce in 2005 — and had not been in contact for 13 years. She said he had been treated for depression.

Sheriff’s spokesman deputy Russell Davis said he did not know the type of weapon used in the attack, though bomb squads searched the rail complex after receiving information about possible explosive devices.

Members of a union representing Valley Transportation Authority workers were meeting when the shooting began, San Jose Mayor Sam Liccardo said, but it is not clear the meeting was related to the attack.

Mr Davis added officials were also investigating a house fire that broke out shortly before the shooting.

Public records show Cassidy owned the two-story home where firefighters responded after being notified by a passer-by.

Cassidy had worked for Valley Transportation Authority since at least 2012, according to the public payroll and pension database Transparent California, first a mechanic from 2012 to 2014, then as someone who maintained substations.

California governor Gavin Newsom, said victims’ relatives were “waiting to hear from the coroner, waiting to hear from any of us, just desperate to find out if their brother, their son, their dad, their mum is still alive”.

Another man wounded in the attack was in critical condition at Santa Clara Valley Medical Centre, spokesperson Joy Alexiou said.

At the White House, US President Joe Biden ordered flags to be flown at half-staff and urged Congress to act on legislation to curb gun violence.

He said in a statement: “Every life that is taken by a bullet pierces the soul of our nation. We can, and we must, do more.”

Wednesday’s attack was Santa Clara County’s second mass shooting in less than two years. A gunman killed three people and then himself at a popular garlic festival in Gilroy in July 2019.